Project

Predicting Hand Written Numbers using Neural Network

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import keras
In [118]:
df=pd.read_csv('mnist_train.csv')
df1=pd.read_csv('mnist_test.csv')
In [119]:
df
Out[119]:
label 1x1 1x2 1x3 1x4 1x5 1x6 1x7 1x8 1x9 ... 28x19 28x20 28x21 28x22 28x23 28x24 28x25 28x26 28x27 28x28
0 5 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 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
2 4 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
4 9 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
59995 8 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
59996 3 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
59997 5 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
59998 6 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
59999 8 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0

60000 rows × 785 columns

In [120]:
df1
Out[120]:
label 1x1 1x2 1x3 1x4 1x5 1x6 1x7 1x8 1x9 ... 28x19 28x20 28x21 28x22 28x23 28x24 28x25 28x26 28x27 28x28
0 7 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
1 2 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
4 4 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
9995 2 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
9996 3 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
9997 4 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
9998 5 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
9999 6 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0

10000 rows × 785 columns

In [122]:
X_train=df.drop(['label'],axis=1)
y_train=df['label']
In [124]:
X_test=df1.drop(['label'],axis=1)
y_test=df1['label']
In [125]:
X_train.shape,y_train.shape,X_test.shape,y_test.shape
Out[125]:
((60000, 784), (60000,), (10000, 784), (10000,))
In [126]:
#Expand the dimension of the image
In [127]:
X_train = X_train.values.astype('float32')
X_train = X_train.reshape(X_train.shape[0],28, 28,1)
In [128]:
X_train.shape
Out[128]:
(60000, 28, 28, 1)
In [129]:
X_test = X_test.values.astype('float32')
X_test = X_test.reshape(X_test.shape[0],28, 28,1)
In [130]:
X_test.shape
Out[130]:
(10000, 28, 28, 1)
In [ ]:
# image plotting
In [121]:
def plot_image(n):
    plt.imshow(X_train[n])
    print('The Number is:',y_train[n])
    plt.show()
In [101]:
#Normalizing the image
In [102]:
X_train=X_train/255
X_test=X_test/255
In [131]:
for i in range(0,5):
    plot_image(i)
The Number is: 5
The Number is: 0
The Number is: 4
The Number is: 1
The Number is: 9
In [ ]:
#Converting to one hot vectors
In [132]:
y_train=keras.utils.to_categorical(y_train)
In [134]:
y_test=keras.utils.to_categorical(y_test)
In [136]:
y_train
Out[136]:
array([[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.]], dtype=float32)
In [137]:
y_test
Out[137]:
array([[0., 0., 0., ..., 1., 0., 0.],
       [0., 0., 1., ..., 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.]], dtype=float32)
In [138]:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPool2D
In [142]:
model=Sequential()
model.add(Conv2D(32,(3,3),input_shape=(28,28,1),activation='relu'))
model.add(MaxPool2D(2,2))

model.add(Conv2D(64,(3,3),activation='relu'))
model.add(MaxPool2D(2,2))

model.add(Flatten())

model.add(Dropout(0.25))

model.add(Dense(10,activation='softmax'))
In [143]:
model.summary()
Model: "sequential_3"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_4 (Conv2D)           (None, 26, 26, 32)        320       
                                                                 
 max_pooling2d_2 (MaxPooling  (None, 13, 13, 32)       0         
 2D)                                                             
                                                                 
 conv2d_5 (Conv2D)           (None, 11, 11, 64)        18496     
                                                                 
 max_pooling2d_3 (MaxPooling  (None, 5, 5, 64)         0         
 2D)                                                             
                                                                 
 flatten_1 (Flatten)         (None, 1600)              0         
                                                                 
 dropout (Dropout)           (None, 1600)              0         
                                                                 
 dense (Dense)               (None, 10)                16010     
                                                                 
=================================================================
Total params: 34,826
Trainable params: 34,826
Non-trainable params: 0
_________________________________________________________________
In [145]:
model.compile(optimizer='adam',loss=keras.losses.categorical_crossentropy,metrics=['accuracy'])
In [146]:
img=model.fit(X_train, y_train,epochs=20, verbose=1)
Epoch 1/20
1875/1875 [==============================] - 33s 15ms/step - loss: 0.4093 - accuracy: 0.9299
Epoch 2/20
1875/1875 [==============================] - 28s 15ms/step - loss: 0.0935 - accuracy: 0.9727
Epoch 3/20
1875/1875 [==============================] - 27s 14ms/step - loss: 0.0761 - accuracy: 0.9770
Epoch 4/20
1875/1875 [==============================] - 27s 14ms/step - loss: 0.0667 - accuracy: 0.9800
Epoch 5/20
1875/1875 [==============================] - 27s 14ms/step - loss: 0.0620 - accuracy: 0.9815
Epoch 6/20
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0543 - accuracy: 0.9840
Epoch 7/20
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0543 - accuracy: 0.9839
Epoch 8/20
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0493 - accuracy: 0.9855
Epoch 9/20
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0470 - accuracy: 0.9860
Epoch 10/20
1875/1875 [==============================] - 626s 334ms/step - loss: 0.0442 - accuracy: 0.9878
Epoch 11/20
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0425 - accuracy: 0.9876
Epoch 12/20
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0419 - accuracy: 0.9881
Epoch 13/20
1875/1875 [==============================] - 24s 13ms/step - loss: 0.0407 - accuracy: 0.9882
Epoch 14/20
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0412 - accuracy: 0.9889
Epoch 15/20
1875/1875 [==============================] - 28s 15ms/step - loss: 0.0395 - accuracy: 0.9893
Epoch 16/20
1875/1875 [==============================] - 28s 15ms/step - loss: 0.0388 - accuracy: 0.9886
Epoch 17/20
1875/1875 [==============================] - 28s 15ms/step - loss: 0.0391 - accuracy: 0.9896
Epoch 18/20
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0371 - accuracy: 0.9900
Epoch 19/20
1875/1875 [==============================] - 29s 15ms/step - loss: 0.0358 - accuracy: 0.9899
Epoch 20/20
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0403 - accuracy: 0.9902
In [148]:
predictions = model.predict(X_test, verbose=0)
In [150]:
score=model.evaluate(X_test,y_test)
313/313 [==============================] - 2s 5ms/step - loss: 0.0496 - accuracy: 0.9881
In [151]:
print('The model accuracy is:',score[1])
The model accuracy is: 0.988099992275238
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.