|
| 1 | +""" |
| 2 | +=================================================================== |
| 3 | +Recognizing hand-written digits using Fastfood kernel approximation |
| 4 | +=================================================================== |
| 5 | +
|
| 6 | +This shows how the Fastfood kernel approximation compares to a dual and primal |
| 7 | +support vector classifier. It is based on the plot_digits_classification |
| 8 | +example of scikit-learn. The idea behind Fastfood is to map the data into a |
| 9 | +feature space (approximation) and then run a linear classifier on the mapped |
| 10 | +data. |
| 11 | +
|
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | +print(__doc__) |
| 16 | + |
| 17 | +# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org> |
| 18 | +# Modified By: Felix Maximilian Möller |
| 19 | +# License: Simplified BSD |
| 20 | + |
| 21 | +# Standard scientific Python imports |
| 22 | +import numpy as np |
| 23 | +import pylab as pl |
| 24 | + |
| 25 | +# Import datasets, classifiers and performance metrics |
| 26 | +from sklearn import datasets, svm, metrics |
| 27 | + |
| 28 | +from sklearn_extra.kernel_approximation import Fastfood |
| 29 | + |
| 30 | +# The digits dataset |
| 31 | +digits = datasets.load_digits() |
| 32 | + |
| 33 | +# The data that we are interested in is made of 8x8 images of digits, |
| 34 | +# let's have a look at the first 3 images, stored in the `images` |
| 35 | +# attribute of the dataset. If we were working from image files, we |
| 36 | +# could load them using pylab.imread. For these images know which |
| 37 | +# digit they represent: it is given in the 'target' of the dataset. |
| 38 | +for index, (image, label) in enumerate(zip(digits.images, digits.target)): |
| 39 | + pl.subplot(2, 4, index + 1) |
| 40 | + pl.axis('off') |
| 41 | + pl.imshow(image, cmap=pl.cm.gray_r, interpolation='nearest') |
| 42 | + pl.title('Training: %i' % label) |
| 43 | + if index > 3: |
| 44 | + break |
| 45 | + |
| 46 | +# To apply an classifier on this data, we need to flatten the image, to |
| 47 | +# turn the data in a (samples, feature) matrix: |
| 48 | +n_samples = len(digits.images) |
| 49 | +data = digits.images.reshape((n_samples, -1)) |
| 50 | +gamma = .001 |
| 51 | +sigma = np.sqrt(1 / (2 * gamma)) |
| 52 | +number_of_features_to_generate = 1000 |
| 53 | +train__idx = range(n_samples // 2) |
| 54 | +test__idx = range(n_samples // 2, n_samples) |
| 55 | + |
| 56 | +# map data into featurespace |
| 57 | +rbf_transform = Fastfood( |
| 58 | + sigma=sigma, n_components=number_of_features_to_generate) |
| 59 | +data_transformed_train = rbf_transform.fit_transform(data[train__idx]) |
| 60 | +data_transformed_test = rbf_transform.transform(data[test__idx]) |
| 61 | + |
| 62 | +# Create a classifier: a support vector classifier |
| 63 | +classifier = svm.SVC(gamma=gamma) |
| 64 | +linear_classifier = svm.LinearSVC() |
| 65 | +linear_classifier_transformation = svm.LinearSVC() |
| 66 | + |
| 67 | +# We learn the digits on the first half of the digits |
| 68 | +classifier.fit(data[train__idx], digits.target[train__idx]) |
| 69 | +linear_classifier.fit(data[train__idx], digits.target[train__idx]) |
| 70 | + |
| 71 | +# Run the linear classifier on the mapped data. |
| 72 | +linear_classifier_transformation.fit( |
| 73 | + data_transformed_train, digits.target[train__idx]) |
| 74 | + |
| 75 | +# Now predict the value of the digit on the second half: |
| 76 | +expected = digits.target[test__idx] |
| 77 | +predicted = classifier.predict(data[test__idx]) |
| 78 | +predicted_linear = linear_classifier.predict(data[test__idx]) |
| 79 | +predicted_linear_transformed = linear_classifier_transformation.predict( |
| 80 | + data_transformed_test) |
| 81 | + |
| 82 | +print("Classification report for dual classifier %s:\n%s\n" |
| 83 | + % (classifier, metrics.classification_report(expected, predicted))) |
| 84 | +print("Classification report for primal linear classifier %s:\n%s\n" |
| 85 | + % (linear_classifier, |
| 86 | + metrics.classification_report(expected, predicted_linear))) |
| 87 | +print( |
| 88 | + "Classification report for primal transformation classifier %s:\n%s\n" |
| 89 | + % (linear_classifier_transformation, |
| 90 | + metrics.classification_report(expected, predicted_linear_transformed))) |
| 91 | + |
| 92 | +print("Confusion matrix for dual classifier:\n%s" |
| 93 | + % metrics.confusion_matrix(expected, predicted)) |
| 94 | +print("Confusion matrix for primal linear classifier:\n%s" |
| 95 | + % metrics.confusion_matrix(expected, predicted_linear)) |
| 96 | +print("Confusion matrix for for primal transformation classifier:\n%s" |
| 97 | + % metrics.confusion_matrix(expected, predicted_linear_transformed)) |
| 98 | + |
| 99 | +for index, (image, prediction) in enumerate( |
| 100 | + zip(digits.images[test__idx], predicted)): |
| 101 | + pl.subplot(2, 4, index + 4) |
| 102 | + pl.axis('off') |
| 103 | + pl.imshow(image, cmap=pl.cm.gray_r, interpolation='nearest') |
| 104 | + pl.title('Prediction: %i' % prediction) |
| 105 | + if index > 3: |
| 106 | + break |
| 107 | + |
| 108 | +pl.show() |
0 commit comments