Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<feature_engineering>
import pandas as pd
import seaborn as sns
import numpy as np
import datetime
from sklearn.ensemble import RandomForestRegressor
from sklearn import preprocessing
test_data.loc[ test_data['Age'] <= 21, 'Age'] = 0
test_data.loc[(test_data['Age'] > 21)&(test_data['Age'] <= 34), 'Age'] = 1
test_data.loc[(test_data['Age'] > 34)&(test_data['Age'] <= 54), 'Age'] = 2
test_data.loc[(test_data['Age'] > 60)&(test_data['Age'] <= 75), 'Age'] = 3
test_data.loc[ test_data['Age'] > 75, 'Age'] ;<feature_engineering>
train = pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')
 test = pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv')
 data = train.append(test, sort=False )
test_data.loc[ test_data['Fare'] <= 7.5, 'Fare'] = 0
test_data.loc[(test_data['Fare'] > 15)&(test_data['Fare'] <= 21.5), 'Fare'] = 1
test_data.loc[(test_data['Fare'] > 21.5)&(test_data['Age'] <= 29), 'Fare'] = 2
test_data.loc[(test_data['Fare'] > 29)&(test_data['Fare'] <= 36.5), 'Fare'] = 3
test_data.loc[ test_data['Fare'] > 36.5, 'Fare'] ;<drop_column>
data['datetime'] = data['datetime'].astype('datetime64[ns]')
 data['Day'] = pd.DatetimeIndex(data['datetime'] ).day
data['Month'] = pd.DatetimeIndex(data['datetime'] ).month
data['Year'] = pd.DatetimeIndex(data['datetime'] ).year
data['Hour'] = pd.DatetimeIndex(data['datetime'] ).hour
data['weekday'] = pd.DatetimeIndex(data['datetime'] ).weekday
x = train_data.drop("Survived", axis = 1 )<prepare_x_and_y>
df=data.drop(['registered','casual','atemp','Day','season'],axis=1 )
y = train_data["Survived"]<import_modules>
lb = preprocessing.LabelBinarizer()
df.Year = lb.fit_transform(df.Year)
 cont=['temp','humidity','windspeed']
feat=df[cont]
minmax_scale = preprocessing.MinMaxScaler().fit(feat.values)
 df[cont] = minmax_scale.transform(feat.values )
from sklearn.model_selection import train_test_split<import_modules>
categ=['holiday','weather','Month','Year','Hour','weekday','workingday']
for var in categ:
df[var] = df[var].astype("category")
 df = pd.get_dummies(data=df, columns=['holiday','weather','workingday','Year'])
 df.head()
from sklearn.model_selection import train_test_split<split>
df_train = df[pd.notnull(df['count'])].sort_values(by=['datetime'])
 df_test = df[~pd.notnull(df['count'])].sort_values(by=['datetime'] )
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.4, random_state = 12 )<train_model>
df_train=df_train.drop(['datetime'],axis=1)
 test=df_test.drop(['datetime','count'],axis=1 )
dtree = DecisionTreeClassifier()
dtree.fit(x_train, y_train)
 y_pred = dtree.predict(x_test)
 dtree_accuracy = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(dtree_accuracy )<train_model>
df_train['count'] = np.log1p(df_train['count'] )
rf = RandomForestClassifier(n_estimators=50, max_depth = 8)
 rf.fit(x_train, y_train)
 y_pred = rf.predict(x_test)
 acc_rf = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_rf )<compute_train_metric>
X = df_train.drop(['count'],axis=1)
 y=df_train['count']
X.shape
lr = LogisticRegression(solver='liblinear', dual = False)
 lr.fit(x_train, y_train)
 y_pred = lr.predict(x_test)
 acc_log = round(lr.score(x_train, y_train)* 100, 2)
 acc_log
accuracy_score(y_test, y_pred )<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=.80, random_state=1)
 rfr = RandomForestRegressor(n_estimators = 100)
 rfr.fit(X_train, y_train)
 pred = rfr.predict(X_test)
 sns.scatterplot(x = y_test, y = pred )
rf = RandomForestClassifier(n_estimators=50, max_depth = 8 )<train_model>
print('RMSLE:', np.sqrt(metrics.mean_squared_log_error(np.expm1(y_test), np.expm1(pred))))
rf.fit(x_train, y_train )<drop_column>
gbm = GradientBoostingRegressor(n_estimators=2000,alpha=0.01)
 gbm.fit(X_train,y_train)
 preds = gbm.predict(X_test)
 print('RMSLE:', np.sqrt(metrics.mean_squared_log_error(np.expm1(y_test), np.expm1(preds))))
x_test = test_data<predict_on_test>
algo_gbm = gbm.predict(X_test)
 algo_rf = rfr.predict(X_test)
 algo_mean =np.expm1(algo_gbm)*0.9 + np.expm1(algo_rf)*0.1
print('RMSLE:', np.sqrt(metrics.mean_squared_log_error(np.expm1(y_test), algo_mean)) )
y_test_predict = rf.predict(x_test )<save_to_csv>
algo_gbm_tst = gbm.predict(test)
 algo_rf_tst = rfr.predict(test)
 algo_mean_tst =np.expm1(algo_gbm_tst)*0.9 + np.expm1(algo_rf_tst)*0.1
output_data = pd.DataFrame({'PassengerId' : test_data.PassengerId, 'Survived' : y_test_predict})
 output_data.to_csv('Titanic_Survival.csv', index = False)
 print("Submission is successfully" )<define_variables>
submission = pd.DataFrame({'datetime':df_test['datetime'],'count':algo_mean_tst})
 submission.head()
random_state_split = 7
Dropout_num = 0
learning_rate = 6e-6
valid = 0.2
epochs_num = 3
batch_size_num = 16
target_corrected = False
target_big_corrected = False<set_options>
submission.to_csv('Submission.csv',index=False )