Recognizing Handwritten Digits with scikit-learn in Python

Vivek VR
4 min readSep 30, 2021

--

Handwritten digit recognition is an ability of machines to recognize human written digits or numbers. OCR[Optical Character Recognition] is one of the examples of handwritten text recognition. It is quite difficult for machines to understand or recognize human written text or numbers. The solution for this problem is programs like OCR or handwritten digit recognition programs. These programs takes image as input and converts them into machine understandable bits. And with the help of these bits, the computer recognizes the digits present in the image.

Handwritten digits
Handwritten digits

This article presents recognition of handwritten digits [0 to 9] using the digits dataset from scikit-learn, using a classifier called SVC[Support Vector Classifier]. And scikit-learn library provides a good example to better understand the issue involved. You can access the example here.

Aim :

The main aim of this project includes predicting numerical value interpreted from an image of hand written form.

Here, we will create model tasked to learn from the digit dataset of scikit-learn module. We use the fit function of the model to train it and predict function to predict the output for any given input. We train our model till it reaches a degree of prediction accuracy.

Hypothesis to be tested:

The Digits data set of the scikit-learn library provides numerous datasets 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. Perform data Analysis to accept or reject this Hypothesis.

Implementation :

Import libraries

this project uses python libraries numpy, sklearn, matplotlib.

Library
Libraries to input

Load digit dataset as dig_data

dig_data

Scikit-learn provides numerous datasets. And the digits dataset is one of those, which contains set of around 1800 images each sizes 8x8 pixels in size. We get more information related to our dataset by using DESCR method of dataset.

dig_data.DESCR

Lets get to know more about the dataset dig_data and what it consists of. This dataset is Bunch type object. Bunch object is similar to a dictionary with multiple values for a specific key. The important keys of dig_data are images(bits of image data), target(the true value of image), target_names(different allowed outputs).

dataset_info

The image data sample from dig_data dataset both in raw bits format and image format.

Seperate the dig_data.images and assign it to digits variable. The digits is reshaped into a 2-dim array.

Now the input to the classifier model digits(dig_data.images) as X and output dig_data.target as Y are split into two parts the train_data and test_data in the ratio 7:3 using sklearn.model_selection.train_test_split.

train_test_split

The classifier that is helpful in this scenario is sklearn.svm.SVC, a support vector machine. Support Vector Machine (SVM) is a supervised machine learning algorithm that is mostly used in classification problems.

Define the SVC classifier as model and train model using model.fit() method with X_train, Y_train as inputs. After training the model, check the score of the model and accuracy_score from sklearn.metrics.

model training

To evaluate the accuracy of a classification model a confusion matrix is used. A confusion matrix is a table that shows the prediction results in X-axis with respect to true values in Y-axis. The below plot is constructed using plot_confusion_matrix function in sklearn.metrics.

confusion_matrix
Confusion matrix

Conclusion

After performing the data analysis on the dataset with three different test cases, we can conclude that the given hypothesis is true i.e., the model predicts the digit 95% accurately.

You can get the source code of the project here.

--

--

Vivek VR

Machine Learning Enthusiast. Writes some stuff….