Kaggle competition: Digit Recognizer
evaluation = pd.DataFrame()<create_dataframe>
%matplotlib inline
def evaluation_df(method, mae, mse, rmse, evaluation):
temp_evaluation = pd.DataFrame({'Method':[method], 'MAE': [mae], 'MSE': [mse], 'RMSE': [rmse]})
 evaluation = pd.concat([evaluation, temp_evaluation])
 evaluation = evaluation[['Method', 'MAE', 'MSE', 'RMSE']]
return evaluation<create_dataframe>
test = pd.read_csv(".. /input/test.csv")
 train = pd.read_csv(".. /input/train.csv" )
evaluation = evaluation_df('Linear Regression', mae, mse, rmse, evaluation )<install_modules>
y_train = train["label"]
y_train.head()
!pip install xgboost<train_model>
x_train = train.drop(labels = ["label"],axis = 1 )
%%time
xgbreg = xgb.XGBRegressor()
xgbreg.fit(X_train_scalled, y_train )<compute_test_metric>
y_train.value_counts()
xgbreg.score(X_train_scalled, y_train )<predict_on_test>
x_train = x_train/255.0
test = test/255.0
y_train_pred = xgbreg.predict(X_train_scalled )<compute_test_metric>
x_train = x_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
 print('x_train shape:', x_train.shape )
r2_score(y_train, y_train_pred )<predict_on_test>
y_train = keras.utils.to_categorical(y_train,num_classes=10 )
y_val_pred = xgbreg.predict(X_val_scalled )<compute_test_metric>
random_seed = 1
r2_score(y_val, y_val_pred )<compute_test_metric>
from sklearn.model_selection import train_test_split
mae = metrics.mean_absolute_error(y_val, y_val_pred)
 mse = metrics.mean_squared_error(y_val, y_val_pred)
 rmse = np.sqrt(metrics.mean_absolute_error(y_val, y_val_pred))
print("Mean Absolute Error")
 print(mae)
 print()
print("Mean Squared Error")
 print(mse)
 print()
print("Root Mean Squared Error")
 print(rmse )<compute_test_metric>
from sklearn.model_selection import train_test_split
evaluation = evaluation_df('Extreme Gradient Boosting', mae, mse, rmse, evaluation )<choose_model_class>
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=random_seed )
<predict_on_test>
from keras.layers import LeakyReLU
<choose_model_class>
model = Sequential()
model.add(Conv2D(32,(3,3), padding='Same', activation='relu', input_shape=(28, 28, 1)))
 model.add(Dropout(0.25))
model.add(Conv2D(32,(7,7),activation='relu'))
model.add(Conv2D(128,(5,5),activation='relu'))
model.add(MaxPool2D(( 2,2)))
 model.add(Conv2D(64,(3,3), padding='Same',activation='relu'))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(Flatten())
 model.add(Dense(128, activation='linear'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(64, activation='linear'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(324, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(32, activation='linear'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(10, activation='softmax'))
model.summary()
%%time
xgbreg = xgb.XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.45, max_delta_step=0, max_depth=7,
min_child_weight=15, monotone_constraints='() ',
n_estimators=120, n_jobs=16, num_parallel_tree=1, random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,
tree_method='exact', validate_parameters=1, verbosity=None)
 xgbreg.fit(X_train_scalled, y_train )<compute_test_metric>
optimizer = Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
xgbreg.score(X_train_scalled, y_train )<predict_on_test>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=20,
zoom_range = 0.13,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x_train )
y_train_pred = xgbreg.predict(X_train_scalled )<compute_test_metric>
model.fit_generator(
 datagen.flow(x_train, y_train, batch_size=256),
steps_per_epoch=len(x_train)//256,
epochs=30,
)
r2_score(y_train, y_train_pred )<predict_on_test>
score = model.evaluate(x_val, y_val, verbose=1)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1] )
y_val_pred = xgbreg.predict(X_val_scalled )<compute_test_metric>
result = model.predict(test)
 result = np.argmax(result,axis=1)
 result = pd.Series(result,name="Label" )
r2_score(y_val, y_val_pred )<compute_test_metric>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),result],axis = 1)
 submission.to_csv("cnn_mnist.csv",index=False )