Wednesday, March 9, 2016

Python Data Analysis 11 - Recognizing Handwritten Digits

Recognizing Handwritten Digits

-- Handwriting Recognition

-- Recognizing Handwritten Digits with scikit-learn

ipython notebook
from sklearn import svm
svc = svm.SVC(gamma=0.001, C=100.)

from sklearn import datasets
digits = datasets.load_digits()

print digits.DESCR

digits.images[0]
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.]])

import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(digits.images[0], cmap=plt.cm.gray_r, interpolation=’nearest’)

digits.target
array([0, 1, 2, ..., 8, 9, 8])
digits.target.size

-- Learning and Predicting

import matplotlib.pyplot as plt
%matplotlib inline

plt.subplot(321)
plt.imshow(digits.images[1791], cmap=plt.cm.gray_r, interpolation=’nearest’)
plt.subplot(322)
plt.imshow(digits.images[1792], cmap=plt.cm.gray_r, interpolation=’nearest’)
plt.subplot(323)
plt.imshow(digits.images[1793], cmap=plt.cm.gray_r, interpolation=’nearest’)
plt.subplot(324)
plt.imshow(digits.images[1794], cmap=plt.cm.gray_r, interpolation=’nearest’)
plt.subplot(325)
plt.imshow(digits.images[1795], cmap=plt.cm.gray_r, interpolation=’nearest’)
plt.subplot(326)
plt.imshow(digits.images[1796], cmap=plt.cm.gray_r, interpolation=’nearest’)

svc.fit(digits.data[1:1790], digits.target[1:1790])

SVC(C=100.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
  gamma=0.001, kernel=’rbf’, max_iter=-1, probability=False,
  random_state=None, shrinking=True, tol=0.001, verbose=False)

svc.predict(digits.data[1791:1976])

array([4, 9, 0, 8, 9, 8])

digits.target[1791:1976]

array([4, 9, 0, 8, 9, 8])

-- Writing Mathematical Expressions with LaTeX

import matplotlib.pyplot as plt
%matplotlib inline
plt.title(r’$\alpha > \beta$’)

from IPython.display import display, Math, Latex
display(Math(r’\mathrm{Roman}’))
display(Math(r’\mathit{Italic}’))
display(Math(r’\mathtt{Typewriter}’))

display(Math(r’\mathcal{CALLIGRAPHY}’))

No comments:

Post a Comment

Blog Archive