Recognizing Handwritten Digits with Scikit-Learn

Aditya Bhandari
Oct 3 · 3 min read

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 the 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.

Image for post
Image for post

Scikit-Learn is a library for Python that contains numerous useful algorithms that can easily be implemented and altered for the purpose of classification and other machine learning tasks.

Prerequisites

If you already have Jupyter notebook and all the necessary python libraries and packages installed you are ready to get started.

If not you can use Google colab too!

Let us start by importing our libraries

Image for post
Image for post

Visualizing the images and Training

Image for post
Image for post

To use a classifier we have to Flatten the image

Image for post
Image for post

Create a classifier: a support vector classifier

Image for post
Image for post

Split data into train and test subsets

Now predict the value of the digit

Image for post
Image for post

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 for Matplotlib our confusion matrix.

Image for post
Image for post
Image for post
Image for post

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 97.11%). I hope this article helps you with your future endeavors!

Thank you for reading my article!

For The Source code, Click here

Follow me:-

Twitter

Instagram

GitHub

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Aditya Bhandari

Written by

Data Scientist to be. IG - @adityabhandariii twitter - @Adityabhndari

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Aditya Bhandari

Written by

Data Scientist to be. IG - @adityabhandariii twitter - @Adityabhndari

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store