Project B- Recognising Handwritten Digits with scikit-learn
By: Md Ikram
Tags: Python, Pandas, Data Analysis, Scikit Learn, Data Visualization

#Step 1 Importing Libraries

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from sklearn import datasets

from sklearn import svm

#Step 2 i.Data sets load ii.Shape finding

digits= datasets.load_digits()

digits.data.shape

(1797, 64)

#Step 3 Using matplotlib finding gray scale image

plt.figure(figsize=(20,4))

for index, (image,label)in enumerate(zip(digits.data[0:6],digits.target[0:6])):

plt.subplot(1,6,index+1)

plt.imshow(np.reshape(image,(8,8)),cmap = plt.cm.gray_r)

plt.title(f’Training: {label}’, fontsize = 20)

#Step 4 Train the Support Vector Classification (SVC) estimator

svc = svm.SVC(gamma=0.001,C=100.)

svc.fit(digits.data[1:1790], digits.target[1:1790])

SVC(C=100.0, gamma=0.001)

#Step 5 Test our estimator.

#lets predict for 5 data

pred = svc.predict(digits.data[1791:1796])

pred

array([4, 9, 0, 8, 9])

#Step 6 Validate

digits.target[1791:1796]

array([4, 9, 0, 8, 9])

#We can see that the value of pred and digits.target is same i.e ([4,9,0,8,9]) we conclude our model is 100% accurate.

#Step 7 checking more data set:

pred_1 = svc.predict(digits.data[1591:1596])

pred_1

array([0, 0, 2, 2, 7])

#Step 8 Validate

digits.target[1591:1596]

array([0, 0, 2, 2, 7])

#Step 9 checking more data set:

pred_2 = svc.predict(digits.data[1084:1091])

pred_2

array([2, 6, 3, 3, 7, 3, 3])

#Step 10 Validate

digits.target[1084:1091]

array([2, 6, 3, 3, 7, 3, 3])

#Step 11 checking more data set:

pred_3 = svc.predict(digits.data[184:195])

pred_3

array([2, 0, 1, 2, 6, 3, 3, 7, 3, 3, 4])

#Step 12 Validate

digits.target[184:195]

array([2, 0, 1, 2, 6, 3, 3, 7, 3, 3, 4])

#Step 13 By SVC checking the accuracy of our model

#i.Split data into Train and Test using train_test_split()

from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test = train_test_split(digits.data,digits.target,test_size = 0.25, random_state=0)

#Step 14 putting data into SVC model

svc.fit(x_train,y_train)

SVC(C=100.0, gamma=0.001)

#Step 15 on test data checking data prediction

pred_acc = svc.predict(x_test)

pred_acc

array([2, 8, 2, 6, 6, 7, 1, 9, 8, 5, 2, 8, 6, 6, 6, 6, 1, 0, 5, 8, 8, 7,

8, 4, 7, 5, 4, 9, 2, 9, 4, 7, 6, 8, 9, 4, 3, 1, 0, 1, 8, 6, 7, 7,

1, 0, 7, 6, 2, 1, 9, 6, 7, 9, 0, 0, 5, 1, 6, 3, 0, 2, 3, 4, 1, 9,

2, 6, 9, 1, 8, 3, 5, 1, 2, 8, 2, 2, 9, 7, 2, 3, 6, 0, 5, 3, 7, 5,

1, 2, 9, 9, 3, 1, 7, 7, 4, 8, 5, 8, 5, 5, 2, 5, 9, 0, 7, 1, 4, 7,

3, 4, 8, 9, 7, 9, 8, 2, 6, 5, 2, 5, 8, 4, 1, 7, 0, 6, 1, 5, 5, 9,

9, 5, 9, 9, 5, 7, 5, 6, 2, 8, 6, 9, 6, 1, 5, 1, 5, 9, 9, 1, 5, 3,

6, 1, 8, 9, 8, 7, 6, 7, 6, 5, 6, 0, 8, 8, 9, 8, 6, 1, 0, 4, 1, 6,

3, 8, 6, 7, 4, 9, 6, 3, 0, 3, 3, 3, 0, 7, 7, 5, 7, 8, 0, 7, 8, 9,

6, 4, 5, 0, 1, 4, 6, 4, 3, 3, 0, 9, 5, 9, 2, 1, 4, 2, 1, 6, 8, 9,

2, 4, 9, 3, 7, 6, 2, 3, 3, 1, 6, 9, 3, 6, 3, 2, 2, 0, 7, 6, 1, 1,

9, 7, 2, 7, 8, 5, 5, 7, 5, 2, 3, 7, 2, 7, 5, 5, 7, 0, 9, 1, 6, 5,

9, 7, 4, 3, 8, 0, 3, 6, 4, 6, 3, 2, 6, 8, 8, 8, 4, 6, 7, 5, 2, 4,

5, 3, 2, 4, 6, 9, 4, 5, 4, 3, 4, 6, 2, 9, 0, 1, 7, 2, 0, 9, 6, 0,

4, 2, 0, 7, 9, 8, 5, 4, 8, 2, 8, 4, 3, 7, 2, 6, 9, 1, 5, 1, 0, 8,

2, 1, 9, 5, 6, 8, 2, 7, 2, 1, 5, 1, 6, 4, 5, 0, 9, 4, 1, 1, 7, 0,

8, 9, 0, 5, 4, 3, 8, 8, 6, 5, 3, 4, 4, 4, 8, 8, 7, 0, 9, 6, 3, 5,

2, 3, 0, 8, 3, 3, 1, 3, 3, 0, 0, 4, 6, 0, 7, 7, 6, 2, 0, 4, 4, 2,

3, 7, 8, 9, 8, 6, 8, 5, 6, 2, 2, 3, 1, 7, 7, 8, 0, 3, 3, 2, 1, 5,

5, 9, 1, 3, 7, 0, 0, 7, 0, 4, 5, 9, 3, 3, 4, 3, 1, 8, 9, 8, 3, 6,

2, 1, 6, 2, 1, 7, 5, 5, 1, 9])

#Step 16 Accuracy checking

from sklearn.metrics import accuracy_score

score = accuracy_score(y_test,pred1)

print(f’Accuracy = {round((score*100), 2)}%’)

Accuracy = 99.88%

#Accuracy of our model is 99.88% which is great