Kaggle competition: Bike Sharing Demand
gc.collect()<train_model>
df = pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')
 test = pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv')
 df.shape, test.shape
lgbmr = LGBMRegressor(n_estimators=500, random_state=10)
 lgbmr.fit(df_train, y_train)
 y_predict = lgbmr.predict(df_val)
 score = np.sqrt(mse(y_val, y_predict))
print(f"score: {score}")
<sort_values>
df['count'] = np.log(df['count'] )
lgbmr_errors.sort_values(by='error', ascending=False ).head()<prepare_x_and_y>
df_total = df.append(test )
X_1st_half = df_train[:int(df_train.shape[0]/2)]
y_1st_half = y_train[:int(df_train.shape[0]/2)]
X_2nd_half = df_train[int(df_train.shape[0]/2):]
y_2nd_half = y_train[int(df_train.shape[0]/2):]<predict_on_test>
df_total['datetime'] = pd.to_datetime(df_total['datetime'] )
lgbmr_1st_half = LGBMRegressor(random_state=10)
 lgbmr_2nd_half = LGBMRegressor(random_state=10)
 lgbmr_1st_half.fit(X_1st_half, y_1st_half)
 lgbmr_2nd_half.fit(X_2nd_half, y_2nd_half)
 y_predict_1 = lgbmr_1st_half.predict(df_val)
 y_predict_2 = lgbmr_2nd_half.predict(df_val)
 y_predict_1_2 =(pd.Series(data=y_predict_1, name='prediction_1')+ pd.Series(data=y_predict_2, name='prediction_2')) /2
score = np.sqrt(mse(y_val, y_predict_1_2))
print(f"score: {score}" )<train_model>
df_total['year'] = df_total['datetime'].dt.year
df_total['month'] = df_total['datetime'].dt.month
df_total['day'] = df_total['datetime'].dt.day
df_total['dayofweek'] = df_total['datetime'].dt.dayofweek
df_total['hour'] = df_total['datetime'].dt.hour
df_total.info()
X_1st_half = df_train[:int(pd.concat([df_train, df_val], axis=0 ).shape[0]/2)]
y_1st_half = y_train[:int(pd.concat([df_train, df_val], axis=0 ).shape[0]/2)]
X_2nd_half = df_train[int(pd.concat([df_train, df_val], axis=0 ).shape[0]/2):]
y_2nd_half = y_train[int(pd.concat([df_train, df_val], axis=0 ).shape[0]/2):]
lgbmr_1st_half = LGBMRegressor(random_state=10)
 lgbmr_2nd_half = LGBMRegressor(random_state=10)
 lgbmr_1st_half.fit(X_1st_half, y_1st_half)
 lgbmr_2nd_half.fit(X_2nd_half, y_2nd_half )<load_pretrained>
train, valid = train_test_split(df, random_state=42)
 train.shape, valid.shape
filename = 'lgbm_model1.pickle'
pickle.dump(lgbmr, open(filename, 'wb'))
loaded_model = pickle.load(open(filename, 'rb'))
filename = 'lgbmr_1st_half.pickle'
pickle.dump(lgbmr, open(filename, 'wb'))
lgbmr_1st_half = pickle.load(open(filename, 'rb'))
filename = 'lgbmr_2nd_half.pickle'
pickle.dump(lgbmr, open(filename, 'wb'))
lgbmr_2nd_half = pickle.load(open(filename, 'rb'))<drop_column>
removed = ['casual', 'registered', 'count', 'datetime']
feats = [col for col in train.columns if col not in removed]
del df_train, df_val, y_train, y_val, lgbmr
gc.collect()<predict_on_test>
rf = RandomForestRegressor(random_state=42 )
STEP = 1000000
y_test_predict = []
for i in range(0, df_test.shape[0], STEP):
batch_prediction = lgbmr_1st_half.predict(df_test.loc[i:i+STEP-1,:])
 y_test_predict.append(list(batch_prediction))
y_test_1st_half = []
for predictions in y_test_predict:
y_test_1st_half = y_test_1st_half + predictions
STEP = 1000000
y_test_predict = []
for i in range(0, df_test.shape[0], STEP):
batch_prediction = lgbmr_2nd_half.predict(df_test.loc[i:i+STEP-1,:])
 y_test_predict.append(list(batch_prediction))
y_test_2nd_half = []
for predictions in y_test_predict:
y_test_2nd_half = y_test_2nd_half + predictions 
<feature_engineering>
rf.fit(train[feats], train['count'] )
sample['meter_reading'] =(pd.Series(data=y_test_1st_half, name='pred_1st_half')+
pd.Series(data=y_test_2nd_half, name='pred_2nd_half')) /2<feature_engineering>
preds = rf.predict(valid[feats] )
sample['meter_reading'] = np.expm1(sample['meter_reading'] )<save_to_csv>
mean_squared_error(valid['count'], preds)**(1/2 )
sample.to_csv("submission.csv", index=False, float_format='%.4f' )<define_variables>
test['count'] = np.exp(rf.predict(test[feats]))
path_data = "/kaggle/input/ashrae-energy-prediction/"
path_train = path_data + "train.csv"
path_test = path_data + "test.csv"
path_building = path_data + "building_metadata.csv"
path_weather_train = path_data + "weather_train.csv"
path_weather_test = path_data + "weather_test.csv"
plt.style.use("seaborn")
 sns.set(font_scale=1)
 myfavouritenumber = 0
seed = myfavouritenumber
random.seed(seed )<load_from_csv>
test[['datetime', 'count']].to_csv('rf.csv', index=False )
df_train = pd.read_csv(path_train)
 building = pd.read_csv(path_building)
 le = LabelEncoder()
building.primary_use = le.fit_transform(building.primary_use)
 weather_train = pd.read_csv(path_weather_train )<set_options>
rf2 = RandomForestRegressor(random_state=42, n_estimators=200, n_jobs=-1, min_samples_leaf=4)
 rf2.fit(train[feats], train['count'])
 preds2 = rf2.predict(valid[feats])
 mean_squared_error(valid['count'], preds2)**(1/2 )
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<categorify>
test['count'] = np.exp(rf2.predict(test[feats]))
test[['datetime', 'count']].to_csv('rf2.csv', index=False )
df_train = reduce_mem_usage(df_train, use_float16=True)
 building = reduce_mem_usage(building, use_float16=True)
 weather_train = reduce_mem_usage(weather_train, use_float16=True )<data_type_conversions>
train = df[df['day'] <= 15]
valid = df[df['day'] > 15]
train.shape, valid.shape
def prepare_data(X, building_data, weather_data, test=False):

 X = X.merge(building_data, on="building_id", how="left")
 X = X.merge(weather_data, on=["site_id", "timestamp"], how="left")
 X.timestamp = pd.to_datetime(X.timestamp, format="%Y-%m-%d %H:%M:%S")
 X.square_feet = np.log1p(X.square_feet)
 if not test:
X.sort_values("timestamp", inplace=True)
 X.reset_index(drop=True, inplace=True)
 gc.collect()
holidays = ["2016-01-01", "2016-01-18", "2016-02-15", "2016-05-30", "2016-07-04",
"2016-09-05", "2016-10-10", "2016-11-11", "2016-11-24", "2016-12-26",
"2017-01-01", "2017-01-16", "2017-02-20", "2017-05-29", "2017-07-04",
"2017-09-04", "2017-10-09", "2017-11-10", "2017-11-23", "2017-12-25",
"2018-01-01", "2018-01-15", "2018-02-19", "2018-05-28", "2018-07-04",
"2018-09-03", "2018-10-08", "2018-11-12", "2018-11-22", "2018-12-25",
"2019-01-01"]
X["hour"] = X.timestamp.dt.hour
X["weekday"] = X.timestamp.dt.weekday
X["is_holiday"] =(X.timestamp.dt.date.astype("str" ).isin(holidays)).astype(int)
 drop_features = ["timestamp", "sea_level_pressure", "wind_direction", "wind_speed"]
X.drop(drop_features, axis=1, inplace=True)
 if test:
row_ids = X.row_id
X.drop("row_id", axis=1, inplace=True)
 return X, row_ids
else:
y = np.log1p(X.meter_reading)
 X.drop("meter_reading", axis=1, inplace=True)
 return X, y<prepare_x_and_y>
rf3 = RandomForestRegressor(random_state=42, n_estimators=200, n_jobs=-1, min_samples_leaf=4)
 rf3.fit(train[feats], train['count'])
 preds3 = rf3.predict(valid[feats])
 mean_squared_error(valid['count'], preds3)**(1/2 )
X_train, y_train = prepare_data(df_train, building, weather_train)
 del df_train, weather_train
gc.collect()<create_dataframe>
test['count'] = np.exp(rf3.predict(test[feats]))
test[['datetime', 'count']].to_csv('rf3.csv', index=False )
X_half_1 = X_train[:int(X_train.shape[0] / 2)]
X_half_2 = X_train[int(X_train.shape[0] / 2):]
y_half_1 = y_train[:int(X_train.shape[0] / 2)]
y_half_2 = y_train[int(X_train.shape[0] / 2):]
categorical_features = ["building_id", "site_id", "meter", "primary_use", "hour", "weekday"]
d_half_1 = lgb.Dataset(X_half_1, label=y_half_1, categorical_feature=categorical_features, free_raw_data=False)
 d_half_2 = lgb.Dataset(X_half_2, label=y_half_2, categorical_feature=categorical_features, free_raw_data=False)
 watchlist_1 = [d_half_1, d_half_2]
watchlist_2 = [d_half_2, d_half_1]
params = {
"objective": "regression",
"boosting": "gbdt",
"num_leaves": 40,
"learning_rate": 0.05,
"feature_fraction": 0.85,
"reg_lambda": 2,
"metric": "rmse"
}
print("Building model with first half and validating on second half:")
 model_half_1 = lgb.train(params, train_set=d_half_1, num_boost_round=1000, valid_sets=watchlist_1, verbose_eval=200, early_stopping_rounds=200)
 print("Building model with second half and validating on first half:")
 model_half_2 = lgb.train(params, train_set=d_half_2, num_boost_round=1000, valid_sets=watchlist_2, verbose_eval=200, early_stopping_rounds=200 )<drop_column>
df_total = df.append(test)
 df_total.sort_values('datetime', inplace=True )
del X_train, y_train, X_half_1, X_half_2, y_half_1, y_half_2, d_half_1, d_half_2, watchlist_1, watchlist_2, df_fimp_1, df_fimp_2, df_fimp
gc.collect()<load_from_csv>
df_total['rolling_temp'] = df_total['temp'].rolling(3, min_periods=1 ).mean()
df_total['rolling_atemp'] = df_total['atemp'].rolling(3, min_periods=1 ).mean()
df_total['rolling_humidity'] = df_total['humidity'].rolling(3, min_periods=1 ).mean()
df_total['rolling_windspeed'] = df_total['windspeed'].rolling(3, min_periods=1 ).mean()
df_test = pd.read_csv(path_test)
 weather_test = pd.read_csv(path_weather_test)
 df_test = reduce_mem_usage(df_test)
 weather_test = reduce_mem_usage(weather_test)
 X_test, row_ids = prepare_data(df_test, building, weather_test, test=True )<set_options>
train = df[df['day'] <= 15]
valid = df[df['day'] > 15]
train.shape, valid.shape
del df_test, building, weather_test
gc.collect()<predict_on_test>
rf4 = RandomForestRegressor(random_state=42, n_estimators=200, n_jobs=-1, min_samples_leaf=4)
 rf4.fit(train[feats], train['count'])
 preds4 = rf4.predict(valid[feats])
 mean_squared_error(valid['count'], preds4)**(1/2 )
pred = np.expm1(model_half_1.predict(X_test, num_iteration=model_half_1.best_iteration)) / 2
del model_half_1
gc.collect()
pred += np.expm1(model_half_2.predict(X_test, num_iteration=model_half_2.best_iteration)) / 2
del model_half_2
gc.collect()<save_to_csv>
test['count'] = np.exp(rf4.predict(test[feats]))
test[['datetime', 'count']].to_csv('rf4.csv', index=False )