Handwritten Digit Recognition using scikit-learn

Dixitritik
4 min readApr 28, 2021

This article delineates the Recognition of Handwritten Digits using scikit-learn. Handwritten digit recognition is the ability of a computer to recognize the human handwritten digits from different sources like images, papers, touch screens and classify them into 10 predefined classes (0–9). Digit recognition has many applications like number plate recognition, postal mail sorting, bank check processing, etc. This work has been performed in order to accept or reject the Hypothesis state below:

“The Digits data set of the 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.”

An estimator that is useful in this case is sklearn.svm.SVC, which uses the technique of Support Vector Classification (SVC).

Support Vector Machines

Support Vector Machine (SVM) is a supervised machine learning algorithm. There is generally plotting of data items in n-dimensional space where n is the number of features, a particular coordinate represents the value of a feature, we perform the classification by finding the hyperplane that distinguishes the two classes. It will choose the hyperplane that separates the classes correctly.

This image describes the working mechanism of SVM Classification with supporting vectors and hyperplanes.

Dataset

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

Implementation

1. Importing Libraries and Loading the Dataset

This data analysis task is performed on Google Colab. The initial step is to import all the required libraries and the dataset (Digits dataset).

2. Analyze the Dataset

This step focuses on describing and viewing the data in order to have a better understanding of the dataset. The shape method is used to return a tuple of shape (Rows, columns) of the data frame. The numerical values represented by images, i.e., the targets, are contained in the ds.targets array. The size method returns the size of the data frame which is equivalent to the total number of elements.

The images of the handwritten digits are contained in a ds.images array. Each element of this array is an image that is represented by an 8x8 matrix of numerical values that correspond to grayscale from white, with a value of 0, to black, with the value 15. Further, some elements of the digit dataset are visualized using the Matplotlib library.

3. Defining & Training the model

In this step, an estimator of SVC type is defined with some initial setting (assigning the values of C and gamma).

Next is the learning process where the model train itself with a training set, which is a set of data in which we already know the belonging class (or the output). We split the data into two parts: Training and Testing set. I have tested the model for 3 cases (each case for a different range of training and validation sets).

  1. Considering the first 1,787 as a training set.

4. Testing the model

Now we are testing our estimator for which we need to decide the testing set. I have used the last 9 as a validation set.

2. Considering the first 1,700 as a training set and the last 95 as a validation set.

3. Considering the first 1,650 as a training set and the last 145 as a validation set.

5. Visualizing the result

Conclusion

We obtained accuracies of more than 95% on all the three different datasets, thus the Hypothesis to be tested is accurate.

--

--