In [19]:
print(__doc__)

# 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 4 images, stored in the `images` attribute of the
# dataset.  If we were working from image files, we could load them using
# matplotlib.pyplot.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.
n_samples = len(digits.images) // 80

digitsToShow = 10;
digitsToTrainOn = 100;  # Must be less than n_samples


images_and_labels = list(zip(digits.images, digits.target))
print()
print('Training samples (%i of %i are shown)' % (digitsToShow, digitsToTrainOn))
for index, (image, label) in enumerate(images_and_labels[:digitsToShow]):
    plt.subplot(2, digitsToShow, index + 1)
    plt.axis('off')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title('%i' % label)
plt.show()

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

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

# We learn the digits at the end of the data set
classifier.fit(data[-digitsToTrainOn:], digits.target[-digitsToTrainOn:])

print(digits.target[-digitsToTrainOn:])

# Now predict the value of the digits at the start of the data set
predicted = classifier.predict(data[digitsToShow:])

# 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))

print("Predictions (%i shown)" % (digitsToShow))
images_and_predictions = list(zip(digits.images[digitsToShow:], predicted))
for index, (image, prediction) in enumerate(images_and_predictions[digitsToShow:]):
    plt.subplot(2, digitsToShow, index + 1)
    plt.axis('off')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title('%i' % prediction)

plt.show()


Automatically created module for IPython interactive environment

Training samples (10 of 100 are shown)
[0 9 5 5 6 5 0 9 8 9 8 4 1 7 7 3 5 1 0 0 2 2 7 8 2 0 1 2 6 3 3 7 3 3 4 6 6
 6 4 9 1 5 0 9 5 2 8 2 0 0 1 7 6 3 2 1 7 4 6 3 1 3 9 1 7 6 8 4 3 1 4 0 5 3
 6 9 6 1 7 5 4 4 7 2 8 2 2 5 7 9 5 4 8 8 4 9 0 8 9 8]
Predictions (10 shown)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-b12338dd2b9f> in <module>()
     58 images_and_predictions = list(zip(digits.images[digitsToShow:], predicted))
     59 for index, (image, prediction) in enumerate(images_and_predictions[digitsToShow:]):
---> 60     plt.subplot(2, digitsToShow, index + 1)
     61     plt.axis('off')
     62     plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')

/Users/clearwhale/anaconda/lib/python3.6/site-packages/matplotlib/pyplot.py in subplot(*args, **kwargs)
   1042 
   1043     fig = gcf()
-> 1044     a = fig.add_subplot(*args, **kwargs)
   1045     bbox = a.bbox
   1046     byebye = []

/Users/clearwhale/anaconda/lib/python3.6/site-packages/matplotlib/figure.py in add_subplot(self, *args, **kwargs)
   1018                     self._axstack.remove(ax)
   1019 
-> 1020             a = subplot_class_factory(projection_class)(self, *args, **kwargs)
   1021 
   1022         self._axstack.add(key, a)

/Users/clearwhale/anaconda/lib/python3.6/site-packages/matplotlib/axes/_subplots.py in __init__(self, fig, *args, **kwargs)
     62                     raise ValueError(
     63                         "num must be 1 <= num <= {maxn}, not {num}".format(
---> 64                             maxn=rows*cols, num=num))
     65                 self._subplotspec = GridSpec(rows, cols)[int(num) - 1]
     66                 # num - 1 for converting from MATLAB to python indexing

ValueError: num must be 1 <= num <= 20, not 21

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: