Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
train_df.drop(columns = ['second','Minute'],inplace = True)
 test_df.drop(columns = ['second','Minute'],inplace = True )<rename_columns>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 submit = pd.read_csv(".. /input/sample_submission.csv" )
train_df = train_df.set_index('datetime')
 test_df = test_df.set_index('datetime')
 test_df_ID = test_df.index
train_df.head()<drop_column>
x_train = train.iloc[:,1:]
y_train = train.iloc[:,0]
casual_df = train_df.drop(['registered','count'],axis = 1)
 casual_df.head()<drop_column>
x_train=x_train/255.0
test=test/255.0
registered_df = train_df.drop(['casual','count'],axis = 1)
 registered_df.head()<feature_engineering>
x_train = x_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )
registered_df['rushHours'] = registered_df['Hour'].isin([8,17,18])
<feature_engineering>
y_train=to_categorical(y_train,num_classes=10)
 print(x_train.shape,y_train.shape,test.shape )
registered_df['registered']=np.log1p(registered_df['registered'])
 registered_df['windspeed']=np.log1p(registered_df['windspeed'])
<feature_engineering>
num_classes = y_train.shape[1]
num_pixels = x_train.shape[1]
casual_df['casual']=np.log1p(casual_df['casual'])
 casual_df['windspeed']=np.log1p(casual_df['windspeed'])
<data_type_conversions>
seed=7
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.10, random_state=seed )
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)
<split>
def cnn_model() :
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.20))
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.20))
model.add(Flatten())
 model.add(Dense(128, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(128, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.compile(optimizer = 'adam' , loss = "categorical_crossentropy", metrics=["accuracy"])
 return model
model=cnn_model()
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 )<train_model>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1)
 datagen.fit(x_train )
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() 
<train_model>
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_test,y_test),
verbose = 1, steps_per_epoch=x_train.shape[0], callbacks=callbacks_list )
get_best_model(X_train, X_valid, y_train, y_valid )<compute_test_metric>
submit.Label =model.predict_classes(test )
<train_on_grid><EOS> 
submit.head()
submit.to_csv('submit.csv',index=False )