Cracking the handwritten digits recognition problem with Scikit-learn
By: Ariel Segura
Tags: Machine Learning, Python, Data Science, Software Engineering, Scikit Learn

Sklearn Hello World!

The example we’ll run is pretty simple: learn to recognize digits. Given a dataset of digits, learn the shape of them and predict unseen digits.

This example is based on the Sklearn basic tutorial.

Verify your Python configuration

Before we move forward, just run a simple Python file to make sure you have configured everything properly.

Open PyCharm Create a new project Create a Python file Add the following line into it:

print("Running Sklearn Hello World!") Run the file. You should see that string in the console.

Import datasets

Sklearn has some built-in datasets that allow you to get started quickly. You could download the dataset from somewhere else if you want to, but in this blog, we’ll use Sklearn’s datasets.

Note: How digits are transformed from images into pixels is out of the scope of this blog. Assume that someone did a transformation to get pixels from scanned images, and that’s your dataset.

Edit your Python file and before the print command, add the following import:

from sklearn import datasets

Explore the dataset:

digits = datasets.load_digits()



print(digits.data)

3. Run your Python file. You should see the following output in the console:

[[ 0. 0. 5. ... 0. 0. 0.]

[ 0. 0. 0. ... 10. 0. 0.]

[ 0. 0. 0. ... 16. 9. 0.]

...

[ 0. 0. 1. ... 6. 0. 0.]

[ 0. 0. 2. ... 12. 0. 0.]

[ 0. 0. 10. ... 12. 1. 0.]]

What you’re seeing in that output are all the digits (or instances) and all their features that each instance has. In this example, the pixels of each digit. If we printed the value digits.target instead, we would see the real values (classifications) for those digits: array([0, 1, 2, …, 8, 9, 8]).

Features are attributes about an instance. A person may have attributes like nationality, skills, etc. Instead of calling them attributes, they’re called features. In our case, our instances (digits) has the brightness levels of each pixel as attributes or features.

Learn from our dataset

ML is about generalizing the behavior of our dataset. It’s like taking a look at the data and saying something like “yes, it seems that next month we’ll increase our sales”. That’s because based on what happened, you’re trying to generalize the situation and predict what may happen in the future.

There are basically two ways of generalizing from data:

Learning by heart: this means “memorizing” all the instances and then try to match new instances to the ones we knew. A good example of this is explained in [1]: If we had to implement a spam filter, one way could be flagging all emails that are identical to emails already flagged as spam. The similarity between emails could be the number of words they have in common with a known spam email. Building a model to represent data: this implies building a model that approximates known values with unseen values. The general idea is that if we know that instances A and B are similar and A has a target value 1, then we can guess that B may have a target value 1 as well. The difference with the first approach is that by building a model, we’re adjusting it to represent the data and then we forget about the instances.

A cat-dogs classifier. In our case, we’ll classify by digit: 0, 1, 2, etc. Source

Let’s create a model that represents our data behavior. As this is a classification problem (given some instances, we want to classify them based on their features and predict the digit they represent), we will call our component classifier and we’ll choose a Support Vector Machine (SVM). There are many other classifiers in Sklearn, but this one will be enough for our use case. For further details on when to use certain components depending on the problem, you can follow the following cheat-sheet: