Recognizing Handwritten Digits in Python Using Scikit-Learn
By: Soutrik Acharya
Tags: Handwriting Recognition, Scikit Learn, Digits Recognition

Handwritten Digits Recognition

In this blog we will discuss about how can we use scikit-learn to do machine learning classification on Digits dataset of handwritten digits. There are other Handwritten recognition datasets available, but here we are using Digits dataset.

Handwriting Recognition

Recognizing handwritten text is a problem that can be traced back to the first automatic machines that needed to recognize individual characters in handwritten documents. For example, the ZIP codes on letters at the post office and the automation needed to recognize these five digits. Perfect recognition of these codes is necessary in order to sort mail automatically and efficiently. Included among the other applications that may come to mind is OCR (Optical Character Recognition) software. OCR software must read handwritten text, or pages of printed books, for general electronic documents in which each character is well defined.

But the problem of handwriting recognition goes farther back in time, more precisely to the early 20th Century (1920s), when Emanuel Goldberg (1881–1970) began his studies regarding this issue and suggested that a statistical approach would be an optimal choice.

To address this issue in Python, the scikit-learn library provides a good example to better understand this technique, the issues involved, and the possibility of making predictions.

Lets start by first uploading the Digits dataset.

Importing Digits Dataset

Now we can play around with the Digits dataset. We can check the directory of the dataset and expand all the five.

Checking the Directory of the Digits Dataset

Now lets check the number of images and labels present in the dataset. We got 1797 images and 1797 labels in the Digits dataset.

The images of the handwritten digits are contained in a digits.images array. Each element of this array is an image that is represented by an 8x8 matrix of numerical values that correspond to a grayscale from white, with a value of 0, to black, with the value 15.

Flattening the Digits Dataset

Here the Digits dataset is reshaped into a 2 dimensional from 3 dimensional array. Flattening the Digits dataset is done like this..

Flattening the Digits Dataset

Splitting the Dataset into Training & Testing set

Let us now train our model by splitting the Digits dataset into 80% training set and 20% testing set. The main purpose of training and testing is to make sure that after we train our model, it is able to generalize well to new data and also overcoming overfitting the model.

Splitting the Dataset

Choosing a Classifier

Now we are using a Classifier, say K Nearest neighbor Classifier to train our model and then making an instance of the model. After training the model, lets store the information learned from the data. Now lets try to predict the labels of new data using the information we have gained from training the model.

Now its time to measure the performance of the model. There are various ways to measure the performance of the model. Here we’ll simply calculate the accuracy(Score) of our model.

Accuracy = Number of Correct Predictions / Total Number of Predictions

In my case, the Accuracy of the model is more than 95%.

Conclusion

We can easily conclude that by using Scikit-Learn library in python, data analysis becomes easy, effective and takes less time.

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