Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<split>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor
import xgboost as xgb
from sklearn.model_selection import train_test_split,cross_validate,cross_val_score,cross_val_predict
from sklearn.model_selection import GridSearchCV,RandomizedSearchCV
from sklearn.metrics import mean_squared_log_error
from sklearn.preprocessing import MinMaxScaler,StandardScaler
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=valid_size, random_state=seed)
 print('X_train shape = {}'.format(X_train.shape))
print('Y_train shape = {}'.format(y_train.shape))
print('X_valid shape = {}'.format(X_valid.shape))
print('Y_valid shape = {}'.format(y_valid.shape))<choose_model_class>
df1=pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv")
 df2=pd.read_csv("/kaggle/input/bike-sharing-demand/test.csv")
 def preprocess(df):
df["hour"]=pd.to_datetime(df["datetime"] ).dt.hour
df["dayofweek"]=pd.to_datetime(df["datetime"] ).dt.dayofweek
df["dayofmonth"]=pd.to_datetime(df["datetime"] ).dt.day
df["month"]=pd.to_datetime(df["datetime"] ).dt.month
df["year"]=pd.to_datetime(df["datetime"] ).dt.year.map({2011:0, 2012:1})
 df=pd.get_dummies(df,columns=["weather","season"],prefix=["weather","season"],drop_first=True,dtype=int)
 return df.drop(["datetime"],axis=1)
 df1=preprocess(df1)
 df2=preprocess(df2 )
def build_model(optimizer):
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='Same', activation='relu', input_shape=(28,28,1)))
 model.add(Conv2D(filters=32, kernel_size=(5,5), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
 return model<load_pretrained>
features=[x for x in df1.columns if x not in ["count","casual","registered"]]
label1=["casual"]
label2=["registered"]
label=["count"]
X_train,y1_train,y2_train,y_train=df1[features],df1[label1],df1[label2],df1[label]
X_test=df2[features]
X_train_0,X_train_1,y_train_0,y_train_1=train_test_split(X_train,y_train,test_size=0.25,random_state=42 )
def save_trained_model(model, save_path, optimizer):
model_json = model.to_json()
with open('{}Kennada MNIST with {}.json'.format(save_path, optimizer), "w")as json_file:
json_file.write(model_json)
 model.save_weights('{}Kennada MNIST with {}.h5'.format(save_path, optimizer))
def load_trained_model(optimizers, optimizer, load_path):
json_file = open('{}Kennada MNIST with {}.json'.format(load_path, optimizers[optimizer]), 'r')
 loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
 model.load_weights('{}Kennada MNIST with {}.h5'.format(load_path, optimizers[optimizer]))
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
 return model<save_to_csv>
rf=RandomForestRegressor(n_estimators=500,n_jobs=-1)
 rf.fit(X_train_0,y_train_0)
 pred=rf.predict(X_train_1)
 print(mean_squared_log_error(y_train_1,pred)**0.5 )
def load_history(load_path, optimizer):
history = pd.read_csv('{}Kennada MNIST with {}.csv'.format(load_path, optimizer))
return history.to_dict('list')
 def save_history(history, save_path, optimizer):
hist_df = pd.DataFrame(history)
 hist_df.to_csv('{}Kennada MNIST with {}.csv'.format(save_path, optimizer), index=False )<define_variables>
rf.fit(X_train_0,y_train_0.apply(lambda x:np.log1p(x)))
 pred=np.expm1(rf.predict(X_train_1))
print(mean_squared_log_error(y_train_1,pred)**0.5 )
batch_size = 1024
verbose = 0
epochs = 30<init_hyperparams>
print(pd.DataFrame({"Features": features,"Importance" : rf.feature_importances_.round(2)} ).sort_values("Importance",ascending=False))
optimizers = {
'sgd': 'SGD',
'rmsprop': 'RMSprop',
'adagrad': 'Adagrad',
'adadelta': 'Adadelta',
'adam': 'Adam',
'adamax': 'Adamax',
'nadam': 'Nadam',
}
learning_rates = {
'sgd': 1e-2,
'rmsprop': 1e-3,
'adagrad': 1e-2,
'adadelta': 1.0,
'adam': 1e-3,
'adamax': 2e-3,
'nadam': 2e-3,
}<choose_model_class>
DM_0=xgb.DMatrix(X_train_0,y_train_0)
 DM_1=xgb.DMatrix(X_train_1,y_train_1)
 params={"booster":"gbtree"}
xgbr=xgb.train(dtrain=DM_0,num_boost_round=380,params=params)
 pred = xgbr.predict(DM_1)
 pred[pred<0]=0
print(mean_squared_log_error(y_train_1,pred)**0.5 )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=0,
factor=0.5,
min_lr=0.00001)
 train_datagen = ImageDataGenerator(rescale=1.0,
rotation_range=10,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.1,
zoom_range=0.25,
horizontal_flip=False)
 valid_datagen = ImageDataGenerator(rescale=1.0 )<train_model>
DM_0=xgb.DMatrix(X_train_0,y_train_0.apply(lambda x:np.log1p(x)))
 params={"booster":"gbtree"}
xgbr=xgb.train(dtrain=DM_0,num_boost_round=380,params=params)
 pred = np.expm1(xgbr.predict(DM_1))
pred[pred<0]=0
print(mean_squared_log_error(y_train_1,pred)**0.5 )
history = {}
model = {}
for n, optimizer in enumerate(optimizers):
model[optimizer] = build_model(optimizer)
 start = time.time()
if to_augment:
h = model[optimizer].fit_generator(train_datagen.flow(X_train, y_train, batch_size=batch_size),
steps_per_epoch=100,
epochs=epochs,
validation_data=valid_datagen.flow(X_valid, y_valid),
callbacks=[learning_rate_reduction],
verbose=verbose)
 else:
h = model[optimizer].fit(X_train,
y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(X_valid,y_valid),
callbacks=[learning_rate_reduction],
verbose=verbose)
 history[optimizer] = h.history
print("{0} Optimizer: ".format(optimizers[optimizer]))
print("Epochs={0:d}, Train accuracy={1:.5f}, Validation accuracy={2:.5f}, Training time={3:.2f} minutes" 
.format(epochs,
max(history[optimizer]['accuracy']),
max(history[optimizer]['val_accuracy']), 
(time.time() -start)/60))<find_best_params>
params={"booster":"gbtree","learning_rate":0.09,"n_jobs":-1,"subsample":0.7,"alpha": 0.009}
xgbr=xgb.train(dtrain=DM_0,num_boost_round=380,params=params)
 pred = np.expm1(xgbr.predict(DM_1))
print(mean_squared_log_error(y_train_1,pred)**0.5 )