# Handwritten Digit Prediction using MNIST Dataset in Keras

In this tutorial, we will learn to predict handwritten digit using MNIST dataset in Keras with the corresponding Python code. We will use Keras API for this purpose.

There will be the following sections:

- Importing Libraries
- Importing Dataset
- Data Preprocessing
- Building the model
- Training the model on the Dataset
- Predicting the test results

We will be using CNN(Convolutional Neural Network).

**What is Convolutional Neural Network?**

Convolutional Neural Networks are a special type of neural network which comes under Deep Learning. This neural network is majorly used for image-related datasets. CNNs are used in a lot of domains, especially in the medical field. eg: The respiratory detection system can be built using CNN.

**HANDWRITTEN DIGIT PREDICTION Using MNIST DATASET**

We will build a model predicting the handwritten digit.

**1. IMPORTING LIBRARIES**

import numpy as np import pandas as pd from keras.models import Sequential from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D from keras.datasets import mnist

Pandas: A Python package which is a fast, powerful, and open-source data manipulation tool.

Numpy: A Python package used for scientific computing.

Sequential: The model used will be sequential.

Dense, Conv2D, Flatten, MaxPooling2D: These are the layers we will use.

So, these are the initial libraries you need to have. You can find documentation of pandas, NumPy, and keras.layers libraries at the end of this tutorial.

**2. IMPORTING DATASET**

We have already imported the mnist dataset from keras. We need to load the data.

(x_train, y_train), (x_test, y_test) = mnist.load_data() print(x_train.shape)

OUTPUT:

(60000, 28, 28)

**3. DATA PREPROCESSING**

First, we will transform our dataset,i.e. reshape.

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

OUTPUT:

(60000, 28, 28, 1)

We will normalize our data between 0 and 1.

x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255

Now, we need to take care of the class variables. For that, we will use to_categorical function from np_utils class.

Originally:

print(y_train.shape) print(y_train[:10])

OUTPUT:

(60000,) [5 0 4 1 9 2 1 3 1 4]

After using the function:

from keras.utils import np_utils y_train = np_utils.to_categorical(y_train, 10) Y_test = np_utils.to_categorical(y_test, 10) print(y_train.shape) print(y_train)

OUTPUT:

(60000, 10) [[0. 0. 0. ... 0. 0. 0.] [1. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] ... [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 1. 0.]]

**4. BUILDING THE MODEL**

We will build a CNN model.

model = Sequential() model.add(Conv2D(28, kernel_size=(3,3), activation = 'relu', input_shape = (28, 28, 1))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(128, activation = 'relu')) model.add(Dense(10,activation = 'softmax'))

We have used one Conv2D layer, one Maxpooling layer, and two dense layers. In between, we have used a flatten layer.

Next, we will compile the model:

model.compile(optimizer='adam', loss = 'mean_squared_error', metrics=['accuracy'])

**Thus, the model has been built.**

**5. TRAINING THE MODEL**

To train our model with the dataset, we will pass x_train and y_train into the fit() function.

model.fit(x_train, y_train, epochs = 5)

I have given just five epochs. You can change according to your convenience.

OUTPUT:

**Thus, we have built a model using mnist dataset from Keras that will predict the handwritten digits.**

Also, you may visit:

- Getting Started with Keras
- Fundamentals of layers and models in keras
- Building A Neural Network using KERAS
- Prediction using LSTM

*Thank you for reaching till here.*

## Leave a Reply