Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<create_dataframe>
plt.style.use('bmh')
 %matplotlib inline
finalleaderboard = {
"Random Forest": forest,
"logistic regression": logistic,
"XGB": XGB,
"KNeighborsClassifier": knn,
"SVC": SVC,
"Linear SVC": LSVC
}
finalleaderboard = pd.DataFrame.from_dict(finalleaderboard, orient='index', columns=['Accuracy'])
 print(finalleaderboard )<save_to_csv>
train_df=pd.read_csv('.. /input/bike-sharing-demand/train.csv')
 test_df=pd.read_csv('.. /input/bike-sharing-demand/test.csv')
 display(train_df.head() )
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y_pred_sub})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<count_values>
train_df.duplicated().sum()
print(np.count_nonzero(y_pred)) 
<load_from_csv>
print(train_df.isnull().sum())
 print(test_df.isnull().sum() )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
def add_day_month_year(df):
df['datetime'] = pd.to_datetime(df['datetime'],
format = '%Y-%m-%d %H:%M:%S',
errors = 'coerce')
 df['Year'] = df['datetime'].dt.year
df['Month'] = df['datetime'].dt.month
df['Day'] = df['datetime'].dt.day
df['Hour'] = df['datetime'].dt.hour
df['Minute'] = df['datetime'].dt.minute
df['second'] = df['datetime'].dt.second
df['weekday'] = df['datetime'].dt.dayofweek
df['weekEnd'] = df['weekday'] * df['weekday'] >= 5
return df
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<define_variables>
train_df = add_day_month_year(train_df)
 test_df = add_day_month_year(test_df )
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )<define_variables>
train_df.drop(columns = ['second','Minute'],inplace = True)
 test_df.drop(columns = ['second','Minute'],inplace = True )
men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )<feature_engineering>
train_df = train_df.set_index('datetime')
 test_df = test_df.set_index('datetime')
 test_df_ID = test_df.index
train_df.head()
train_data["Age"] = train_data["Age"].fillna(train_data["Age"].median())
 train_data["Embarked"] = train_data["Embarked"].fillna('S')
 train_data.loc[train_data["Embarked"] == "S", "Embarked"] = 0
train_data.loc[train_data["Embarked"] == "C", "Embarked"] = 1
train_data.loc[train_data["Embarked"] == "Q", "Embarked"] = 2
train_data.loc[train_data["Sex"] == "male", "Sex"] = 0
train_data.loc[train_data["Sex"] == "female", "Sex"] = 1<feature_engineering>
casual_df = train_df.drop(['registered','count'],axis = 1)
 casual_df.head()
test_data["Age"] = test_data["Age"].fillna(test_data["Age"].median())
 test_data["Fare"] = test_data["Fare"].fillna(test_data["Fare"].median())
 test_data.loc[test_data["Embarked"] == "S", "Embarked"] = 0
test_data.loc[test_data["Embarked"] == "C", "Embarked"] = 1
test_data.loc[test_data["Embarked"] == "Q", "Embarked"] = 2
test_data.loc[test_data["Sex"] == "male", "Sex"] = 0
test_data.loc[test_data["Sex"] == "female", "Sex"] = 1<categorify>
registered_df = train_df.drop(['casual','count'],axis = 1)
 registered_df.head()
y = train_data["Survived"]
features = ["Pclass", "Sex", 'Age',"SibSp", "Parch",'Embarked']
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 RF1 = RandomForestClassifier(n_estimators=700, max_depth=7, max_features=5 , n_jobs=-1)
 adaboost = AdaBoostClassifier(RF1,n_estimators=10)
 adaboost.fit(X, y )<compute_train_metric>
registered_df['rushHours'] = registered_df['Hour'].isin([8,17,18])
scores = model_selection.cross_val_score(adaboost, X, y, cv=10)
 print('The scores mean is :',scores.mean())
 print('The Best Score:',scores.max())
 predictions = adaboost.predict(X_test )<save_to_csv>
registered_df['registered']=np.log1p(registered_df['registered'])
 registered_df['windspeed']=np.log1p(registered_df['windspeed'])
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<set_options>
casual_df['casual']=np.log1p(casual_df['casual'])
 casual_df['windspeed']=np.log1p(casual_df['windspeed'])
pd.options.mode.chained_assignment = None 
<load_from_csv>
registered_df['rushHours'] = pd.factorize(registered_df['rushHours'])[0].reshape(-1, 1)
 registered_df['weekEnd'] = pd.factorize(registered_df['weekEnd'])[0].reshape(-1, 1)
 casual_df['weekEnd'] = pd.factorize(casual_df['weekEnd'])[0].reshape(-1, 1)
test_data = pd.read_csv('.. /input/titanic/test.csv')
 train_data = pd.read_csv('.. /input/titanic/train.csv')
 comb = [train_data, test_data]<data_type_conversions>
Y_registered=registered_df.registered
registered_df.drop(columns=['registered','atemp'],inplace = True)
 X_train, X_valid, y_train, y_valid = train_test_split(registered_df,Y_registered, train_size=0.8, test_size=0.2,random_state=0 )
for ds in comb:
age_avg = ds['Age'].mean()
age_std = ds['Age'].std()
age_nan = ds['Age'].isnull().sum()
age_rand_list = np.random.randint(age_avg - age_std, age_avg + age_std, size=age_nan)
 ds['Age'][np.isnan(ds['Age'])] = age_rand_list
ds['Age'] = ds['Age'].astype(int )<feature_engineering>
def get_best_model(X_train, X_valid, y_train, y_valid):
estimators=[('et',ExtraTreesRegressor()),('hgr', HistGradientBoostingRegressor())]
models=[RandomForestRegressor() ,AdaBoostRegressor() ,BaggingRegressor() ,SVR() ,LinearRegression() ,DecisionTreeRegressor() ,ExtraTreesRegressor() , HistGradientBoostingRegressor() ,VotingRegressor(estimators=estimators)]
model_names=['RandomForestRegressor','AdaBoostRegressor','BaggingRegressor','SVR','LinearRegression','DecisionTreeRegressor','ExtraTreesRegressor',' HistGradientBoostingRegressor','VotingRegressor']
rmsle=[]
d={}
for model in range(len(models)) :
clf=models[model]
clf.fit(X_train,y_train)
 print("model_name : ",model_names[model])
 print(clf.get_params())
 test_pred=clf.predict(X_valid)
 rmsle.append(np.sqrt(mean_squared_log_error(abs(test_pred),(y_valid))))
d={'Modelling Algo':model_names,'RMSLE':rmsle}
rmsle_frame=pd.DataFrame(d)
 print(f'{rmsle_frame}
______________________________________________________________________________________')
 sns.factorplot(y='Modelling Algo',x='RMSLE',data=rmsle_frame,kind='bar',size=5,aspect=2)
 plt.show()
emb_mode = comb[0]['Embarked'].mode().at[0]
comb[0]['Embarked'] = comb[0]['Embarked'].fillna(emb_mode )<feature_engineering>
get_best_model(X_train, X_valid, y_train, y_valid )
fare = float(comb[1]['Fare'].mode())
 comb[1]['Fare'] = comb[1]['Fare'].fillna(fare )<data_type_conversions>
def rmsle(y_pred,y_true):
y_pred = np.expm1(y_pred)
 y_true = np.expm1(y_true)
 log1=np.log(y_pred + 1)
 log2=np.log(y_true + 1)
 se =(log1 - log2)** 2
mse=np.mean(se)
 return np.sqrt(mse)
 myScorer = make_scorer(rmsle, greater_is_better=False )
for ds in comb:
ds['Cabin_Null'] = ds['Cabin'].isnull().astype(int)
 pd.crosstab(comb[0]['Cabin_Null'], comb[0]['Survived'] )<sort_values>
HistGradientAlgo_registered = HistGradientBoostingRegressor()
param = {
'max_iter':[100],
'max_depth' : [10],
'max_leaf_nodes':[15],
}
gridSearch_HistGradientAlgo_registered=GridSearchCV(HistGradientAlgo_registered,param,scoring=myScorer,cv=10,verbose=3)
 gridSearch_HistGradientAlgo_registered.fit(X_train, y_train)
 best_HistGradientAlgo_registered=gridSearch_HistGradientAlgo_registered.best_estimator_
bestHistGradientAlgo_testScore_registered=best_HistGradientAlgo_registered.score(X_train, y_train )
comb[0][['Cabin_Null', 'Survived']].groupby(['Cabin_Null'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
pred_registered = best_HistGradientAlgo_registered.predict(X_valid)
 print(rmsle(pred_registered,y_valid))
for daset in comb:
daset['TickType'] = 0
tic_let = daset['Ticket'].str.extract(r'(^\S+)')
 daset['TickType'] = tic_let
daset['TickType'] = daset['TickType'].fillna('Other')
 comb[0]['TickType'].unique()<feature_engineering>
Y_casual=casual_df.casual
casual_df.drop(columns=['casual','atemp'],inplace = True)
 X_train, X_valid, y_train, y_valid = train_test_split(casual_df,Y_casual, train_size=0.8, test_size=0.2,random_state=0 )
for dataset in comb:
for letter in "ACFPSW":
let_regex = r"^(" + letter + ".*)"
l_tic = dataset.TickType.str.extract(let_regex ).dropna(axis=0 ).reset_index(drop=True)
 tic_l_list = list(l_tic[0].unique())
 dataset['TickType'].replace(tic_l_list, letter, inplace=True)
 pd.crosstab(comb[0].TickType, comb[0].Survived )<sort_values>
get_best_model(X_train, X_valid, y_train, y_valid )
for ds in comb:
ds['TickType'] = ds['TickType'].replace('F', 'Other')
 comb[0][['TickType', 'Survived']].groupby(['TickType'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
HistGradientAlgo_casual = HistGradientBoostingRegressor()
param = {
'max_iter':[i for i in range(115,118)],
'max_depth' : [i for i in range(13,18)],
'max_leaf_nodes':[25]
}
gridSearch_HistGradientAlgo_casual=GridSearchCV(HistGradientAlgo_casual,param,scoring=myScorer,cv=5,verbose=3)
 gridSearch_HistGradientAlgo_casual.fit(X_train, y_train)
 best_HistGradientAlgo_casual=gridSearch_HistGradientAlgo_casual.best_estimator_
bestHistGradientAlgo_testScore_casual=best_HistGradientAlgo_casual.score(X_train, y_train )
for ds in comb:
ds['RelatAboard'] = ds['SibSp'] + ds['Parch']
comb[0][['RelatAboard', 'Survived']].groupby(['RelatAboard'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
pred_casual = best_HistGradientAlgo_casual.predict(X_valid)
 print(rmsle(pred_casual,y_valid))
for ds in comb:
ds['OTTmembers'] = 2
ds.loc[ds['RelatAboard'] == 0, 'OTTmembers'] = 0
ds.loc[(ds['RelatAboard'] == 1)|(ds['RelatAboard'] == 2), 'OTTmembers'] = 1
ds.loc[ds['RelatAboard'] == 3, 'OTTmembers'] = 3
comb[0][['OTTmembers', 'Survived']].groupby(['OTTmembers'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
test_df['windspeed']=np.log1p(test_df['windspeed'])
 test_df['rushHours'] = test_df['Hour'].isin([8,17,18])
 test_df['rushHours'] = pd.factorize(test_df['rushHours'])[0].reshape(-1, 1)
 test_df['weekEnd'] = pd.factorize(test_df['weekEnd'])[0].reshape(-1, 1)
 test_df.drop(columns=['atemp'],inplace = True)
 pred_casual = np.round(np.expm1(best_HistGradientAlgo_casual.predict(test_df.drop('rushHours',axis = 1)))).astype(int)
 pred_registered = np.round(np.expm1(best_HistGradientAlgo_registered.predict(test_df)) ).astype(int)
for ds in comb:
ds['Rank'] = 0
rank = ds['Name'].str.extract(r'(\w+\.) ')
 ds['Rank'] = rank
ranks = set(comb[0]['Rank'].values)
 print(ranks )<sort_values>
pred_registered = pd.DataFrame(pred_registered,columns = ['count'])
 pred_registered
ot_list = ['Capt.', 'Col.', 'Countess.', 'Don.', 'Dr.', 'Jonkheer.', 'Rev.', 'Sir.', 'Major.']
miss_list = ['Lady.', 'Mlle.', 'Mme.', 'Ms.']
for dataset in comb:
dataset['Rank'].replace(ot_list, 'Other', inplace=True)
 dataset['Rank'].replace(miss_list, 'Miss.', inplace=True)
 comb[0][['Rank', 'Survived']].groupby(['Rank'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
pred_casual = pd.DataFrame(pred_casual,columns = ['count'])
 pred_casual
comb[0]['AgeGroup'] = pd.cut(comb[0]['Age'], 5)
 comb[0][['AgeGroup', 'Survived']].groupby(['AgeGroup'], as_index=False ).mean().sort_values(by='AgeGroup', ascending=False )<feature_engineering>
predictions = pd.DataFrame({'datetime':test_df_ID})
 predictions['count'] = pred_registered['count'] + pred_casual['count']
for ds in comb:
ds.loc[ds['Age'] < 16, 'Age'] = 0
ds.loc[(ds['Age'] >= 16)&(ds['Age'] < 48), 'Age'] = 1
ds.loc[(ds['Age'] >= 48)&(ds['Age'] < 64), 'Age'] = 2
ds.loc[ds['Age'] >= 64, 'Age'] = 3
ds['Age'] = ds['Age'].astype(int)
 comb[0][['Age', 'Survived']].groupby(['Age'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
predictions.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )