Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<load_from_csv>
warnings.filterwarnings('ignore')
 sns.set(style='whitegrid' , palette='tab10')
 %matplotlib inline
holy_grail = pd.read_csv(".. /input/holygrail/submission.csv")
 y_test = holy_grail["target"]<compute_test_metric>
train_df = pd.read_csv('.. /input/train.csv',header = 0)
 test_df = pd.read_csv('.. /input/test.csv',header=0 )
def scores(y_test, y_predicted):
accuracy = accuracy_score(y_test, y_predicted)
 f1 = f1_score(y_test, y_predicted, pos_label = None, average = 'weighted')
 precision = precision_score(y_test, y_predicted, pos_label = None, average = 'weighted')
 recall = recall_score(y_test, y_predicted, pos_label = None, average = 'weighted')
 print("accuracy = %.4f, f1 = %.4f, precision = %.4f, recall = %.4f" %(accuracy, f1, precision, recall))
return accuracy, f1, precision, recall<compute_test_metric>
Bike_data=pd.concat([train_WithoutOutliers,test_df],ignore_index=True)
 Bike_data.shape
accuracy, f1, precision, recall = scores(y_test, y_predicted )<predict_on_test>
Bike_data['date']=Bike_data.datetime.apply(lambda c : c.split() [0])
 Bike_data['hour']=Bike_data.datetime.apply(lambda c : c.split() [1].split(':')[0] ).astype('int')
 Bike_data['year']=Bike_data.datetime.apply(lambda c : c.split() [0].split('-')[0] ).astype('int')
 Bike_data['month']=Bike_data.datetime.apply(lambda c : c.split() [0].split('-')[1] ).astype('int')
 Bike_data['weekday']=Bike_data.date.apply(lambda c : datetime.strptime(c,'%Y-%m-%d' ).isoweekday())
 Bike_data.head()
probabilities_SVM = classifier_SVM.predict_proba(mapped_test)
 probabilities_LogisticRegression = classifier_LogisticRegression.predict_proba(mapped_test)
 probability_of_ones_SVM = probabilities_SVM[:,1]
probability_of_ones_LogisticRegression = probabilities_LogisticRegression[:,1]
FPrates_SVM, TPrates_SVM, thresholds_SVM = roc_curve(y_test, probability_of_ones_SVM)
 FPrates_LogisticRegression, TPrates_LogisticRegression, thresholds_LogisticRegression = roc_curve(y_test, probability_of_ones_LogisticRegression)
 sns.set_style('whitegrid')
 plt.figure(figsize =(10, 8))
plt.plot(FPrates_SVM, TPrates_SVM, label = 'SVM')
 plt.plot(FPrates_LogisticRegression, TPrates_LogisticRegression, label = 'Logistic Regression')
 plt.plot([0, 1], [0, 1], color = 'blue', linestyle = '--')
 plt.axis([0, 1, 0, 1])
 plt.xlabel('False Positive Rate', fontsize = 14)
 plt.ylabel('True Positive Rate', fontsize = 14)
 plt.title('ROC Curve', fontsize = 14)
 plt.legend(loc = "lower right")
 plt.show()<load_from_csv>
Bike_data["windspeed_rfr"]=Bike_data["windspeed"]
dataWind0 = Bike_data[Bike_data["windspeed_rfr"]==0]
dataWindNot0 = Bike_data[Bike_data["windspeed_rfr"]!=0]
rfModel_wind = RandomForestRegressor(n_estimators=1000,random_state=42)
 windColumns = ["season","weather","humidity","month","temp","year","atemp"]
rfModel_wind.fit(dataWindNot0[windColumns], dataWindNot0["windspeed_rfr"])
 wind0Values = rfModel_wind.predict(X= dataWind0[windColumns])
 dataWind0.loc[:,"windspeed_rfr"] = wind0Values
Bike_data = dataWindNot0.append(dataWind0)
 Bike_data.reset_index(inplace=True)
 Bike_data.drop('index',inplace=True,axis=1 )
train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/test.csv")
 submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/submission.csv")
 train.head() 
<define_variables>
workingday_df=Bike_data[Bike_data['workingday']==1]
workingday_df = workingday_df.groupby(['hour'], as_index=True ).agg({'casual':'mean',
'registered':'mean',
'count':'mean'})
 nworkingday_df=Bike_data[Bike_data['workingday']==0]
nworkingday_df = nworkingday_df.groupby(['hour'], as_index=True ).agg({'casual':'mean',
'registered':'mean',
'count':'mean'})
 fig, axes = plt.subplots(1, 2,sharey = True)
 workingday_df.plot(figsize=(15,5),title = 'The average number of rentals initiated per hour in the working day',ax=axes[0])
 nworkingday_df.plot(figsize=(15,5),title = 'The average number of rentals initiated per hour in the nonworkdays',ax=axes[1] )
print(f'Total reported cases are {len(train)}.')
 print(f'Total confirmed cases are {int(train["ConfirmedCases"].sum())}.')
 print(f'Total fatality cases are {int(train["Fatalities"].sum())}.')
 print(f'Total countries are {len(train["Country/Region"].unique())}.' )<data_type_conversions>
temp_df = Bike_data.groupby(['date','weekday'], as_index=False ).agg({'year':'mean',
'month':'mean',
'temp':'median'})
 temp_df.dropna(axis = 0 , how ='any', inplace = True)
 temp_month = temp_df.groupby(['year','month'], as_index=False ).agg({'weekday':'min',
'temp':'median'})
 temp_df['date']=pd.to_datetime(temp_df['date'])
 temp_month.rename(columns={'weekday':'day'},inplace=True)
 temp_month['date']=pd.to_datetime(temp_month[['year','month','day']])
 fig = plt.figure(figsize=(18,6))
ax = fig.add_subplot(1,1,1)
 plt.plot(temp_df['date'] , temp_df['temp'] , linewidth=1.3 , label='Daily average')
 ax.set_title('Change trend of average temperature per day in two years')
 plt.plot(temp_month['date'] , temp_month['temp'] , marker='o', linewidth=1.3 ,
label='Monthly average')
 ax.legend()
train["Date"] = train["Date"].apply(lambda x: x.replace("-",""))
train["Date"] = train["Date"].astype(int)
 train.head()<count_missing_values>
humidity_df = Bike_data.groupby('date', as_index=False ).agg({'humidity':'mean'})
 humidity_df['date']=pd.to_datetime(humidity_df['date'])
 humidity_df=humidity_df.set_index('date')
 humidity_month = Bike_data.groupby(['year','month'], as_index=False ).agg({'weekday':'min',
'humidity':'mean'})
 humidity_month.rename(columns={'weekday':'day'},inplace=True)
 humidity_month['date']=pd.to_datetime(humidity_month[['year','month','day']])
 fig = plt.figure(figsize=(18,6))
ax = fig.add_subplot(1,1,1)
 plt.plot(humidity_df.index , humidity_df['humidity'] , linewidth=1.3,label='Daily average')
 plt.plot(humidity_month['date'], humidity_month['humidity'] ,marker='o',
linewidth=1.3,label='Monthly average')
 ax.legend()
ax.set_title('Change trend of average humidity per day in two years' )
train = train.drop(['Province/State'],axis=1)
 train = train.dropna()
train.isnull().sum()<data_type_conversions>
count_df = Bike_data.groupby(['date','weekday'], as_index=False ).agg({'year':'mean',
'month':'mean',
'casual':'sum',
'registered':'sum',
'count':'sum'})
 count_df.dropna(axis = 0 , how ='any', inplace = True)
 count_month = count_df.groupby(['year','month'], as_index=False ).agg({'weekday':'min',
'casual':'mean',
'registered':'mean',
'count':'mean'})
 count_df['date']=pd.to_datetime(count_df['date'])
 count_month.rename(columns={'weekday':'day'},inplace=True)
 count_month['date']=pd.to_datetime(count_month[['year','month','day']])
 fig = plt.figure(figsize=(18,6))
ax = fig.add_subplot(1,1,1)
 plt.plot(count_df['date'] , count_df['count'] , linewidth=1.3 , label='Daily average')
 ax.set_title('Change trend of average number of rentals initiated per day in two years')
 plt.plot(count_month['date'] , count_month['count'] , marker='o',
linewidth=1.3 , label='Monthly average')
 ax.legend()
test["Date"] = test["Date"].apply(lambda x: x.replace("-",""))
test["Date"] = test["Date"].astype(int)
 test.isnull().sum() 
<prepare_x_and_y>
day_df=Bike_data.groupby('date' ).agg({'year':'mean','season':'mean',
'casual':'sum', 'registered':'sum'
,'count':'sum','temp':'mean',
'atemp':'mean'})
 season_df = day_df.groupby(['year','season'], as_index=True ).agg({'casual':'mean',
'registered':'mean',
'count':'mean'})
 temp_df = day_df.groupby(['year','season'], as_index=True ).agg({'temp':'mean',
'atemp':'mean'} )
x = train[['Lat', 'Long', 'Date']]
y1 = train[['ConfirmedCases']]
y2 = train[['Fatalities']]
x_test = test[['Lat', 'Long', 'Date']]<choose_model_class>
count_weather = Bike_data.groupby('weather')
 count_weather[['casual','registered','count']].count()
Tree_model = RandomForestClassifier(max_depth=200, random_state=0 )<predict_on_test>
Bike_data[Bike_data['weather']==4]
Tree_model.fit(x,y1)
 pred1 = Tree_model.predict(x_test)
 pred1 = pd.DataFrame(pred1)
 pred1.columns = ["ConfirmedCases_prediction"]<predict_on_test>
windspeed_df = Bike_data.groupby('date', as_index=False ).agg({'windspeed_rfr':'mean'})
 windspeed_df['date']=pd.to_datetime(windspeed_df['date'])
 windspeed_df=windspeed_df.set_index('date')
 windspeed_month = Bike_data.groupby(['year','month'], as_index=False ).agg({'weekday':'min',
'windspeed_rfr':'mean'})
 windspeed_month.rename(columns={'weekday':'day'},inplace=True)
 windspeed_month['date']=pd.to_datetime(windspeed_month[['year','month','day']])
 fig = plt.figure(figsize=(18,6))
ax = fig.add_subplot(1,1,1)
 plt.plot(windspeed_df.index , windspeed_df['windspeed_rfr'] , linewidth=1.3,label='Daily average')
 plt.plot(windspeed_month['date'], windspeed_month['windspeed_rfr'] ,
marker='o', linewidth=1.3,label='Monthly average')
 ax.legend()
ax.set_title('Change trend of average number of windspeed per day in two years' )
Tree_model.fit(x,y2)
 pred2 = Tree_model.predict(x_test)
 pred2 = pd.DataFrame(pred2)
 pred2.columns = ["Death_prediction"] 
<load_from_csv>
df2=Bike_data[Bike_data['windspeed']>40]
df2=df2[df2['count']>400]
df2
Sub = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 sub_new = Sub[["ForecastId"]]
sub_new<concatenate>
day_df = Bike_data.groupby(['date'], as_index=False ).agg({'casual':'sum','registered':'sum',
'count':'sum', 'workingday':'mean',
'weekday':'mean','holiday':'mean',
'year':'mean'})
 day_df.head()
submit = pd.concat([pred1,pred2,sub_new],axis=1)
 submit.head() 
<data_type_conversions>
holiday_coun=day_df.groupby('year', as_index=True ).agg({'holiday':'sum'})
 holiday_coun
submit.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
submit = submit[['ForecastId','ConfirmedCases', 'Fatalities']]
submit["ConfirmedCases"] = submit["ConfirmedCases"].astype(int)
 submit["Fatalities"] = submit["Fatalities"].astype(int )<save_to_csv>
dummies_month = pd.get_dummies(Bike_data['month'], prefix= 'month')
 dummies_season=pd.get_dummies(Bike_data['season'],prefix='season')
 dummies_weather=pd.get_dummies(Bike_data['weather'],prefix='weather')
 dummies_year=pd.get_dummies(Bike_data['year'],prefix='year')
 Bike_data=pd.concat([Bike_data,dummies_month,dummies_season,dummies_weather,dummies_year],axis=1 )
submit.to_csv('submission.csv', index=False )<load_from_csv>
dataTrain = Bike_data[pd.notnull(Bike_data['count'])]
dataTest= Bike_data[~pd.notnull(Bike_data['count'])].sort_values(by=['datetime'])
 datetimecol = dataTest['datetime']
yLabels=dataTrain['count']
yLabels_log=np.log(yLabels)
train_data = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/train.csv")
 test_data = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/test.csv" )<feature_engineering>
dropFeatures = ['casual' , 'count' , 'datetime' , 'date' , 'registered' ,
'windspeed' , 'atemp' , 'month','season','weather', 'year' ]
dataTrain = dataTrain.drop(dropFeatures , axis=1)
 dataTest = dataTest.drop(dropFeatures , axis=1 )
for row in range(len(train_data['Province/State'])) :
if str(train_data['Province/State'][row])== 'nan':
train_data['Province/State'][row] = train_data['Country/Region'][row]
for row in range(len(test_data['Province/State'])) :
if str(test_data['Province/State'][row])== 'nan':
test_data['Province/State'][row] = test_data['Country/Region'][row]<filter>
rfModel = RandomForestRegressor(n_estimators=1000 , random_state = 42)
 rfModel.fit(dataTrain , yLabels_log)
 preds = rfModel.predict(X = dataTrain )
<define_variables><EOS> 
predsTest= rfModel.predict(X = dataTest)
 submission=pd.DataFrame({'datetime':datetimecol , 'count':[max(0,x)for x in np.exp(predsTest)]})
 submission.to_csv('bicycle_predictions.csv',index=False )