Recognizing Handwritten Digits with Scikit-Learn



Recognizing handwritten text is a problem that traces back to the first automatic machines that needed to recognize individual characters in handwritten documents. Think about, 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 to sort mail automatically and efficiently. Included among the other applications that may come to mind is OCR (Optical Character Recognition) software. OCR software must read handwritten text, or pages of printed books, for general electronic documents in which each character is well defined. But the problem of handwriting recognition goes farther back in time, more precisely to the early 20th Century (the 1920s), when Emanuel Goldberg (1881–1970) began his studies regarding this issue and suggested that a statistical approach would be an optimal choice.

To address this issue in Python, the scikit-learn library provides a good example to better understand this technique, the issues involved, and the possibility of making predictions.

scikit-learn library:

Aim:

we will have an estimator with the task of learning through a fit() function, and once it has reached a degree of predictive capability (a model sufficiently valid), it will produce a prediction with the predict() function. Then we will discuss the training set and validation set created this time from a series of images.

The hypothesis to be tested:

The Digits Dataset

Implementation :

Import libraries

this project uses python libraries numpy, sklearn, matplotlib.


Load digit dataset as dig_data

Scikit-learn provides numerous datasets. And the digits dataset is one of those, which contains set of around 1800 images each sizes 8x8 pixels in size. We get more information related to our dataset by using DESCR method of dataset.



Lets get to know more about the dataset dig_data and what it consists of. This dataset is Bunch type object. Bunch object is similar to a dictionary with multiple values for a specific key. The important keys of dig_data are images(bits of image data), target(the true value of image), target_names(different allowed outputs).


The image data sample from dig_data dataset both in raw bits format and image format.


Seperate the dig_data.images and assign it to digits variable. The digits is reshaped into a 2-dim array.



Now the input to the classifier model digits(dig_data.images) as X and output dig_data.target as Y are split into two parts the train_data and test_data in the ratio 7:3 using sklearn.model_selection.train_test_split.

Now the input to the classifier model digits(dig_data.images) as X and output dig_data.target as Y are split into two parts the train_data and test_data in the ratio 7:3 using sklearn.model_selection.train_test_split.

The classifier that is helpful in this scenario is sklearn.svm.SVC, a support vector machine. Support Vector Machine (SVM) is a supervised machine learning algorithm that is mostly used in classification problems.

Define the SVC classifier as model and train model using model.fit() method with X_train, Y_train as inputs. After training the model, check the score of the model and accuracy_score from sklearn.metrics.



To evaluate the accuracy of a classification model a confusion matrix is used. A confusion matrix is a table that shows the prediction results in X-axis with respect to true values in Y-axis. The below plot is constructed using plot_confusion_matrix function in sklearn.metrics.

confusion_matrix
Confusion matrix

Conclusion

After performing the data analysis on the dataset with three different test cases, we can conclude that the given hypothesis is true i.e., the model predicts the digit 95% accurately.

Source code: https://github.com/mukunddholariya/suvenconsultants/blob/main/Recognizing%20Handwritten%20Digits%20with%20scikit-learn/Recognizing_Handwritten_Digits_with_Scikit_Learn.ipynb

I am thankful to mentors at https://internship.suvenconsultants.com for providing awesome problem statements and giving many of us a Coding Internship Experience. Thank you www.suvenconsultants.com

Comments

Popular posts from this blog