Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<split>
mnist_test = pd.read_csv(".. /input/mnist-digit-recognizer/mnist_test.csv")
 mnist_train = pd.read_csv(".. /input/mnist-digit-recognizer/mnist_train.csv")
 sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
random_seed = 2
X_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=random_seed )<choose_model_class>
test['dataset'] = 'test'
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1)
<choose_model_class>
train['dataset'] = 'train'
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(MaxPooling2D(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(MaxPooling2D(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"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"] )<choose_model_class>
dataset = pd.concat([train.drop('label', axis=1), test] ).reset_index()
checkpoint = ModelCheckpoint('mnist-cnn.h5',
monitor='val_acc',
save_best_only=True,
verbose=1 )<choose_model_class>
mnist = pd.concat([mnist_train, mnist_test] ).reset_index(drop=True)
 labels = mnist['label'].values
mnist.drop('label', axis=1, inplace=True)
 mnist.columns = cols
learn_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
<train_model>
idx_mnist = mnist.sort_values(by=list(mnist.columns)).index
dataset_from = dataset.sort_values(by=list(mnist.columns)) ['dataset'].values
original_idx = dataset.sort_values(by=list(mnist.columns)) ['index'].values
batch_size=96
history = model.fit(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs=30,
validation_data=(X_val, Y_val),
steps_per_epoch=X_train.shape[0] // batch_size,
verbose=1,
callbacks=[checkpoint, learn_rate_reduction] )<load_from_csv>
for i in range(len(idx_mnist)) :
if dataset_from[i] == 'test':
sample_submission.loc[original_idx[i], 'Label'] = labels[idx_mnist[i]]
data_test = np.loadtxt('/kaggle/input/digit-recognizer/test.csv', skiprows = 1, delimiter=',')
 x_test = data_test.reshape(data_test.shape[0], 28, 28, 1)
 x_test /= 255.0<predict_on_test>
sample_submission.to_csv('submission.csv', index=False )