Sitemap

Recognizing Handwritten Digits

4 min readMay 21, 2021

--

Author : Rithik Alias

Dataset Used

The scikit-learn library provides numerous datasets that are useful for testing many problems of data analysis and prediction of the results. Also in this case there is a dataset of images called Digits. This dataset consists of 1,797 images that are 8x8 pixels in size. Each image is a handwritten digit in grayscale.

Each image of the dataset will be stored as an array as shown below.

array([[ 0., 0., 5., 13., 9., 1., 0., 0.], [ 0., 0., 13., 15., 10., 15., 5., 0.], [ 0., 3., 15., 2., 0., 11., 8., 0.], [ 0., 4., 12., 0., 0., 8., 8., 0.], [ 0., 5., 8., 0., 0., 9., 8., 0.], [ 0., 4., 11., 0., 1., 12., 7., 0.], [ 0., 2., 14., 5., 10., 12., 0., 0.], [ 0., 0., 6., 13., 10., 0., 0., 0.]])

The above array represents the following image that of a ‘0’. In the above array the values represents each pixel. The image is of size 8*8 pixels. Here each number in this array correspond to a grayscale from white, with a value of 0, to black, with the value 15.

The 1st image in the dataset

We can plot the above array and obtain this corresponding image using matplotlib library. The code is available in the code that is provided in this article.

The numerical values represented by images, i.e., the targets, are contained in the digit.targets array.

digits.target , You will get the following result:

array([0, 1, 2, …, 8, 9, 8])

The dataset is a training set consisting of 1,797 images. You can determine if that is true.

digits.target.size

This will be the result: 1797

Objective

The objective is to recognize these handwritten digits using our model.

We also want to test the hypothesis “The Digits data set of scikit-learn library provides numerous data-sets that are useful for testing many problems of data analysis and prediction of the results. Some Scientist claims that it predicts the digit accurately 95% of the times.”

The Code

The link to the code : https://colab.research.google.com/drive/1Boe5GFMYe7v6BgVJSWU78h-9dzP5u2Qm?usp=sharing

Learning and Predicting

Once we define a predictive model, we must instruct it with a training set, which is a set of data in which we already know the belonging class. Given the large quantity of elements contained in the Digits dataset, we will certainly obtain a very effective model, i.e., one that’s capable of recognizing with good certainty the handwritten number.

Get Rithik Alias’s stories in your inbox

Join Medium for free to get updates from this writer.

We are going to use SVM classifier to classify our data. Support vector machines (SVMs) are a set of supervised learning methods used for classification, regression and outliers detection. Many articles and blogs are available on support vector machines or SVMs.

So, estimator that is useful in this case is sklearn.svm.SVC, which uses the technique of Support Vector Classification (SVC). Thus, you have to import the svm module of the scikit-learn library. You can create an estimator of SVC type and then choose an initial setting, assigning the values C and gamma generic values. These values can then be adjusted in a different way during the course of the analysis.

from sklearn import svm

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

Then by fitting the training data to the svc model we train the model. Then by using the test data, we can test and print the classification report of the model.

1st we used 1000 data for training and remaining for testing. Then the classification report was as follows,

Classification report for 1000 training data

2nd we used 1500 data for training and remaining for testing. Then the classification report was as follows,

Classification report for 1500 training data

3rd we used 1700 data for training and remaining for testing. Then the classification report was as follows,

Classification report for 1700 training data

3rd we used 1791 data for training and remaining for testing. Then the classification report was as follows,

Classification report for 1791 training data

This time we got a 100% accuracy on testing.

Conclusion

1st when we used a ratio of 1000:797 on training data : testing data , we got an accuracy of 0.97.

2nd when we used a ratio of 1500:297 on training data : testing data , we got an accuracy of 0.95.

1st when we used a ratio of 1700:97 on training data : testing data , we got an accuracy of 0.98.

1st when we used a ratio of 1791:6 on training data : testing data , we got an accuracy of 1.00.

Inference

From the above conclusions we can infer that the hypothesis “The Digits data set of scikit-learn library predicts the digit accurately 95% of the times” is true since we got our accuracy greater than 95% in all 4 cases.

Reference

“I am thankful to mentors at https://internship.suvenconsultants.com for providing awesome problem statements and giving many of us a Coding Internship Exprience. Thank you www.suvenconsultants.com"

--

--

Rithik Alias
Rithik Alias

Written by Rithik Alias

Masters Student at IIITMK specialized in Data Analytics