Kaggle competition: Bike Sharing Demand
for ds in comb:
ds.loc[ds['Fare'] <= 10.5, 'Fare'] = 0
ds.loc[(ds['Fare'] > 10.5)&(ds['Fare'] <= 39.688), 'Fare'] = 1
ds.loc[ds['Fare'] > 39.688, 'Fare'] = 2
ds['Fare'] = ds['Fare'].astype(int)
 comb[0][['Fare', 'Survived']].groupby(['Fare'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<categorify>
train=pd.read_csv(r'.. /input/bike-sharing-demand/train.csv')
 test=pd.read_csv(r'.. /input/bike-sharing-demand/test.csv')
 df=train.copy()
test_df=test.copy()
df.head()
s_dict = {'male': 0, 'female': 1}
for ds in comb:
ds['Sex'] = ds['Sex'].map(s_dict)
 ds['Embarked'] = ds['Embarked'].astype('category' ).cat.codes
ds['TickType'] = ds['TickType'].astype('category' ).cat.codes
ds['Rank'] = ds['Rank'].astype('category' ).cat.codes<drop_column>
df['datetime'] = pd.to_datetime(df['datetime'])
 test_df['datetime'] = pd.to_datetime(test_df['datetime'])
 df['year'] = df['datetime'].apply(lambda x: x.year)
 df['month'] = df['datetime'].apply(lambda x: x.month)
 df['day'] = df['datetime'].apply(lambda x: x.day)
 df['hour'] = df['datetime'].apply(lambda x: x.hour)
 test_df['year'] = test_df['datetime'].apply(lambda x: x.year)
 test_df['month'] = test_df['datetime'].apply(lambda x: x.month)
 test_df['day'] = test_df['datetime'].apply(lambda x: x.day)
 test_df['hour'] = test_df['datetime'].apply(lambda x: x.hour)
 df.drop(['datetime', 'casual', 'registered'], axis=1, inplace=True)
 test_df.drop(['datetime'], axis=1, inplace=True)
 df
drop_col_test = ['Cabin', 'Name', 'Parch', 'SibSp', 'RelatAboard', 'Ticket']
drop_col_train = drop_col_test.copy()
drop_col_train.extend(['FareGroup', 'AgeGroup', 'PassengerId'])
 comb[0] = comb[0].drop(columns=drop_col_train)
 comb[1] = comb[1].drop(columns=drop_col_test )<prepare_x_and_y>
df = pd.get_dummies(df, columns=['year', 'month', 'day', 'hour', 'holiday', 'workingday', 'season', 'weather'])
 test_df = pd.get_dummies(test_df, columns=['year', 'month', 'day', 'hour', 'holiday', 'workingday', 'season', 'weather'])
 df, test_df = df.align(test_df, join='left', axis=1)
 test_df = test_df.drop(['count'], axis=1)
 print(df.shape)
 print(test_df.shape )
train = comb[0]
test = comb[1]<sort_values>
def rmsle(y, pred):
log_y = np.log1p(y)
 log_pred = np.log1p(pred)
 squared_error =(log_y - log_pred)**2
rmsle = np.sqrt(np.mean(squared_error))
return rmsle
train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
df_train_target = df['count']
df_train_features = df.drop('count',axis=1)
 def print_best_params(model, params):
grid_model = GridSearchCV(
 model,
param_grid = params,
cv=5,
scoring='neg_mean_squared_error')
 grid_model.fit(df_train_features, df_train_target)
 rmse = np.sqrt(-1*grid_model.best_score_)
 print(
 '{0} 5 CV 시 최적 평균 RMSE 값 {1}, 최적 alpha:{2}'.format(model.__class__.__name__, np.round(rmse, 4), grid_model.best_params_))
return grid_model.best_estimator_
train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
df['count'] = np.log1p(df['count'] )
train[['Age', 'Survived']].groupby(['Age'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
x_train,x_test,y_train,y_test=train_test_split(df.drop('count',axis=1),df['count'],test_size=0.3,random_state=42)
 lr_reg = LinearRegression()
lr_reg.fit(x_train, y_train)
 lr_pred = lr_reg.predict(x_test)
 y_test_exp = np.expm1(y_test)
 lr_pred_exp = np.expm1(lr_pred)
 print('LinearRegression RMSLE:', rmsle(y_test_exp, lr_pred_exp))
train[['Fare', 'Survived']].groupby(['Fare'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
rf_model = RandomForestRegressor()
rf_model.fit(x_train, y_train)
 rf_pred = rf_model.predict(x_test)
 y_test_exp = np.expm1(y_test)
 rf_pred_exp = np.expm1(rf_pred)
 print('RandomForest RMSLE:', rmsle(y_test_exp, rf_pred_exp))
train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
xgb_model = XGBRegressor(learning_rate=0.2)
 xgb_model.fit(x_train, y_train)
 xgb_pred = xgb_model.predict(x_test)
 y_test_exp = np.expm1(y_test)
 xgb_pred_exp = np.expm1(xgb_pred)
 print('xgboost RMSLE:', rmsle(y_test_exp, xgb_pred_exp))
train[['Cabin_Null', 'Survived']].groupby(['Cabin_Null'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
lgb_params = {
'learning_rate' : [0.05],
'n_estimators':[500],
'max_bin' : [80],
}
lgb_model = LGBMRegressor()
lgb_model.fit(x_train, y_train)
 lgb_pred = lgb_model.predict(x_test)
 y_test_exp = np.expm1(y_test)
 lgb_pred_exp = np.expm1(lgb_pred)
 print('LGBMRegressor RMSLE:', rmsle(y_test_exp,lgb_pred_exp))
lgb_estimator = print_best_params(lgb_model, lgb_params)
train[['TickType', 'Survived']].groupby(['TickType'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
X_train = df.drop(['count'], axis=1)
 y_train = df['count']
X_test = test_df
X_test
train[['OTTmembers', 'Survived']].groupby(['OTTmembers'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
lgb_model = LGBMRegressor()
lgb_model.fit(X_train, y_train)
 pred = lgb_model.predict(X_test)
 pred_exp = np.expm1(pred)
 submission = pd.read_csv('.. /input/bike-sharing-demand/sampleSubmission.csv')
 submission.loc[:, 'count'] = pred_exp
submission
train[['Rank', 'Survived']].groupby(['Rank'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<split>
submission.to_csv('submission.csv', index=False )
<train_model><EOS> 
submission.to_csv('submission.csv', index=False )