Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<save_to_csv>
train = pd.read_csv("/kaggle/input/train.csv")
 test = pd.read_csv("/kaggle/input/test.csv")
 submission = pd.read_csv("/kaggle/input/sampleSubmission.csv" )
site_1.to_csv('site1.csv.gz',index=False,compression='gzip', float_format='%.4f' )<merge>
all_data_temp = pd.concat([train, test])
 all_data_temp
t = test[['building_id', 'meter', 'timestamp']]
t['row_id'] = t.index
site_1 = site_1.merge(t, left_on = ['building_id', 'meter', 'timestamp'], right_on = ['building_id', 'meter', 'timestamp'], how = "left")
 site_1 = site_1[['meter_reading_scraped', 'row_id']].set_index('row_id' ).dropna()
submission.loc[site_1.index, 'meter_reading'] = site_1['meter_reading_scraped']
del site_1
gc.collect()<load_from_csv>
all_data = pd.concat([train, test], ignore_index=True)
 all_data
site_2 = pd.read_csv('.. /input/asu-buildings-energy-consumption/asu_2016-2018.csv', parse_dates = ['timestamp'])
 site_2 = site_2[site_2['timestamp'].dt.year > 2016]<save_to_csv>
all_data['date'] = all_data['datetime'].apply(lambda x: x.split() [0])
 all_data['year'] = all_data['datetime'].apply(lambda x: x.split() [0].split('-')[0])
 all_data['month'] = all_data['datetime'].apply(lambda x: x.split() [0].split('-')[1])
 all_data['hour'] = all_data['datetime'].apply(lambda x: x.split() [1].split(':')[0])
 all_data["weekday"] = all_data['date'].apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).weekday() )
site_2.to_csv('site2.csv.gz',index=False,compression='gzip', float_format='%.4f' )<merge>
categorical_features = ['season', 'holiday', 'workingday', 'weather', 'weekday', 'month', 'year', 'hour']
for feature in categorical_features:
all_data[feature] = all_data[feature].astype("category" )
t = test[['building_id', 'meter', 'timestamp']]
t['row_id'] = t.index
site_2 = site_2.merge(t, left_on = ['building_id', 'meter', 'timestamp'], right_on = ['building_id', 'meter', 'timestamp'], how = "left")
 site_2 = site_2[['meter_reading', 'row_id']].set_index('row_id' ).dropna()
submission.loc[site_2.index, 'meter_reading'] = site_2['meter_reading']
del site_2
gc.collect()<save_to_csv>
train = all_data[pd.notnull(all_data['count'])]
test = all_data[~pd.notnull(all_data['count'])]
y = train['count']
submission.to_csv('submission.csv' )<define_variables>
drop_features = ['count', 'casual', 'registered', 'datetime', 'date', 'datetime', 'windspeed', 'month']
X_train = train.drop(drop_features, axis=1)
 X_test = test.drop(drop_features, axis=1 )
folds = 5
debug = False
num_rounds=1000
use_ucf = True
use_sort = True
del_2016 = True
ucf_year = [2017, 2018]<set_options>
def rmsle(y_true, y_pred, convertExp=True):
if convertExp:
y_true = np.exp(y_true)
 y_pred = np.exp(y_pred)
 log_true = np.nan_to_num(np.array([np.log(y+1)for y in y_true]))
log_pred = np.nan_to_num(np.array([np.log(y+1)for y in y_pred]))
output = np.sqrt(np.mean(( log_true - log_pred)**2))
return output
py.init_notebook_mode(connected=True)
<set_options>
linear_reg_model = LinearRegression()
log_y = np.log1p(y)
 linear_reg_model.fit(X_train, log_y)
 preds = linear_reg_model.predict(X_train)
 print('Linear Regression RMSLE:', rmsle(log_y, preds, True))
def reduce_mem_usage(df, use_float16=False):

 start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
if is_datetime(df[col])or is_categorical_dtype(df[col]):
continue
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if use_float16 and c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 else:
df[col] = df[col].astype('category')
 end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 *(start_mem - end_mem)/ start_mem))
return df<load_from_disk>
ridge_model = Ridge()
ridge_params = {'max_iter':[3000], 'alpha':[0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000]}
rmsle_scorer = metrics.make_scorer(rmsle, greater_is_better=False)
 gridsearch_ridge_model = GridSearchCV(estimator=ridge_model,
param_grid=ridge_params,
scoring=rmsle_scorer,
cv=5)
 log_y = np.log1p(y)
 gridsearch_ridge_model.fit(X_train, log_y)
 print('Best Parameter:', gridsearch_ridge_model.best_params_)
 preds = gridsearch_ridge_model.best_estimator_.predict(X_train)
 print('Ridge Regression RMSLE:', rmsle(log_y, preds, True))
%%time
root = Path('.. /input/ashrae-feather-format-for-fast-loading')
 train_df = pd.read_feather(root/'train.feather')
 weather_train_df = pd.read_feather(root/'weather_train.feather')
 weather_test_df = pd.read_feather(root/'weather_test.feather')
 building_meta_df = pd.read_feather(root/'building_metadata.feather' )<categorify>
lasso_model = Lasso()
lasso_alpha = 1/np.array([0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000])
 lasso_params = {'max_iter':[3000], 'alpha':lasso_alpha}
gridsearch_lasso_model = GridSearchCV(estimator=lasso_model,
param_grid=lasso_params,
scoring=rmsle_scorer,
cv=5)
 log_y = np.log1p(y)
 gridsearch_lasso_model.fit(X_train, log_y)
 print('Best Parameter:', gridsearch_lasso_model.best_params_)
 preds = gridsearch_lasso_model.best_estimator_.predict(X_train)
 print('Lasso Regression RMSLE:', rmsle(log_y, preds, True))
ucf_root = Path('.. /input/ashrae-ucf-spider-and-eda-full-test-labels')
 leak_df = pd.read_pickle(ucf_root/'site0.pkl')
 leak_df['meter_reading'] = leak_df.meter_reading_scraped
leak_df.drop(['meter_reading_original','meter_reading_scraped'], axis=1, inplace=True)
 leak_df.fillna(0, inplace=True)
 leak_df.loc[leak_df.meter_reading < 0, 'meter_reading'] = 0
leak_df = leak_df[leak_df.timestamp.dt.year > 2016]
print(len(leak_df))<concatenate>
randomforest_model = RandomForestRegressor()
rf_params = {'random_state':[42], 'n_estimators':[100, 120, 140]}
gridsearch_random_forest_model = GridSearchCV(estimator=randomforest_model,
param_grid=rf_params,
scoring=rmsle_scorer,
cv=5)
 log_y = np.log1p(y)
 gridsearch_random_forest_model.fit(X_train, log_y)
 print('Best Parameter:', gridsearch_random_forest_model.best_params_)
 preds = gridsearch_random_forest_model.best_estimator_.predict(X_train)
 print('Random Forest Regression RMSLE:', rmsle(log_y, preds, True))
<load_pretrained><EOS> 
submission['count'] = np.exp(randomforest_preds)
 submission.to_csv('submission.csv', index=False )