Kaggle competition: Bike Sharing Demand
train_df['Embarked'] = train_df['Embarked'].apply(lambda x: int(x))<data_type_conversions>
train = pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv", parse_dates=["datetime"])
 test = pd.read_csv("/kaggle/input/bike-sharing-demand/test.csv", parse_dates=["datetime"] )
test_df['Fare'].fillna(value=test_df['Fare'].median() , inplace=True )<define_search_space>
train["datetime-year"] = train["datetime"].dt.year
train["datetime-month"] = train["datetime"].dt.month
train["datetime-day"] = train["datetime"].dt.day
train["datetime-hour"] = train["datetime"].dt.hour
train["datetime-minute"] = train["datetime"].dt.minute
train["datetime-second"] = train["datetime"].dt.second
train["datetime-dayofweek"] = train["datetime"].dt.dayofweek
print(train.shape)
 train[["datetime", "datetime-year", "datetime-month", "datetime-day", "datetime-hour", "datetime-minute", "datetime-second", "datetime-dayofweek"]].head()
guess_ages = np.zeros(( 2,3))
for dataset in combine:
for i in range(0, 2):
for j in range(0, 3):
guess_df = dataset[(dataset['Sex'] == i)& \ 
(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),\
'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 train_df.head()<feature_engineering>
train.loc[train["datetime-dayofweek"] == 0, "datetime-dayofweek(humanized)"] = "Monday"
train.loc[train["datetime-dayofweek"] == 1, "datetime-dayofweek(humanized)"] = "Tuesday"
train.loc[train["datetime-dayofweek"] == 2, "datetime-dayofweek(humanized)"] = "Wednesday"
train.loc[train["datetime-dayofweek"] == 3, "datetime-dayofweek(humanized)"] = "Thursday"
train.loc[train["datetime-dayofweek"] == 4, "datetime-dayofweek(humanized)"] = "Friday"
train.loc[train["datetime-dayofweek"] == 5, "datetime-dayofweek(humanized)"] = "Saturday"
train.loc[train["datetime-dayofweek"] == 6, "datetime-dayofweek(humanized)"] = "Sunday"
print(train.shape)
 train[["datetime", "datetime-dayofweek", "datetime-dayofweek(humanized)"]].head()
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']
train_df.head()<drop_column>
test["datetime-year"] = test["datetime"].dt.year
test["datetime-month"] = test["datetime"].dt.month
test["datetime-day"] = test["datetime"].dt.day
test["datetime-hour"] = test["datetime"].dt.hour
test["datetime-minute"] = test["datetime"].dt.minute
test["datetime-second"] = test["datetime"].dt.second
test["datetime-dayofweek"] = test["datetime"].dt.dayofweek
print(test.shape)
 test[["datetime", "datetime-year", "datetime-month", "datetime-day", "datetime-hour", "datetime-minute", "datetime-second", "datetime-dayofweek"]].head()
train_df.drop(columns=['AgeBand'], inplace=True )<feature_engineering>
test.loc[test["datetime-dayofweek"] == 0, "datetime-dayofweek(humanized)"] = "Monday"
test.loc[test["datetime-dayofweek"] == 1, "datetime-dayofweek(humanized)"] = "Tuesday"
test.loc[test["datetime-dayofweek"] == 2, "datetime-dayofweek(humanized)"] = "Wednesday"
test.loc[test["datetime-dayofweek"] == 3, "datetime-dayofweek(humanized)"] = "Thursday"
test.loc[test["datetime-dayofweek"] == 4, "datetime-dayofweek(humanized)"] = "Friday"
test.loc[test["datetime-dayofweek"] == 5, "datetime-dayofweek(humanized)"] = "Saturday"
test.loc[test["datetime-dayofweek"] == 6, "datetime-dayofweek(humanized)"] = "Sunday"
print(test.shape)
 test[["datetime", "datetime-dayofweek", "datetime-dayofweek(humanized)"]].head()
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31.0), 'Fare'] = 2
dataset.loc[(dataset['Fare'] > 31.0)&(dataset['Fare'] <= 512.329), 'Fare'] = 3
train_df.head()<drop_column>
%matplotlib inline
train_df.drop(columns=['FareBand'], inplace=True )<data_type_conversions>
train["datetime-year(str)"] = train["datetime-year"].astype('str')
 train["datetime-month(str)"] = train["datetime-month"].astype('str')
 train["datetime-year_month"] = train["datetime-year(str)"] + "-" + train["datetime-month(str)"]
print(train.shape)
 train[["datetime", "datetime-year_month"]].head()
for dataset in combine:
dataset['Fare'] = dataset['Fare'].astype(int )<feature_engineering>
feature_names = ["datetime-year", "season", "datetime-hour", "datetime-dayofweek", "workingday", "holiday",
"weather", "humidity", "temp", "atemp", "windspeed"]
feature_names
for dataset in combine:
dataset['Age*Pclass'] = dataset['Age'] * dataset['Pclass'] 
<prepare_x_and_y>
label_name = "count"
label_name
y = train_df['Survived']
X = train_df.drop('Survived', axis=1)
<split>
model = RandomForestRegressor(n_jobs=-1,
random_state=37)
 model
X_train, X_val, y_train, y_val = train_test_split(X, y )<train_model>
def rmsle(predict, actual):
predict = np.array(predict)
 actual = np.array(actual)
 log_predict = np.log(predict + 1)
 log_actual = np.log(actual + 1)
 distance = log_predict - log_actual
square_distance = distance ** 2
mean_square_distance = square_distance.mean()
score = np.sqrt(mean_square_distance)
 return score
rmsle_score = make_scorer(rmsle)
 rmsle_score
linreg = LogisticRegression(random_state=1)
 linreg.fit(X_train, y_train)
<compute_test_metric>
score = cross_val_score(model, X_train, y_train,
cv=20, scoring=rmsle_score ).mean()
print("Score = {0:.5f}".format(score))
predictions = linreg.predict(X_val)
 accuracy_score(y_val, predictions)
 round(linreg.score(X_train, y_train), 3), round(linreg.score(X_val, y_val), 3 )<drop_column>
model.fit(X_train, y_train )
X_test = test_df.drop(columns=['PassengerId'] )<split>
model.fit(X_train, y_train )
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)
 D_train = xgb.DMatrix(X_train, label=y_train)
 D_test = xgb.DMatrix(X_val, label=y_val)
 clf = xgb.XGBClassifier()
parameters = {
"eta" : [0.05, 0.10, 0.16, 0.13, 0.14, 0.15,] ,
"max_depth" : [ 3, 4, 5, 6, 8, 10, 12, 15],
"min_child_weight" : [ 3, 5, 7],
"gamma" : [ 0.05, 0.1, 0.15 , 0.2, 0.5],
"colsample_bytree" : [ 0.8, 0.9, 0.6 , 0.7 ],
}
grid = GridSearchCV(clf, parameters, n_jobs=-1, cv=3)
 grid.fit(X_train, y_train)
<find_best_params>
predictions = model.predict(X_test)
 print(predictions.shape)
 predictions
grid.best_params_<init_hyperparams>
submission = pd.read_csv("/kaggle/input/bike-sharing-demand/sampleSubmission.csv")
 print(submission.shape)
 submission.head()
param_3 = {'colsample_bytree': 0.7,
'eta': 0.14,
'gamma': 0.15,
'max_depth': 6,
'min_child_weight': 5}<train_model>
submission["count"] = predictions
print(submission.shape)
 submission.head()
D_train = xgb.DMatrix(X_train, label=y_train)
 D_test = xgb.DMatrix(X_val, label=y_val)
 param = {
'eta': 0.1,
'max_depth': 3,
'objective': 'multi:softprob',
'num_class': 3}
steps = 20
model = xgb.train(param, D_train, steps)
 preds = model.predict(D_test)
 best_preds = np.asarray([np.argmax(line)for line in preds])
 print("Precision = {}".format(precision_score(y_val, best_preds, average='macro')))
 print("Recall = {}".format(recall_score(y_val, best_preds, average='macro')))
 print("Accuracy = {}".format(accuracy_score(y_val, best_preds)))
<train_model>
submission.to_csv("submission.csv", index=False )
D_test = xgb.DMatrix(X_test)
 param = {
'eta': 0.1,
'max_depth': 3,
'objective': 'multi:softprob',
'num_class': 3}
steps = 20
model = xgb.train(param, D_train, steps)
 preds = model.predict(D_test )<define_variables>
submission.to_csv("submission.csv", index=False )