Recognizing Handwritten Digits Using Scikit-Learn in Python

Shrusti Warekar
The Startup
Published in
4 min readOct 19, 2020

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. Classifying handwritten text or numbers is important for many real-world scenarios. For example, a postal service can scan postal codes on envelopes to automate grouping of envelopes which has to be sent to the same place. This article presents recognizing the handwritten digits (0 to 9) using the famous digits data set from Scikit-Learn, using a classifier called Logistic Regression.

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. Here we are going to analyze the digits data-set of the Sci-Kit learn library using Jupyter Notebook.

  • First we start with importing required Libraries:
  • Then understanding the dataset:
Data Understanding

The data files train.csv and test.csv contain gray-scale images of hand-drawn digits, from zero through nine.

Each image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total. Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255, inclusive.

  • Now we load the Digits dataset into the notebook. After loading the data we can read lots of information about the datasets by calling the DESCR attribute.

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.

  • Let’s visually check the contents of this result using the matplotlib library.
  • The numerical values represented by images, i.e. the targets, are contained in the digit.targets array.
Observed that dataset is a training set containing 1,797 images.

This dataset contains 1,797 elements, and so we can consider the first 1,791 as a training set and will use the last six as a validation set. Here we can see in detail these six handwritten digits by using the matplotlib library:

Confusion matrix

A confusion matrix is a table that is often used to evaluate the accuracy of a classification model. We can use Seaborn or Matplotlib to plot the confusion matrix. We will be using Seaborn for our confusion matrix.

Confusion Matrix

The non-linear model gives approx. 92.5% accuracy than of linear model 90.27% accuracy. Thus, going forward, let’s choose hyperparameters corresponding to non-linear models.

The evaluation metric for this contest is the categorization accuracy, or the proportion of test images that are correctly classified. For example, a categorization accuracy of 0.97 indicates that you have correctly classified all but 3% of the images.

  • Let’s predict the accuracy of our model using KNN classifier.

Conclusion

From this article, we can see how easy it is to import a dataset, build a model using Scikit-Learn, train the model, make predictions with it, and finding the accuracy of our prediction(which in our case is 98.33%). I hope this article helps you with your future endeavors!

Hope you like my article!!

--

--