Kaggle competition: Digit Recognizer
predict = model.predict(x_test)
 predict = np.argmax(predict, axis=1 )<concatenate>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 print(train.shape)
 train.head()
out = np.column_stack(( range(1, predict.shape[0]+1), predict))<save_to_csv>
test= pd.read_csv(".. /input/digit-recognizer/test.csv")
 print(test.shape)
 test.head()
np.savetxt('submission.csv', out, header="ImageId,Label",
comments="", fmt="%d,%d" )<set_options>
Y_train = train["label"]
X_train = train.drop(columns = ["label"],axis = 1 )
%matplotlib inline 
<load_from_csv>
X_train = X_train / 255.0
test = test / 255.0
print("x_train shape: ",X_train.shape)
 print("test shape: ",test.shape )
train_data = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test_data = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<prepare_x_and_y>
Y_train = to_categorical(Y_train, num_classes = 10 )
X = train_data.drop(["label"],axis = 1 ).values
Y = train_data["label"].values<prepare_x_and_y>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1,random_state = 2)
 print("x_train shape",X_train.shape)
 print("x_test shape",X_val.shape)
 print("y_train shape",Y_train.shape)
 print("y_test shape",Y_val.shape )
X = X.reshape([42000,28,28,1])
 Y = Y.reshape([42000,1] )<categorify>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3,3),activation = 'relu',
kernel_initializer='he_normal', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3,3),
activation = 'relu', kernel_initializer='he_normal'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size =(2,2)))
 model.add(Dropout(0.4))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same',
activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same',
activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size =(2,2), strides =(2,2)))
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation = 'softmax'))
Y = to_categorical(Y, num_classes = 10 )<split>
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999 )
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.1, random_state = 14 )<feature_engineering>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
x_train = x_train/255
x_test = x_test/255<choose_model_class>
epochs = 39
batch_size = 64
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3,3), padding = 'same', activation='relu', input_shape=(28, 28, 1)) ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(64,(3,3), padding = 'same', activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(128,(3,3), padding = 'same', activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(128,(3,3), padding = 'same', activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation='softmax')
 ] )<choose_model_class>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=0.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 )
optimizer = Adam(learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999)
 model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics = ['accuracy'] )<choose_model_class>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
steps_per_epoch=X_train.shape[0] // batch_size, callbacks=[annealer] )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.6,
min_lr=0.00001 )<define_variables>
results = model.predict(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("sub.csv",index=False)