Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<set_options>
% matplotlib inline
data = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 print(data.head(5))
del features, target
gc.collect()<load_from_csv>
data['datetime'] = pd.to_datetime(data['datetime'])
 data['hour'] = data['datetime'].dt.hour
data['month'] = data['datetime'].dt.month
test['datetime'] = pd.to_datetime(test['datetime'])
 test['hour'] = data['datetime'].dt.hour
test['month'] = data['datetime'].dt.month
test_df = pd.read_csv(DATA_PATH + 'test.csv')
 row_ids = test_df["row_id"]
test_df.drop("row_id", axis=1, inplace=True)
 test_df = reduce_mem_usage(test_df )<merge>
data['season'] = data.season.astype('category')
 data['month'] = data.month.astype('category')
 data['hour'] = data.hour.astype('category')
 data['holiday'] = data.holiday.astype('category')
 data['workingday'] = data.workingday.astype('category')
 data['weather'] = data.weather.astype('category')
 test['season'] = test.season.astype('category')
 test['month'] = test.month.astype('category')
 test['hour'] = test.hour.astype('category')
 test['holiday'] = test.holiday.astype('category')
 test['workingday'] = test.workingday.astype('category')
 test['weather'] = test.weather.astype('category')
test_df = test_df.merge(building_df,left_on='building_id',right_on='building_id',how='left')
 del building_df
gc.collect()<load_from_csv>
data.drop('datetime',axis=1,inplace=True)
 test.drop('datetime',axis=1,inplace=True )
weather_df = pd.read_csv(DATA_PATH + 'weather_test.csv')
 weather_df = fill_weather_dataset(weather_df)
 weather_df = reduce_mem_usage(weather_df)
<merge>
dataWithoutOutliers = data[np.abs(data["count"]-data["count"].mean())<=(3*data["count"].std())]
test_df = test_df.merge(weather_df,how='left',on=['timestamp','site_id'])
 del weather_df
gc.collect()<feature_engineering>
dataTrain = pd.read_csv(".. /input/train.csv")
 dataTest = pd.read_csv(".. /input/test.csv")
 MergeData = dataTrain.append(dataTest)
 MergeData.reset_index(inplace=True)
 MergeData.drop('index',inplace=True,axis=1 )
test_df = features_engineering(test_df )<predict_on_test>
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning)
 %matplotlib inline
MergeData["date"] = MergeData.datetime.apply(lambda x : x.split() [0])
 MergeData["hour"] = MergeData.datetime.apply(lambda x : x.split() [1].split(":")[0] ).astype("int")
 MergeData["year"] = MergeData.datetime.apply(lambda x : x.split() [0].split("-")[0])
 MergeData["weekday"] = MergeData.date.apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).weekday())
 MergeData["month"] = MergeData.date.apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).month)
results = []
for model in models:
if results == []:
results = np.expm1(model.predict(test_df, num_iteration=model.best_iteration)) / len(models)
 else:
results += np.expm1(model.predict(test_df, num_iteration=model.best_iteration)) / len(models)
 del model
gc.collect()<set_options>
dataWind0 = MergeData[MergeData["windspeed"]==0]
dataWindNot0 = MergeData[MergeData["windspeed"]!=0]
rfg_wind = RandomForestRegressor()
windColumns = ["season","weather","humidity","month","temp","year","atemp"]
rfg_wind.fit(dataWindNot0[windColumns], dataWindNot0["windspeed"])
 wind0Values = rfg_wind.predict(X= dataWind0[windColumns])
 dataWind0["windspeed"] = wind0Values
MergeData = dataWindNot0.append(dataWind0)
 MergeData.reset_index(inplace=True)
 MergeData.drop('index',inplace=True,axis=1 )
del test_df, models
gc.collect()<save_to_csv>
categoricalFeatureNames = ["season","holiday","workingday","weather","weekday","month","year","hour"]
numericalFeatureNames = ["temp","humidity","windspeed","atemp"]
dropFeatures = ['casual',"count","datetime","date","registered"]
results_df = pd.DataFrame({"row_id": row_ids, "meter_reading": np.clip(results, 0, a_max=None)})
 del row_ids,results
gc.collect()
results_df.to_csv("submission.csv", index=False)
<set_options>
for var in categoricalFeatureNames:
MergeData[var] = MergeData[var].astype("category" )
warnings.filterwarnings('ignore')
<load_from_csv>
train = MergeData[pd.notnull(MergeData['count'])].sort_values(by=["datetime"])
 test = MergeData[~pd.notnull(MergeData['count'])].sort_values(by=["datetime"])
 datetimecol = test["datetime"]
yLabels = train["count"]
yLablesRegistered = train["registered"]
yLablesCasual = train["casual"]
test = pd.read_csv('.. /input/ashrae-energy-prediction/test.csv', index_col=0, parse_dates = ['timestamp'])
 building = pd.read_csv('.. /input/ashrae-energy-prediction/building_metadata.csv', usecols=['site_id', 'building_id'] )<merge>
dataTrain = train.drop(dropFeatures,axis=1)
 dataTest = test.drop(dropFeatures,axis=1 )
test = test.merge(building, left_on = "building_id", right_on = "building_id", how = "left" )<load_from_csv>
def rmsle(y, y_,convertExp=True):
if convertExp:
y = np.exp(y),
y_ = np.exp(y_)
 log1 = np.nan_to_num(np.array([np.log(v + 1)for v in y]))
log2 = np.nan_to_num(np.array([np.log(v + 1)for v in y_]))
calc =(log1 - log2)** 2
return np.sqrt(np.mean(calc))
submission_base = pd.read_csv('.. /input/ashrae-half-and-half/submission.csv', index_col=0 )<create_dataframe>
rfModel = RandomForestRegressor(n_estimators=100)
 yLabelsLog = np.log1p(yLabels)
 rfModel.fit(dataTrain,yLabelsLog)
 preds = rfModel.predict(X= dataTrain)
 print("RMSLE Value For Random Forest: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
submission = submission_base.copy()<load_from_csv>
[max(0, x)for x in np.exp(predsTest)]
site_0 = pd.read_csv('.. /input/new-ucf-starter-kernel/submission_ucf_replaced.csv', index_col=0)
 gc.collect()<save_to_csv>
np.exp(predsTest )
site_0.to_csv('site0.csv.gz',index=False,compression='gzip', float_format='%.4f' )<define_variables>
gbm = GradientBoostingRegressor(n_estimators=4000,alpha=0.01);
yLabelsLog = np.log1p(yLabels)
 gbm.fit(dataTrain,yLabelsLog)
 preds = gbm.predict(X= dataTrain)
 print("RMSLE Value For Gradient Boost: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
<load_pretrained><EOS> 
submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(gbmpredsTest)]
})
 submission.to_csv('my_submission_19.csv', index=False)