Recognising Handwritten Digits
In this article we will be train a model which recognize handwritten digits using scikit-learn and Support Vector Machines.
Importing scikit-learn as it contains the dataset.
import sklearn
Importing required dataset: The handwritten dataset is present in sklearn under datasets module. We can also use dataset available on kaggle or any other platform, but for the sake of simplicity we were using this dataset.
from sklearn import datasets
data = datasets.load_digits()
The dataset has two parts one containing images and other has labels. data.images contain all the images while data.target contain all labels. Let’s see how many images do we have.
print(len(data.images))
print(len(data.target))
Let’s give the names to our dataset as images and labels.
images = data.images
labels = data.target
Reshaping the images.
images = images.reshape((images.shape[0], -1))
images.shape
Now, we will visualize the images using matplotlib which is an awesome python library for begineers. Let’s see what is our first image.
import matplotlib.pyplot as plt
plt.gray()
imgplot = plt.imshow(data.images[0])
print(“label: “,data.target[0])
plt.show()
importing SVM
from sklearn import svm
model = svm.SVC(gamma = 0.001)
splitting the data into train and test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.33, random_state=42)
let’s fit our model
model.fit(X_train, y_train)
Let’s check the training accuracy and see how our model performs on the training data:
score = model.score(X_test, y_test)
It will give → 0.98989898989899
Wow! it gives 98.989% of accuracy.
Let’s visualize the output along with it’s label.
plt.gray()
test_img = X_test[8].reshape(8,8)
imgplot = plt.imshow(test_img)
print(“label: “,y_test[8])
plt.show()
t = X_test[5].reshape(1,-1)
pred = model.predict(t)
print(“prediction: “,pred)
Clearly, it recognizes the handwritten 5…
We can also try other models as well, which may give more accuracy.
So, this is the end of our simplest model.
here’s the link of my Github repository containing ipynb file :-
https://github.com/Kartik-Khandelwal/HandwrittenDigitRecognizer
I am thankful to the 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 for providing such opportunity.