12/7/2016 PyRevolution/Handwriting_Recognition.
ipynb at master · snazrul1/PyRevolution · GitHub
Handwriting Recognition
Author: Syed (Sadat) Nazrul
Import libraries and dataset from ScikitLearn
In [13]: import [Link] as plt
%matplotlib inline
from [Link] import load_digits
digits = load_digits()
Analyze a sample image
In [14]: import pylab as pl
[Link]()
[Link]([Link][0])
[Link]()
<[Link] at 0x7f1b8c186550>
Analyze image pixels
Each element represents the pixel of our greyscale image. The value ranges from 0 to 255 for an 8 bit pixel.
In [15]: [Link][0]
Out[15]: array([[ 0., 0., 5., 13., 9., 1., 0., 0.],
[ 0., 0., 13., 15., 10., 15., 5., 0.],
[ 0., 3., 15., 2., 0., 11., 8., 0.],
[ 0., 4., 12., 0., 0., 8., 8., 0.],
[ 0., 5., 8., 0., 0., 9., 8., 0.],
[ 0., 4., 11., 0., 1., 12., 7., 0.],
[ 0., 2., 14., 5., 10., 12., 0., 0.],
[ 0., 0., 6., 13., 10., 0., 0., 0.]])
Visualize first 15 images
In [16]: images_and_labels = list(zip([Link], [Link]))
[Link](figsize=(5,5))
for index, (image, label) in enumerate(images_and_labels[:15]):
[Link](3, 5, index + 1)
[Link]('off')
[Link](image, cmap=[Link].gray_r, interpolation='nearest')
[Link]('%i' % label)
[Link] 2/4
12/7/2016 PyRevolution/Handwriting_Recognition.ipynb at master · snazrul1/PyRevolution · GitHub
In [17]: import random
from sklearn import ensemble
#Define variables
n_samples = len([Link])
x = [Link]((n_samples, ‐1))
y = [Link]
#Create random indices
sample_index=[Link](range(len(x)),len(x)/5) #20‐80
valid_index=[i for i in range(len(x)) if i not in sample_index]
#Sample and validation images
sample_images=[x[i] for i in sample_index]
valid_images=[x[i] for i in valid_index]
#Sample and validation targets
sample_target=[y[i] for i in sample_index]
valid_target=[y[i] for i in valid_index]
#Using the Random Tree Classifier
classifier = [Link]()
#Fit model with sample data
[Link](sample_images, sample_target)
#Attempt to predict validation data
score=[Link](valid_images, valid_target)
print 'Random Tree Classifier:\n'
print 'Score\t'+str(score)
Random Tree Classifier:
Score 0.889429763561
In [20]: i=150
[Link]()
[Link]([Link][i])
[Link]()
[Link](x[i])
<[Link] at 0x7f1b804b0cd0>
Out[20]: array([0])
© 2016 GitHub, Inc. Terms Privacy Security Status Help Contact GitHub API Training Shop Blog About
[Link] 3/4