Digit Recognizer aka “Hello World” of Machine Learning
By: Tarak Dutta
Tags: Mnist Dataset, Digit Recognition, Keras, Sequential, Machine Learning

Digit Recognizer is one of the basic and first problem that a budding Machine Learning engineer should try their hands on. It is a simple problem where the challenge is to recognize hand-written digits.

As per Kaggle, here is the detailed description…

MNIST (“Modified National Institute of Standards and Technology”) is the de facto “hello world” dataset of computer vision. Since its release in 1999, this classic dataset of handwritten images has served as the basis for benchmarking classification algorithms. As new machine learning techniques emerge, MNIST remains a reliable resource for researchers and learners alike. In this competition, your goal is to correctly identify digits from a dataset of tens of thousands of handwritten images. We’ve curated a set of tutorial-style kernels which cover everything from regression to neural networks. We encourage you to experiment with different algorithms to learn first-hand what works well and how techniques compare. https://www.kaggle.com/c/digit-recognizer/overview

Step 1: Import the libraries

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns

Step 2: Load the data files to Pandas DataFrame

train_data = pd.read_csv(r’/kaggle/input/digit-recognizer/train.csv’)

test_data = pd.read_csv(r’/kaggle/input/digit-recognizer/test.csv’)

Step 3: Preview the Data Files

print(‘Train Data sample’)

print(train_data.head())

print(‘Shape of the train data file {}’.format(train_data.shape))

print(‘Shape of the train data file {}’.test_data.shape))

Step 4: Let’s convert the Pandas DataFrame data to a Numpy Array

train_data = np.array(train_data, dtype=’float32')

test_data = np.array(test_data, dtype=’float32')

print(‘Shape of the train data {}’.format(train_data.shape))

print(‘Shape of the train data{}’.test_data.shape))

Step 5: Let’s visualize a random sample from the data set

import random

i = random.randint(1, 42000)

plt.imshow(train_data[i,1:].reshape(28,28))

label = train_data[i,0]

label

Step 6: Visualize some more sample from the data

w_grid = 15

l_grid = 15

fig,axes = plt.subplots(w_grid, l_grid, figsize=(16,16))

axes = axes.ravel()

n_training = len(train_data)

for i in np.arange(0, w_grid*l_grid):

index = np.random.randint(0, n_training)

axes[i].imshow(train_data[index,1:].reshape(28,28))

axes[i].set_title(train_data[index,0], fontsize=8)

axes[i].axis(‘off’)

plt.subplots_adjust(hspace=0.4)

Step 7: Split the data into train and validate data

x_train = train_data[:,1:]

x_train = x_train/255. # Normalize the data

y_train = train_data[:,0] from sklearn.model_selection import train_test_split

x_train,x_validate,y_train,y_validate = train_test_split(x_train,y_train,test_size=0.2, random_state=5) # Check the shape of train and validate data print(‘Shape of x Train data is {}’.format(x_train.shape))

print(‘Shape of y Train data is {}’.format(y_train.shape))

print(‘Shape of x Validate data is {}’.format(x_validate.shape))

Print(‘Shape of y Validate data is {}’.format(y_validate.shape)) # Most of python model can process data only 1-D array, so we will convert the data into a single dimension array

x_train = x_train.reshape(x_train.shape[0], *(28,28,1))

x_validate = x_validate.reshape(x_validate.shape[0], *(28,28,1))

Step 8: Import the Model packages and train it

# Import the packages

from keras.models import Sequential

from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout

from keras.optimizers import Adam

from keras.callbacks import TensorBoard # Define and fit the model

cnn_model = Sequential() cnn_model.add(Conv2D(32, 3, 3, input_shape=(28,28,1), activation = ‘relu’))

cnn_model.add(MaxPooling2D(pool_size = (2,2)))

cnn_model.add(Flatten())

cnn_model.add(Dense(32, activation=’relu’))

cnn_model.add(Dense(10, activation=’sigmoid’)) cnn_model.compile(loss =’sparse_categorical_crossentropy’, optimizer=Adam(lr=0.001), metrics =[‘accuracy’])

epochs = 80

cnn_model.fit(x_train, y_train, batch_size = 512, epochs = epochs, verbose = 1,validation_data = (x_validate, y_validate)) #check the model

cnn_model.summary()

Step 9: Normalize and reshape Test Data

x_test = test_data/255.

x_test = x_test.reshape(x_test.shape[0], *(28,28,1))

Step 10: Make a prediction and validate it