Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
true_labels = data_train.label
data_train = data_train.drop('label', axis = 1 )<categorify>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300)
 data_train_embedded = tsne.fit_transform(sample.drop('label', axis = 1))<train_model>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1 )
X_train, X_holdout, y_train, y_holdout = train_test_split(data_train_pd.drop('label', axis = 1), data_train_pd.label,
test_size = 0.25, random_state=0)
 knn = KNeighborsClassifier(n_neighbors=10, n_jobs=-1)
 knn.fit(X_train, y_train)
<train_model>
X_train = X_train / 255.0
test = test / 255.0
X_train, X_holdout, y_train, y_holdout = train_test_split(data_train_pd.drop('label', axis = 1), data_train_pd.label,
test_size = 0.25, random_state=0)
 bnbclf = BernoulliNB()
bnbclf.fit(X_train, y_train )<compute_test_metric>
Y_train = np_utils.to_categorical(Y_train, num_classes = 10 )
print("Accuracy score: {:.2f}".format(bnbclf.score(X_holdout, y_holdout)))
 print("Cross-entropy loss: {:.2f}".format(log_loss(np.array(y_holdout), bnbclf.predict_proba(X_holdout))))<train_on_grid>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1 )
bnb_params = {'alpha': np.arange(0.01, 0.1, 0.05),
'binarize' : np.arange(0, 0.5, 0.2),
'fit_prior': [True, False]
}
bnbcv = GridSearchCV(bnbclf, param_grid = bnb_params, cv = 3 )<train_model>
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"))
bnbcv.fit(X_train, y_train)
 bnb_best = bnbcv.best_estimator_<find_best_params>
model.compile(optimizer = 'Adam' , loss = "categorical_crossentropy", metrics=["accuracy"] )
bnbcv.best_params_<compute_test_metric>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
print("Accuracy score: {:.2f}".format(bnb_best.score(X_holdout, y_holdout)))
 print("Cross-entropy loss: {:.2f}".format(log_loss(np.array(y_holdout), bnb_best.predict_proba(X_holdout))))<choose_model_class>
epochs = 20
batch_size = 86
model = Sequential()
model.add(Convolution2D(32,(3, 3), activation='relu', input_shape=(28,28,1)))
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(axis=1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer="zeros", gamma_initializer="ones",))
model.add(Convolution2D(32,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(axis=1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer="zeros", gamma_initializer="ones",))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))<feature_engineering>
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.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
data_train = data_train / 255
data_test = data_test / 255<prepare_x_and_y>
datagen.fit(X_train )
y = np.array(pd.get_dummies(true_labels))<split>
from keras.callbacks import ReduceLROnPlateau
X_train, X_holdout, y_train, y_holdout = train_test_split(data_train, y,
test_size = 0.25, random_state=17 )<choose_model_class>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
factor=0.1,
patience=5,
min_lr=0.000001,
verbose=1 )<train_model>
history = 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] )
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
 result = model.fit(X_train, y_train, batch_size=32, epochs=10, verbose=1, validation_data=(X_holdout, y_holdout), callbacks = [reduce_lr] )<choose_model_class>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
layer_names = [layer.name for layer in model.layers]
layer_outputs = [layer.output for layer in model.layers]
layer_outputs = [layer_outputs[0], layer_outputs[2]]
feature_map_model = Model(model.input, layer_outputs)
 im = X_train[99:100,:]
feature_maps = feature_map_model.predict(im )<train_model>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )