Handwritten Digit Recognition on MNIST dataset
By: Ishika Tailor
Tags: Logistic Regression, Data Science, Machine Learning, Digit Recognition

In this Blog, I will explain basic digit recognition using Logistic Regression as well as LinearSVC. But note that there are many classification algorithms( SGD, SVM, RandomForest, etc) which can be trained on this dataset including deep learning algorithms (CNN).

Let’s understand from basic and analyse the accuracy of both method.

MNIST Dataset

What is MNIST?

MNIST is a dataset of 70,000 images of digit handwritten by high school students and employees of the US Census Bureau. All images are labelled with the respective digit they represent. MNIST is the hello world of machine learning.

There are 70,000 images and each image has 784 (28*28) features. Each image is 28*28 pixels, and each feature simply represents one pixel’s intensity from 0 (white) to 255 (black).

28*28=784 pixels [ 1 D array ]

Step:1 Import Dataset

One thing I want to share is while importing MNIST dataset, I have faced one error shown in below image. This happen because as time change some method will also change. So I solved it by typing fetch_openml(‘mnist_784’)

Error while Load Dataset

Successfully Load Data

Step: 2 Analyse the Data

Here, Data is 1D array which size is 784.

Data [70,000 1D array]

Label is in object format. So, for build predictions of digit, we have to convert it into integer format.

Label of Each Image

Shape:

Step: 3 Let’s plot one 1D array on plot.

I have taken 2600th image of dataset.

Note that → Here, interpolation=’nearest’ simply displays an image without trying to interpolate between pixels if the display resolution is not the same as the image resolution. It will result an image in which pixels are displayed as a square of multiple pixels.

There is no relation between interpolation=’nearest’ and the grayscale image being displayed in color. By default imshow uses the jet colormap to display an image. If you want it to be displayed in greyscale, call the gray() method to select the gray colormap.

Label of 2600th image is ‘9’ and we can also see clearly.

Step: 4 Split the Dataset in train and test

Case: 1 By LinearSVC()

Let’s consider first 5000 images because if I train my model on 70,000 images then it will be very time consuming. You can try shuffle the Data for better accuracy of model.

Since there are 10 digits (0–9), we need a multi-class classifier. The Linear SVM that comes with sklearn can perform multi-class classification.

Prediction of Data

As we can see that the svc estimator has learned correctly. It is able to recognize the handwritten digits, interpreting correctly all nine digits of the validation set.

Case: 2 Create a 9 detector model using Logisctic regression

[ choose_one_digit=2600]

We can conclude that predicted value for this image nine is true.

Step: 5 Calculate Accuracy of both model

a is calculation of linearSVC and b is calculation of Logistic regression.

Output:

Accuracy of LinearSVC

Accuracy of Logistic regression for 9 digit

Conclusion: Average Accuracy of LinearSCV= 0.82,Average Accuracy of Logistic Regression= 0.93

GithHub Link with description : digit_recognition_mnistdata.ipynb

I am thankful to mentors at suvenconsultants for providing awesome problem statements and giving many of us a Coding Internship Exprience. Thank you Suvenconsultants.