Kaggle competition: Bike Sharing Demand
log_reg.best_estimator_<choose_model_class>
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category = DeprecationWarning)
 %matplotlib inline
rf_clf = RandomForestClassifier(max_depth=5, max_features=4, min_samples_leaf=11,
n_estimators=50, n_jobs=-1, random_state=1)
 sgd_clf = SGDClassifier(alpha=0.1, max_iter=50, n_jobs=-1, random_state=0)
 bagging_clf = BaggingClassifier(base_estimator=DecisionTreeClassifier() , max_features=5,
max_samples=40, n_estimators=20, random_state=1)
 adaboost_clf = AdaBoostClassifier(base_estimator=LogisticRegression(random_state=1),
learning_rate=0.9, n_estimators=21, random_state=1)
 log_reg_clf = LogisticRegression(C=0.1, max_iter=25, random_state=1)
<predict_on_test>
dailyData = pd.read_csv(".. /input/bike-sharing-demand/train.csv" )
voiting_clf = VotingClassifier(estimators=([('rf', rf_clf),('bagg', bagging_clf),('lr', log_reg_clf),('ab', adaboost_clf)]))
voiting_clf.fit(X_train, y_train)
 voiting_clf_predict = voiting_clf.predict(X_val)
 voiting_clf.score(X_val, y_val )<choose_model_class>
dailyData.datetime.apply(lambda x:x.split() [0] )
model = VotingClassifier(estimators=([('rf', rf_clf),('bagg', bagging_clf),('lr', log_reg_clf),('ab', adaboost_clf)]))
model.fit(X, Y)
 predict = model.predict(X_test)
<save_to_csv>
dailyData["date"] = dailyData.datetime.apply(lambda x:x.split() [0])
 dailyData["hour"] = dailyData.datetime.apply(lambda x: x.split() [1].split(":")[0])
 dailyData["weekday"] = dailyData.date.apply(lambda dateString: calendar.day_name[datetime.strptime(dateString, "%Y-%m-%d" ).weekday() ])
 dailyData["month"] = dailyData.date.apply(lambda dateString: calendar.month_name[datetime.strptime(dateString, "%Y-%m-%d" ).month])
 dailyData["season"] = dailyData.season.map({1:"Spring", 2:"Summer", 3:"Fall", 4:"winter"})
 dailyData["weather"] = dailyData.weather.map({1: " Clear + Few clouds + Partly cloudy + Partly cloudy",\
2 : " Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist ", \
3 : " Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds", \
4 :" Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog " } )
output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': predict})
 output.to_csv('my_submission_11.csv', index=False )<set_options>
categoryvariables = ["hour", "weekday", "month", "season", "weather", "holiday", "workingday"]
for var in categoryvariables:
dailyData[var] = dailyData[var].astype("category" )
%matplotlib inline
warnings.filterwarnings('ignore')
<load_from_csv>
dailyData = dailyData.drop(["datetime"], axis =1 )
train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 combine = [train_df, test_df] 
<groupby>
typesCountSerie = dailyData.dtypes.value_counts()
typeNamesColumn = list(map(lambda t: t.name , typesCountSerie.index.values)) ;
typeCountColumn = typesCountSerie.values
intialDataTypeDf = pd.DataFrame({
"variableType": typeNamesColumn,
"count": typeCountColumn
})
 groupedDataTypeDf = intialDataTypeDf.groupby(['variableType'] ).sum() [['count']].reset_index()
fig,ax = plt.subplots()
fig.set_size_inches(12, 5)
 sn.barplot(data=groupedDataTypeDf, x="variableType", y="count", ax=ax)
 ax.set(xlabel = 'variableType', ylabel = 'Count', title = "Count of the different Datatypes" )
train_df.groupby('Pclass')['Survived'].mean()<groupby>
np.sum(np.abs(dailyData["count"]-dailyData["count"].mean())<=(3*dailyData["count"].std()))
train_df.groupby('Embarked')['Survived'].mean()<groupby>
dailyDataWithoutOutliers = dailyData[np.abs(dailyData["count"]-dailyData["count"].mean())<=(3*dailyData["count"].std())]
train_df.groupby('Sex')['Survived'].mean()<sort_values>
dataTrain = pd.read_csv(".. /input/bike-sharing-demand/train.csv")
 dataTest = pd.read_csv(".. /input/bike-sharing-demand/test.csv" )
train_df.groupby('SibSp')['Survived'].mean().sort_values(ascending=False )<sort_values>
data = dataTrain.append(dataTest)
 data.reset_index(inplace = True)
 data.drop('index', inplace = True, axis = 1 )
train_df.groupby('Parch')['Survived'].mean().sort_values(ascending=False )<drop_column>
data["date"] = data.datetime.apply(lambda x : x.split() [0])
 data["hour"] = data.datetime.apply(lambda x : x.split() [1].split(":")[0] ).astype("int")
 data["year"] = data.datetime.apply(lambda x : x.split() [0].split("-")[0])
 data["weekday"] = data.date.apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).weekday())
 data["month"] = data.date.apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).month)
 dataWindspeedOriginal = data["windspeed"]
train_df.drop(columns=['Cabin', 'Ticket', 'PassengerId'], inplace=True)
 test_df.drop(columns=['Cabin', 'Ticket'], inplace=True )<feature_engineering>
wCol= ["season","weather","humidity","month","temp","year","atemp"]
dataWind0 = data[data["windspeed"] == 0]
dataWindNot0 = data[data["windspeed"] != 0]
dataWindNot0["windspeed"] = dataWindNot0["windspeed"].astype("str")
 rfModel_wind = RandomForestClassifier()
rfModel_wind.fit(dataWindNot0[wCol], dataWindNot0["windspeed"])
 Wind0Values = rfModel_wind.predict(X = dataWind0[wCol])
 dataWind0["windspeed"] = Wind0Values
data = dataWindNot0.append(dataWind0)
 data["windspeed"] = data["windspeed"].astype("float")
 data.reset_index(inplace=True)
 data.drop('index',inplace=True,axis=1)
for dataset in combine:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )<feature_engineering>
categoricalFeatureNames = ["season","holiday","workingday","weather","weekday","month","year","hour"]
numericalFeatureNames = ["temp","humidity","windspeed","atemp"]
dropFeatures = ['casual',"count","datetime","date","registered"]
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train_df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<categorify>
for var in categoricalFeatureNames:
data[var] = data[var].astype("category" )
title_mapping = {'Mrs': 1, 'Miss': 2, 'Master': 3, 'Mr': 4, 'Rare': 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping )<drop_column>
dataTrain = data[pd.notnull(data['count'])].sort_values(by=["datetime"])
 dataTest = data[~pd.notnull(data['count'])].sort_values(by = ["datetime"])
 datetimecol = dataTest["datetime"]
yLabels = dataTrain["count"]
yLabelsRegistered = dataTrain["registered"]
yLabelsCasual = dataTrain["casual"]
train_df.drop(columns=['Name'], inplace=True)
 test_df.drop(columns=['Name'], inplace=True )<data_type_conversions>
X_train, X_validate, y_train, y_validate = train_test_split(dataTrain, yLabels, test_size=0.3, random_state=42)
 dateTimeColValidate = X_validate["datetime"]
gender_mapping = {'female': 1, 'male': 0}
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map(gender_mapping ).astype(int )<groupby>
dataTrain = dataTrain.drop(dropFeatures,axis=1)
 dataTest = dataTest.drop(dropFeatures,axis=1)
 X_train = X_train.drop(dropFeatures,axis=1)
 X_validate = X_validate.drop(dropFeatures,axis=1 )
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_df.groupby('FamilySize')['Survived'].mean()<feature_engineering>
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))
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1 
<count_values>
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning )
train_df['IsAlone'].value_counts()<drop_column>
lModel = LinearRegression()
lModel.fit(X = X_train,y = np.log1p(y_train))
preds = lModel.predict(X= X_validate)
 print("RMSLE Value For Linear Regression In Validation: ",rmsle(np.exp(np.log1p(y_validate)) ,np.exp(preds),False))
train_df.drop(columns=['SibSp', 'Parch', 'FamilySize'], inplace=True)
 test_df.drop(columns=['SibSp', 'Parch', 'FamilySize'], inplace=True )<groupby>
train_df.groupby('Embarked')['Survived'].mean()<categorify>
ridge_m_ = Ridge()
ridge_params_ = { 'max_iter':[3000],'alpha':[0.01,0.05,0.1, 1, 2, 3, 4, 10, 30,100,200,300,400,800,900,1000]}
rmsle_scorer = metrics.make_scorer(rmsle, greater_is_better = False)
 grid_ridge_m = GridSearchCV(ridge_m_,
ridge_params_,
scoring = rmsle_scorer,
cv=5)
 grid_ridge_m.fit(X = X_train, y = np.log1p(y_train))
preds = grid_ridge_m.predict(X = X_validate)
 print(grid_ridge_m.best_params_)
 print("RMSLE Value For Ridge Regression: ",rmsle(np.exp(np.log1p(y_validate)) ,np.exp(preds),False))
fig,ax= plt.subplots()
fig.set_size_inches(20,5)
 df = pd.DataFrame(grid_ridge_m.cv_results_)
 df
df["alpha"] = df["params"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_test_score"].apply(lambda x:-x)
 sn.pointplot(data=df,x="alpha",y="rmsle",ax=ax )
port_mapping = {'S': 0, 'Q': 1, 'C': 2}
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map(port_mapping)
 train_df.info()<count_values>
lasso_m_ = Lasso()
alpha = [0.001,0.005,0.01,0.3,0.1,0.3,0.5,0.7,1]
lasso_params_ = { 'max_iter':[3000],'alpha':alpha}
grid_lasso_m = GridSearchCV(lasso_m_, lasso_params_, scoring = rmsle_scorer, cv = 5)
 grid_lasso_m.fit(X = X_train,y = np.log1p(y_train))
preds = grid_lasso_m.predict(X= X_validate)
 print(grid_lasso_m.best_params_)
 print("RMSLE Value: ",rmsle(np.exp(np.log1p(y_validate)) ,np.exp(preds), False))
fig,ax= plt.subplots()
fig.set_size_inches(20,5)
 df = pd.DataFrame(grid_lasso_m.cv_results_)
 df["alpha"] = df["params"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_test_score"].apply(lambda x:-x)
 sn.pointplot(data=df,x="alpha",y="rmsle",ax=ax )
train_df['Embarked'].value_counts()<data_type_conversions>
rfModel = RandomForestRegressor(n_estimators=100)
 rfModel.fit(X = X_train,y = np.log1p(y_train))
preds = rfModel.predict(X= X_validate)
 print("RMSLE Value: ",rmsle(np.exp(np.log1p(y_validate)) ,np.exp(preds), False))
train_df['Embarked'].fillna(value=0, inplace=True )<feature_engineering>
submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest)]
})
 submission.to_csv('bike_predictions_gbm_separate_without_fe.csv', index=False )