Kaggle competition: Bike Sharing Demand
train = train.drop(['Fare'], axis=1)
 test = test.drop(['Fare'], axis=1)
 all_data = [train,test]
train.head()<feature_engineering>
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.feature_selection import VarianceThreshold
from sklearn.linear_model import ElasticNet
import matplotlib.pyplot as plt
from sklearn.preprocessing import QuantileTransformer, quantile_transform
from sklearn.compose import TransformedTargetRegressor
from sklearn.linear_model import RidgeCV
from sklearn.compose import ColumnTransformer
import xgboost
from sklearn.metrics import mean_squared_log_error
for dataset in all_data:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train['Title'], train['Sex'])
<feature_engineering>
def trans(x,l1=0.3,l2=0):
if l1!=0:
return(( x+l2)**l1-1)/l1
else:
return np.log(x+l2)
 def rev_trans(x,l1=0.3,l2=0):
return(x*l1+1)**(1/l1)-l2
for dataset in all_data:
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[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean() 
<categorify>
df = pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv" )
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in all_data:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train.head() 
<drop_column>
df['windspeed'] = np.where(df['windspeed']==0.0,8.9981,df['windspeed'] )
train = train.drop(['Name', 'PassengerId'], axis=1)
 test = test.drop(['Name'], axis=1)
 all_data = [train, test] 
<prepare_x_and_y>
df['humidity'] = np.where(df['humidity']==0.0,8,df['humidity'] )
X_train = train.drop("Survived", axis=1)
 Y_train = train["Survived"]
X_test = test.drop("PassengerId", axis=1 ).copy() 
<train_model>
df.drop(columns='temp',inplace = True )
random_forest= RandomForestClassifier(n_estimators=100,
max_features='auto',
criterion='entropy',
max_depth=10)
 random_forest.fit(X_train, Y_train)
 Y_prediction = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 print(round(acc_random_forest,2,), "%")
<predict_on_test>
df['windspeed'] = np.log(df['windspeed'] )
clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
max_depth=1, random_state=0 ).fit(X_train, Y_train)
 y_prediction= clf.predict(X_test)
 clf.score(X_train, Y_train)
 acc_clf = round(clf.score(X_train, Y_train)* 100, 2)
 print(round(acc_clf,2,), "%")
<train_model>
df['weather'].value_counts()
model = LGBMClassifier().fit(X_train, Y_train)
 y_predict= model.predict(X_test)
 model.score(X_train, Y_train)
 acc_model = round(model.score(X_train, Y_train)* 100, 2)
 print(round(acc_model,2,), "%")
<compute_train_metric>
df['weather'] = np.where(df['weather']==4,3,df['weather'] )
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred = logreg.predict(X_test)
 acc_log = round(logreg.score(X_train, Y_train)* 100, 2)
 print(round(acc_log,2,), "%")
<compute_train_metric>
df['weather'].value_counts()
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 Y_pred = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, Y_train)* 100, 2)
 print(round(acc_decision_tree,2,), "%")
<train_model>
df['datetime'] = pd.to_datetime(df['datetime'] )
params_xgb = {'colsample_bylevel': 0.7, 'learning_rate': 0.03, 'max_depth': 3,
'n_estimators': 400, 'reg_lambda': 15, 'subsample': 0.5}
xgb = XGBClassifier(**params_xgb)
 y_preds = xgb.fit(X_train, Y_train ).predict(X_test)
 acc_xgb = round(xgb.score(X_train, Y_train)* 100, 2)
 print(round(acc_xgb,2,), "%")
<prepare_output>
df['Hour'] = df['datetime'].dt.hour
df['DayOfWeek'] = df['datetime'].dt.dayofweek
df['Year'] = df['datetime'].dt.year
df['WeekOfYear'] = df['datetime'].dt.isocalendar().week
df['Month'] = df['datetime'].dt.month
results = pd.DataFrame({
'Model': ['LGBMClassifier', 'Logistic Regression',
'Random Forest', 'Boosting',
'Decision Tree','xgb'],
'Score': [ acc_model,acc_log,
acc_random_forest, acc_clf,
acc_decision_tree,acc_xgb]})
 result_df = results.sort_values(by='Score', ascending=False)
 result_df = result_df.set_index('Score')
 result_df.head(7)
<compute_train_metric>
hourMonthSeason = df.pivot_table(index=['season','Month','DayOfWeek','Hour'],values = 'count',aggfunc=np.mean ).reset_index()
hourMonthSeason['y_SMH'] = hourMonthSeason['count']
rf = RandomForestClassifier(n_estimators=100)
 scores = cross_val_score(rf, X_train, Y_train, cv=10, scoring = "accuracy")
 print("Scores:", scores)
 print("Mean:", scores.mean())
 print("Standard Deviation:", scores.std())
<train_model>
df = pd.merge(df,hourMonthSeason[['season','Month','DayOfWeek','Hour','y_SMH']],on = ['season','Month','DayOfWeek','Hour'],how = 'left' )
params_xgb = {'colsample_bylevel': 0.7, 'learning_rate': 0.03, 'max_depth': 3,
'n_estimators': 400, 'reg_lambda': 15, 'subsample': 0.5}
xgb = XGBClassifier(**params_xgb)
 y_preds = xgb.fit(X_train, Y_train ).predict(X_test)
 print("Score: ",xgb.score, 4*100, "%")
<save_to_csv>
X = df.copy()
X.drop(columns = ['WeekOfYear','casual', 'count', 'registered','holiday'],inplace = True )
submission = pd.DataFrame({
"PassengerId": test['PassengerId'],
"Survived": y_preds
})
 submission.to_csv('submission.csv', index=False)
<set_options>
y_log=trans(df['count'])
 X_train, X_eval, y_train, y_eval=train_test_split(X, y_log, test_size=0.3, random_state=42 )
plt.style.use('seaborn-whitegrid' )<load_from_csv>
xgb_best = xgboost.XGBRegressor(colsample_bytree= 0.7,
learning_rate =0.03,
max_depth= 5,
min_child_weight= 0.4,
n_estimators= 250,
nthread= 4,
objective= 'reg:linear',
silent= 1,
subsample= 0.7)
 xgb_best.fit(X_train,y_train)
 pred = xgb_best.predict(X_eval)
 print(r2_score(y_eval,pred))
print(np.sqrt(mean_squared_error(y_eval,pred)))
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.info() 
<load_from_csv>
df_test = pd.read_csv("/kaggle/input/bike-sharing-demand/test.csv" )
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.info()<filter>
df_test['windspeed'] = np.where(df_test['windspeed']==0.0,8.9981,df_test['windspeed'])
 df_test['humidity'] = np.where(df_test['humidity']==0.0,8,df_test['humidity'] )
train_data[train_data['Fare'] > 500]<filter>
df_test.drop(columns='temp',inplace = True)
 df_test['windspeed'] = np.log(df_test['windspeed'])
 df_test['weather'] = np.where(df_test['weather']==4,3,df_test['weather'] )
test_data[test_data['Fare'] > 500]<categorify>
df_test['datetime'] = pd.to_datetime(df_test['datetime'])
 df_test['Hour'] = df_test['datetime'].dt.hour
df_test['DayOfWeek'] = df_test['datetime'].dt.dayofweek
df_test['Year'] = df_test['datetime'].dt.year
df_test['WeekOfYear'] = df_test['datetime'].dt.isocalendar().week
df_test['Month'] = df_test['datetime'].dt.month
train_data["Fare"].replace({ 512.3292 : 7.25}, inplace=True)
 test_data["Fare"].replace({ 512.3292 : 7.25}, inplace=True )<filter>
df_test = pd.merge(df_test,hourMonthSeason[['season','Month','DayOfWeek','Hour','y_SMH']],on = ['season','Month','DayOfWeek','Hour'],how = 'left' )
train_data = train_data[(train_data.PassengerId != 259)&(train_data.PassengerId != 680)&(train_data.PassengerId != 738)]<count_missing_values>
X_test = df_test.copy()
test_date = X_test['datetime']
X_test.drop(columns = ['WeekOfYear','datetime','holiday'],inplace = True )
train_data.isna().sum() 
<count_missing_values>
X_test = X_test[cols_list]
test_data.isna().sum()<feature_engineering>
y_test_predict=xgb_best.predict(X_test)
 y_test_final = rev_trans(y_test_predict)
 df_test['count']=y_test_final
train_data['Age'] = train_data['Age'].replace(np.nan, 29)
 test_data['Age'] = test_data['Age'].replace(np.nan, 30 )<drop_column>
df_test[['datetime','count']].to_csv('/kaggle/working/submissionNoParams.csv', index=False )