Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<count_missing_values>
%matplotlib inline
train_data.isnull()<count_missing_values>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train_data.isnull().sum()<drop_column>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train_data.drop(["Name","Cabin"], axis=1, inplace=True )<categorify>
train_withoutliers = train[np.abs(train['count']-train['count'].mean())<=(3*train['count'].std())]
train_data['Embarked'] = train_data['Embarked'].fillna(value=train_data['Embarked'].mode() [0] )<count_missing_values>
data = pd.concat([train_withoutliers,test],ignore_index=True)
 data.shape
train_data.isnull().sum()<rename_columns>
data['datetime'] = pd.to_datetime(data['datetime'],errors='coerce')
 data['year'] = data['datetime'].apply(lambda x: x.year )
train_data['Sex'] = train_data['Sex'].replace(['male', 'female'],[1,0])
 train_data.rename(columns = {'Sex' : 'gender'}, inplace = True )<rename_columns>
data['hour'] = data['datetime'].apply(lambda x: x.hour ).astype('int' )
train_data['Embarked'] = train_data['Embarked'].replace(['S','C','Q'],[0,1,2])
 train_data.rename(columns = {'Embarked' : 'port'}, inplace = True )<rename_columns>
data['weekday'] = data['datetime'].apply(lambda x: x.weekday())
 data['date'] = data['datetime'].apply(lambda x: x.date())
 data[["date","weekday"]].head()
data['month'] = data['datetime'].apply(lambda x: x.month )
train_data.rename(columns = {'Pclass' : 'passenger_cls'} )<drop_column>
workingday_df=data[data['workingday']==1]
nworkingday_df=data[data['workingday']==0]
workingday_df = workingday_df.groupby(['hour'], as_index=True ).agg({'casual':'mean',
'registered':'mean',
'count':'mean'})
 nworkingday_df = nworkingday_df.groupby(['hour'], as_index=True ).agg({'casual':'mean',
'registered':'mean',
'count':'mean'})
 workingday_df.plot(title="dailyrentals on workingday")
 nworkingday_df.plot(title="dailyrentals on nworkingday" )
train_data['family_members'] = train_data['SibSp'] + train_data['Parch']
train_data.drop(['SibSp', 'Parch'], axis = 1, inplace=True )<feature_engineering>
df1=data[data['windspeed']>40]
df2=df1[df1["count"]>170]
train_data.loc[ train_data['Age'] <= 21, 'Age'] = 0
train_data.loc[(train_data['Age'] > 21)&(train_data['Age'] <= 34), 'Age'] = 1
train_data.loc[(train_data['Age'] > 34)&(train_data['Age'] <= 54), 'Age'] = 2
train_data.loc[(train_data['Age'] > 60)&(train_data['Age'] <= 75), 'Age'] = 3
train_data.loc[ train_data['Age'] > 75, 'Age'] ;<feature_engineering>
data[data['count'].notnull() ].groupby(['season','year'])[['casual','registered']].sum()
train_data.loc[ train_data['Fare'] <= 7.5, 'Fare'] = 0
train_data.loc[(train_data['Fare'] > 15)&(train_data['Fare'] <= 21.5), 'Fare'] = 1
train_data.loc[(train_data['Fare'] > 21.5)&(train_data['Age'] <= 29), 'Fare'] = 2
train_data.loc[(train_data['Fare'] > 29)&(train_data['Fare'] <= 36.5), 'Fare'] = 3
train_data.loc[ train_data['Fare'] > 36.5, 'Fare'] ;<drop_column>
dummies_month = pd.get_dummies(data['month'], prefix= 'month')
 dummies_season = pd.get_dummies(data['season'], prefix= 'season')
 dummies_weather = pd.get_dummies(data['weather'], prefix= 'weather')
 dummies_year = pd.get_dummies(data['year'], prefix= 'year' )
train_data.drop(['Ticket'], axis = 1, inplace=True )<count_missing_values>
data=pd.concat([data,dummies_month,dummies_season,dummies_weather,dummies_year],axis=1)
test_data.isnull().sum()<drop_column>
dataTrain = data[pd.notnull(data['count'])]
dataTest= data[~pd.notnull(data['count'])].sort_values(by=['datetime'])
 datetimecol = dataTest['datetime']
yLabels = dataTrain['count']
yLabels_log = np.log(yLabels+1)
test_data.drop(['Name', 'Ticket','Cabin'], axis = 1, inplace = True )<feature_engineering>
dropFeatures = ['casual' , 'count' , 'datetime' , 'registered' , 'date' ,
'windspeed' , 'atemp' ,'season','weather','month','year']
dataTrain = dataTrain.drop(dropFeatures,axis=1)
 dataTest = dataTest.drop(dropFeatures,axis=1 )
test_data['Age'] = test_data[['Age','Pclass']].apply(cal_age,axis=1)
<categorify>
from sklearn.model_selection import train_test_split
<data_type_conversions>
from sklearn.model_selection import train_test_split
test_data['Fare'].fillna(value=test_data['Fare'].mean() , inplace=True )<count_missing_values>
X_train, X_valid, y_train, y_valid = train_test_split(dataTrain,
yLabels_log, test_size=0.1, random_state=42)
test_data.isnull().sum()<rename_columns>
from sklearn.ensemble import RandomForestRegressor
test_data['Sex'] = test_data['Sex'].replace(['male', 'female'],[1,0])
 test_data.rename(columns = {'Sex' : 'gender'}, inplace = True )<rename_columns>
from sklearn.ensemble import RandomForestRegressor
test_data['Embarked'] = test_data['Embarked'].replace(['S','C','Q'],[0,1,2])
 test_data.rename(columns = {'Embarked' : 'port'}, inplace = True )<rename_columns>
predsTest= rfModel.predict(dataTest)
 submission=pd.DataFrame({'datetime':datetimecol , 'count':[max(0,x)for x in(np.exp(predsTest)-1)]})
 submission.to_csv('bike_predictions.csv',index=False )
<drop_column><EOS> 
rf_dict={'count':rfModel.feature_importances_.round(3)}
pd.DataFrame(rf_dict,X_train.columns )