Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<init_hyperparams>
warnings.filterwarnings("ignore")
 sns.set(style='white', context='notebook', palette='deep')
 def binary_pred_stats(ytrue, ypred, threshold=0.5):
one_correct = np.sum(( ytrue==1)*(ypred > threshold))
zero_correct = np.sum(( ytrue==0)*(ypred <= threshold))
sensitivity = one_correct / np.sum(ytrue==1)
 specificity = zero_correct / np.sum(ytrue==0)
 accuracy =(one_correct + zero_correct)/ len(ytrue)
 return sensitivity, specificity, accuracy
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):

 plt.imshow(cm, interpolation='nearest', cmap=cmap)
 plt.title(title)
 plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
 plt.yticks(tick_marks, classes)
 if normalize:
cm = cm.astype('float')/ cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 2.
 for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])) :
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
 plt.tight_layout()
plt.ylabel('True label')
 plt.xlabel('Predicted label' )
lgb_params = dict(tuner_cv.best_params)
 lgb_params["learning_rate"] = 0.005
lgb_params["early_stopping_round"] = 200
lgb_params["num_iterations"] = 20000<statistical_test>
df_train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 df_test = pd.read_csv(".. /input/digit-recognizer/test.csv")
folds_average_lgbm = FoldsAverageLGBM(folds )<train_model>
df_train.info()
df_train.head()
Y_train = df_train['label']
X_train = df_train.drop(labels = ["label"],axis = 1)
 X_test = df_test
Y_train.hist()
Y_train.value_counts()
X_train = X_train.values.reshape(-1,28,28,1)
 X_test = X_test.values.reshape(-1,28,28,1)
 input_shape =(28,28,1)
 X_train = X_train / 255.0
X_test = X_test / 255.0
Y_train = to_categorical(Y_train, num_classes = 10)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.15, random_state = 3)
 plt.figure()
g = plt.imshow(X_train[10][:,:,0])
 print(Y_train[10])
folds_average_lgbm.fit(lgb_params, train_x, train_y )<compute_test_metric>
model = Sequential()
model.add(Conv2D(filters = 128, kernel_size=(5, 5), activation='relu', padding='same', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size=(5, 5), activation='relu', padding='same', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax'))
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 model.summary()
np.sqrt(mean_squared_error(df_train.target, folds_average_lgbm.oof_preds))<predict_on_test>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 epochs = 50
batch_size = 128
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train)
y_pred = folds_average_lgbm.predict(test_x )<save_to_csv>
%%time

 estimator = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction])
sub = df_sample.copy()
sub["target"] = y_pred
sub.to_csv("submission_optuna_lgbm_ohe_v1.csv", index=False)
 sub.head()<set_options>
predtrain = model.predict(X_train)
 sensitivity, specificity, accuracy = binary_pred_stats(Y_train, predtrain)
 print("train set:", sensitivity, specificity, accuracy)
 predtest = model.predict(X_val)
 sensitivity, specificity, accuracy = binary_pred_stats(Y_val, predtest)
 print("test set: ", sensitivity, specificity, accuracy )
warnings.filterwarnings("ignore" )<load_from_csv>
results = model.predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_with_datagen.csv",index=False )