Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<compute_test_metric>
import pandas as pd
import numpy as np
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
rf = RandomForestClassifier(random_state=123)
 rf.fit(x_train,y_train)
 rf_pre = rf.predict(x_test)
 print("rf recall:",round(recall_score(y_test,rf_pre),2))
print("rf precision:",round(precision_score(y_test,rf_pre),2))
print("rf f1_score:",round(f1_score(y_test,rf_pre),2))
print("rf rou_auc_score:",round(roc_auc_score(y_test,rf_pre),2))<train_on_grid>
train = pd.read_csv(".. /input/train.csv")
 train.head()
params = {
"max_depth":(3,10),
"n_estimators":(50,300),
"min_samples_leaf":(1,5),
"min_samples_split":(0.1,0.2),
"min_impurity_decrease":(0,0.2),
}
rf_gd = RandomizedSearchCV(rf,param_distributions=params,cv=5,scoring="roc_auc")
 rf_gd.fit(x_train,y_train)
 print(rf_gd.best_score_)
 print(rf_gd.best_estimator_)
 print(rf_gd.best_params_ )<compute_test_metric>
X_train = train.drop(labels="label", axis=1)
 y_train = train["label"].values
X_train = X_train.values.reshape(-1, 28, 28, 1)
 X_train = X_train/255
rfgd_pre = rf_gd.predict(x_test)
 print("rf_gd recall:",round(recall_score(y_test,rfgd_pre),2))
print("rf_gd precision:",round(precision_score(y_test,rfgd_pre),2))
print("rf_gd f1_score:",round(f1_score(y_test,rfgd_pre),2))
print("rf_gd rou_auc_score:",round(roc_auc_score(y_test,rfgd_pre),2))<compute_train_metric>
y_train = to_categorical(y_train, num_classes=10 )
knn = KNeighborsClassifier()
knn.fit(x_train,y_train)
 knn_pre = knn.predict(x_test)
 print("knn recall:",round(recall_score(y_test,knn_pre),2))
print("knn precision:",round(precision_score(y_test,knn_pre),2))
print("knn f1_score:",round(f1_score(y_test,knn_pre),2))
print("knn rou_auc_score:",round(roc_auc_score(y_test,knn_pre),2))<train_on_grid>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42 )
params = {'algorithm': ['auto'], 'weights': ['uniform', 'distance'], 'leaf_size': list(range(1,50,5)) ,
'n_neighbors': [3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,22]}
knn_gd=GridSearchCV(knn, param_grid = params, verbose=True,
cv=10, scoring = "roc_auc")
 knn_gd.fit(x_train, y_train)
 print(knn_gd.best_score_)
 print(knn_gd.best_estimator_)
 print(knn_gd.best_params_ )<compute_test_metric>
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout, Input, Add
from keras.layers import BatchNormalization, Activation
from keras.models import Model
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
knngd_pre = knn_gd.predict(x_test)
 print("knngd_pre recall:",round(recall_score(y_test,knngd_pre),2))
print("knngd_pre precision:",round(precision_score(y_test,knngd_pre),2))
print("knngd_pre f1_score:",round(f1_score(y_test,knngd_pre),2))
print("knngd_pre rou_auc_score:",round(roc_auc_score(y_test,knngd_pre),2))<compute_test_metric>
inputs = Input(shape=(28, 28, 1))
bn0 = BatchNormalization(scale=True )(inputs)
 conv1 = Conv2D(32, kernel_size=(7,7), padding='same', activation='relu', kernel_initializer='uniform' )(bn0)
 conv1 = Conv2D(32, kernel_size=(7,7), padding='same', activation='relu', kernel_initializer='uniform' )(conv1)
 bn1 = BatchNormalization(scale=True )(conv1)
 max_pool1 = MaxPooling2D(pool_size=(2,2))(bn1)
 conv2 = Conv2D(32, kernel_size=(5,5), padding='same', activation='relu', kernel_initializer='uniform' )(max_pool1)
 conv2 = Conv2D(32, kernel_size=(5,5), padding='same', activation='relu', kernel_initializer='uniform' )(conv2)
 conv2 = Conv2D(32, kernel_size=(5,5), padding='same', activation='relu', kernel_initializer='uniform' )(conv2)
 bn2 = BatchNormalization(scale=True )(conv2)
 res_conv1 = Conv2D(32, kernel_size=(3,3), padding='same', kernel_initializer='uniform' )(max_pool1)
 res_bn1 = BatchNormalization(scale=True )(res_conv1)
 add1 = Add()([res_bn1, bn2])
 act1 = Activation('relu' )(add1)
 max_pool2 = MaxPooling2D(pool_size=(2,2))(act1)
 conv3 = Conv2D(64, kernel_size=(5,5), padding='same', activation='relu', kernel_initializer='uniform' )(max_pool2)
 conv3 = Conv2D(64, kernel_size=(5,5), padding='same', activation='relu', kernel_initializer='uniform' )(conv3)
 conv3 = Conv2D(64, kernel_size=(5,5), padding='same', activation='relu', kernel_initializer='uniform' )(conv3)
 bn3 = BatchNormalization(scale=True )(conv3)
 res_conv2 = Conv2D(64, kernel_size=(3,3), padding='same', kernel_initializer='uniform' )(max_pool2)
 res_bn2 = BatchNormalization(scale=True )(res_conv2)
 add2 = Add()([res_bn2, bn3])
 act2 = Activation('relu' )(add2)
 max_pool3 = MaxPooling2D(pool_size=(2,2))(act2)
 flatten = Flatten()(max_pool3)
 dense1 = Dense(128, activation='relu' )(flatten)
 do = Dropout(0.25 )(dense1)
 dense2 = Dense(10, activation='softmax' )(do)
 model = Model(inputs=[inputs], outputs=[dense2])
 adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])
 model.summary()
xgb = XGBClassifier(random_state=123,eval_metric="logloss",use_label_encoder=False)
 xgb.fit(x_train,y_train)
 xgb_pre = xgb.predict(x_test)
 print("xgb recall:",round(recall_score(y_test,xgb_pre),2))
print("xgb precision:",round(precision_score(y_test,xgb_pre),2))
print("xgb f1_score:",round(f1_score(y_test,xgb_pre),2))
print("xgb rou_auc_score:",round(roc_auc_score(y_test,xgb_pre),2))<train_on_grid>
datagen = ImageDataGenerator(rotation_range=25,
width_shift_range=0.1,
height_shift_range=0.1)
 datagen.fit(X_train )
params ={
"max_depth":(2,8),
"n_estimators":(100,400),
"learning_rate":(0.5,1),
"gamma":(0,0.2),
"reg_lambda":(0.1,0.3),
"reg_alpha":(0.1,0.3),
"colsample_bytree":(0.8,1),
}
xgb_gd = RandomizedSearchCV(xgb,param_distributions=params,cv=5,scoring="roc_auc")
 xgb_gd.fit(x_train,y_train)
 print(xgb_gd.best_score_)
 print(xgb_gd.best_estimator_)
 print(xgb_gd.best_params_ )<compute_test_metric>
checkpointer = ModelCheckpoint(filepath='weights.{epoch:02d}-{val_loss:.3f}.hdf5', verbose=1, save_best_only=True)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=2, min_lr=0.000001, verbose=1)
 batch_size = 32
epochs = 40
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size),
epochs=epochs, validation_data=(X_val,y_val),
verbose=1, steps_per_epoch=X_train.shape[0] // batch_size,
callbacks=[checkpointer, reduce_lr] )
xgbgd_pre = xgb_gd.predict(x_test)
 print("xgb_gd recall:",round(recall_score(y_test,xgbgd_pre),2))
print("xgb_gd precision:",round(precision_score(y_test,xgbgd_pre),2))
print("xgb_gd f1_score:",round(f1_score(y_test,xgbgd_pre),2))
print("xgb_gd rou_auc_score:",round(roc_auc_score(y_test,xgbgd_pre),2))<compute_train_metric>
model = load_model("weights.17-0.005.hdf5" )
logstic = LogisticRegression(random_state=123)
 logstic.fit(x_train,y_train)
 logstic_pre = logstic.predict(x_test)
 print("logstic recall:",round(recall_score(y_test,logstic_pre),2))
print("logstic precision:",round(precision_score(y_test,logstic_pre),2))
print("logstic f1_score:",round(f1_score(y_test,logstic_pre),2))
print("logstic rou_auc_score:",round(roc_auc_score(y_test,logstic_pre),2))<train_on_grid>
test = pd.read_csv(".. /input/test.csv")
 X_test = test/255
X_test = X_test.values.reshape(-1, 28, 28, 1 )
prams = {
"C":[0.1,0.2,0.3,0.4,0.5],
"solver":["newton-cg","lbfgs","sag","saga"]
}
logstic_gd=GridSearchCV(logstic, param_grid = prams, verbose=True,
cv=10, scoring = "roc_auc")
 logstic_gd.fit(x_train, y_train)
 print(logstic_gd.best_score_)
 print(logstic_gd.best_estimator_)
 print(logstic_gd.best_params_ )<compute_test_metric>
results = model.predict(X_test)
 results = np.argmax(results,axis = 1 )
<split><EOS> 
results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("resnet_mnist.csv",index=False )