In [1]:
%matplotlib inline
In [36]:
# Standard scientific Python imports
import matplotlib.pyplot as plt
import numpy as np
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
from sklearn.neighbors import KNeighborsClassifier, NearestNeighbors
from ipywidgets import interact_manual
In [3]:
# The digits dataset
digits = datasets.load_digits()
In [4]:
# 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.
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)
plt.show()
In [6]:
# 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))
In [9]:
# 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])
Out[9]:
In [13]:
# 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))
In [14]:
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()
In [7]:
neigh = None
@interact_manual(k=(1, 20))
def make_nn_classifier(k):
global neigh
neigh = KNeighborsClassifier(n_neighbors=k)
print('training...')
neigh.fit(data[:n_samples // 2], digits.target[:n_samples // 2])
print('done!')
print(neigh)
In [11]:
# Now predict the value of the digit on the second half:
expected = digits.target[n_samples // 2:]
predicted = neigh.predict(data[n_samples // 2:])
print("Classification report for classifier %s:\n%s\n"
% (neigh, metrics.classification_report(expected, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
In [12]:
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()
In [41]:
neigh = None
kk = None
rr = None
algo = None
@interact_manual(k=(1, 20),
r=(0, 50, 0.1),
algorithm=['auto', 'ball_tree', 'kd_tree', 'brute'])
def make_knn(k, r, algorithm):
global neigh
global kk, rr, algo
kk, rr, algo = k, r, algorithm
neigh = NearestNeighbors(n_neighbors=k, radius=r, algorithm=algorithm)
print('training...')
neigh.fit(data[:n_samples // 2])
print('done!')
print(neigh)
In [42]:
# Now predict the value of the digit on the second half:
distances, indices = neigh.kneighbors(data[n_samples // 2:], 4)
In [43]:
distances
Out[43]:
In [44]:
indices
Out[44]:
In [40]:
neigh.kneighbors_graph(data[n_samples // 2:], n_neighbors=kk).toarray()
Out[40]:
In [32]:
help(neigh.kneighbors_graph)