In [105]:
import numpy as np
k_w = 28
k_h = 28
def read_images(n):
    """
    Read images from the mnist handwritten digits dataset (60.000 images)
    """
    with open('/home/ch/ev3dev/digit-recognition-training-data/train-images.idx3-ubyte', 'r') as f:
        fstr = np.fromfile(f, np.dtype('>i4'), 2)        
        sz = np.fromfile(f, np.dtype('>i4'), 2)
        
        a = [np.fromfile(f, np.dtype('>u1'), k_w * k_h).astype(np.float64) / 255.0 * 2 -1 for i in xrange(0, n)]            
        f.close()
        return np.asarray(a)
    
def read_labels(n):    
    """
    Read images from the mnist handwritten digits dataset (60.000 images)
    """
    with open('/home/ch/ev3dev/digit-recognition-training-data/train-labels.idx1-ubyte', 'r') as f:        
        sz = np.fromfile(f, np.dtype('>i4'), 2)
        
        a = [np.fromfile(f, np.dtype('>u1'), 1)[0] for i in xrange(0, n)] 
        f.close()
        return np.asarray(a)

In [106]:
images = read_images(3000)
labels = read_labels(3000)
len(images)
len(labels)


Out[106]:
3000

In [107]:
import matplotlib.pyplot as plt
%matplotlib inline

In [108]:
img = images[0]
plt.imshow((255 - img).reshape(28, 28), cmap=plt.cm.gray, interpolation='none')


Out[108]:
<matplotlib.image.AxesImage at 0x7f233ea75890>

In [109]:


In [110]:
# Standard scientific Python imports
import matplotlib.pyplot as plt

# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics

# Create a classifier: a support vector classifier
classifier = svm.SVC(kernel="rbf", C=2.8, gamma=.0073)

n_samples = len(images)

# We learn the digits on the first half of the digits
classifier.fit(images[:n_samples / 2], labels[:n_samples / 2])

# Now predict the value of the digit on the second half:
expected = labels[n_samples / 2:]
predicted = classifier.predict(images[n_samples / 2:])

print("Classification report for classifier %s:\n%s\n"
      % (classifier, metrics.classification_report(expected, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))

images_and_predictions = list(zip([image.reshape(28, 28) for image in images[n_samples / 2:]], predicted, expected))
for index, (image, prediction, expected) in enumerate(images_and_predictions[:4]):
    plt.subplot(2, 4, index + 5)
    plt.axis('off')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title('Pred: %i, %i' % (prediction, expected))

plt.show()


Classification report for classifier SVC(C=2.8, cache_size=200, class_weight=None, coef0=0.0, degree=3,
  gamma=0.0073, kernel='rbf', max_iter=-1, probability=False,
  random_state=None, shrinking=True, tol=0.001, verbose=False):
             precision    recall  f1-score   support

          0       0.99      0.96      0.97       145
          1       0.99      0.99      0.99       167
          2       0.92      0.97      0.94       155
          3       0.99      0.92      0.95       153
          4       0.91      0.90      0.91       165
          5       0.88      0.99      0.93       137
          6       0.96      0.96      0.96       165
          7       0.94      0.95      0.94       152
          8       0.97      0.88      0.92       127
          9       0.91      0.91      0.91       134

avg / total       0.94      0.94      0.94      1500


Confusion matrix:
[[139   0   2   0   1   0   2   0   1   0]
 [  0 165   1   0   0   0   0   1   0   0]
 [  0   0 151   0   2   0   0   1   1   0]
 [  0   1   0 140   0   7   0   3   2   0]
 [  0   0   3   0 149   0   3   1   0   9]
 [  0   0   1   0   0 135   1   0   0   0]
 [  0   1   2   0   1   3 158   0   0   0]
 [  0   0   3   0   4   0   0 144   0   1]
 [  1   0   1   2   0   9   0   0 112   2]
 [  1   0   1   0   7   0   0   3   0 122]]

In [79]:


In [82]:
# Original
#
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
# License: BSD 3 clause

# Standard scientific Python imports
import matplotlib.pyplot as plt

# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics

# The digits dataset
digits = datasets.load_digits()

# The data that we are interested in is made of 8x8 images of digits, let's
# have a look at the first 3 images, stored in the `images` attribute of the
# dataset.  If we were working from image files, we could load them using
# pylab.imread.  Note that each image must have the same size. For these
# images, we know which digit they represent: it is given in the 'target' of
# the dataset.
images_and_labels = list(zip(digits.images, digits.target))
for index, (image, label) in enumerate(images_and_labels[:4]):
    plt.subplot(2, 4, index + 1)
    plt.axis('off')    
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title('Training: %i' % label)

# To apply a classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))

# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001)

# We learn the digits on the first half of the digits
classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2])

# Now predict the value of the digit on the second half:
expected = digits.target[n_samples / 2:]
predicted = classifier.predict(data[n_samples / 2:])

print("Classification report for classifier %s:\n%s\n"
      % (classifier, metrics.classification_report(expected, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))

images_and_predictions = list(zip(digits.images[n_samples / 2:], predicted))
for index, (image, prediction) in enumerate(images_and_predictions[:4]):
    plt.subplot(2, 4, index + 5)
    plt.axis('off')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title('Prediction: %i' % prediction)

plt.show()


Classification report for classifier SVC(C=1.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):
             precision    recall  f1-score   support

          0       1.00      0.99      0.99        88
          1       0.99      0.97      0.98        91
          2       0.99      0.99      0.99        86
          3       0.98      0.87      0.92        91
          4       0.99      0.96      0.97        92
          5       0.95      0.97      0.96        91
          6       0.99      0.99      0.99        91
          7       0.96      0.99      0.97        89
          8       0.94      1.00      0.97        88
          9       0.93      0.98      0.95        92

avg / total       0.97      0.97      0.97       899


Confusion matrix:
[[87  0  0  0  1  0  0  0  0  0]
 [ 0 88  1  0  0  0  0  0  1  1]
 [ 0  0 85  1  0  0  0  0  0  0]
 [ 0  0  0 79  0  3  0  4  5  0]
 [ 0  0  0  0 88  0  0  0  0  4]
 [ 0  0  0  0  0 88  1  0  0  2]
 [ 0  1  0  0  0  0 90  0  0  0]
 [ 0  0  0  0  0  1  0 88  0  0]
 [ 0  0  0  0  0  0  0  0 88  0]
 [ 0  0  0  1  0  1  0  0  0 90]]

In [100]:
print type(digits.images[:1][0][0])
print type(images[:1][0][0])


<type 'numpy.ndarray'>
<type 'numpy.float64'>

In [85]:
len(data)


Out[85]:
1797

In [ ]: