Kaggle competition: Digit Recognizer
augumentator = tf.keras.preprocessing.image.ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.15,
shear_range=0.1,
zoom_range=0.1,
validation_split=0.0,
horizontal_flip=False,
vertical_flip=False)
 augumentator.fit(X_train )<train_model>
%matplotlib inline
history = model.fit(augumentator.flow(X_train, y_train, batch_size = 32), epochs = 10,
validation_data =(X_holdout, y_holdout), verbose = 1, callbacks = [reduce_lr] )<load_pretrained>
train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_df = pd.read_csv('.. /input/digit-recognizer/test.csv' )
mnist = tf.keras.datasets.mnist 
(X_train_mnist, y_train_mnist),(X_val_mnist, y_val_mnist)= mnist.load_data()<categorify>
X = train_df.drop('label',axis=1)
 y = train_df['label']
X = X.values.reshape(-1,28,28,1)
 X = X/255
y = to_categorical(y)
 print(plt.imshow(X[2][:,:,0]))
print(str(y[1]))
y_train_mnist = np.array(pd.get_dummies(pd.Series(y_train_mnist)))
 y_holdout_mnist = np.array(pd.get_dummies(pd.Series(y_val_mnist)) )<define_variables>
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=123 )
X_train_mnist = X_train_mnist.reshape(-1, 28, 28, 1)
 X_holdout_mnist = X_val_mnist.reshape(-1, 28, 28, 1)
 X_train_mnist = X_train_mnist / 255
X_holdout_mnist = X_holdout_mnist /255<concatenate>
datagen = ImageDataGenerator(zoom_range = 0.1, width_shift_range = 0.1, height_shift_range = 0.1, rotation_range = 10 )
X_train_ext = np.concatenate(( X_train, X_train_mnist), axis = 0)
 X_holdout_ext = np.concatenate(( X_holdout, X_holdout_mnist), axis = 0)
 y_train_ext = np.concatenate(( y_train, y_train_mnist), axis = 0)
 y_holdout_ext = np.concatenate(( y_holdout, y_holdout_mnist), axis = 0 )<train_model>
model = Sequential()
model.fit(X_train_ext, y_train_ext, batch_size=32, epochs=20, verbose=1, validation_data=(X_holdout, y_holdout), callbacks = [reduce_lr] )<save_to_csv>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation = 'relu', input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(5, 5), activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides =(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(5, 5), activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides =(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(1024, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation = 'softmax'))
predictions = model.predict(data_test ).argmax(axis = 1)
 predictions
submission = pd.DataFrame({'ImageId':np.arange(1, len(predictions)+1), 'Label':predictions})
 submission.to_csv('submission.csv', index=False )<import_modules>
model.compile(optimizer='adam',metrics=['accuracy'],loss='categorical_crossentropy' )
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from tensorflow.keras import utils
from tensorflow.keras.preprocessing import image
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
import tensorflow as tf
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<load_from_csv>
reduction_lr = ReduceLROnPlateau(monitor='val_accuracy',patience=2, verbose=1, factor=0.2, min_lr=0.00001 )
data_train = np.loadtxt('/kaggle/input/digit-recognizer/train.csv', skiprows = 1, delimiter= ',')
 data_train[0:5]<train_model>
hist = model.fit_generator(datagen.flow(X_train,y_train,batch_size=32),epochs=20,validation_data =(X_test,y_test),callbacks=[reduction_lr] )
x_train = data_train[:, 1:]
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
 input_shape =(28, 28, 1 )<feature_engineering>
final_loss, final_acc = model.evaluate(X_test, y_test, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))
x_train = x_train / 255.0<prepare_x_and_y>
test_df = test_df.values.reshape(-1, 28, 28, 1)/ 255
y_pred = model.predict(test_df, batch_size = 64)
 y_pred = np.argmax(y_pred,axis = 1)
 y_pred = pd.Series(y_pred,name="Label")
 y_pred
<categorify><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),y_pred],axis = 1)
 submission.to_csv("submission.csv",index=False )