In this section we'll apply scikit-learn to the classification of handwritten digits. This will go a bit beyond the iris classification we saw before: we'll discuss some of the metrics which can be used in evaluating the effectiveness of a classification model, see an example of K-fold cross-validation, and present a more involved exercise.
We'll work with the handwritten digits dataset which we saw in an earlier section of the tutorial.
from sklearn.datasets import load_digits
digits = load_digits()
We'll re-use some of our code from before to visualize the data and remind us what we're looking at:
%pylab inline
# copied from notebook 02_sklearn_data.ipynb
fig = plt.figure(figsize=(6, 6)) # figure size in inches
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
# plot the digits: each image is 8x8 pixels
for i in range(64):
ax = fig.add_subplot(8, 8, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary)
# label the image with the target value
ax.text(0, 7, str(digits.target[i]))
A good first-step for many problems is to visualize the data using one of the Dimensionality Reduction techniques we saw earlier. We'll start with the most straightforward one, Principal Component Analysis (PCA).
PCA seeks orthogonal linear combinations of the features which show the greatest
variance, and as such, can help give you a good idea of the structure of the
data set. Here we'll use RandomizedPCA
, because it's faster for large N
.
from sklearn.decomposition import RandomizedPCA
pca = RandomizedPCA(n_components=2)
proj = pca.fit_transform(digits.data)
plt.scatter(proj[:, 0], proj[:, 1], c=digits.target)
plt.colorbar()
Here we see that the digits do cluster fairly well, so we can expect even a fairly naive classification scheme to do a decent job separating them.
A weakness of PCA is that it produces a linear dimensionality reduction:
this may miss some interesting relationships in the data. If we want to
see a nonlinear mapping of the data, we can use one of the several
methods in the manifold
module. Here we'll use Isomap (a concatenation
of Isometric Mapping) which is a manifold learning method based on
graph theory:
from sklearn.manifold import Isomap
iso = Isomap(n_neighbors=5, n_components=2)
proj = iso.fit_transform(digits.data)
plt.scatter(proj[:, 0], proj[:, 1], c=digits.target)
plt.colorbar()
It can be fun to explore the various manifold learning methods available, and how the output depends on the various parameters used to tune the projection. In any case, these visualizations show us that there is hope: even a simple classifier should be able to adequately identify the members of the various classes.
For most classification problems, it's nice to have a simple, fast, go-to method to provide a quick baseline classification. If the simple and fast method is sufficient, then we don't have to waste CPU cycles on more complex models. If not, we can use the results of the simple method to give us clues about our data.
One good method to keep in mind is Gaussian Naive Bayes. It is a generative classifier which fits an axis-aligned multi-dimensional Gaussian distribution to each training label, and uses this to quickly give a rough classification. It is generally not sufficiently accurate for real-world data, but (especially in high dimensions) can perform surprisingly well.
from sklearn.naive_bayes import GaussianNB
from sklearn import cross_validation
# split the data into training and validation sets
data_train, data_test, target_train, target_test = cross_validation.train_test_split(digits.data, digits.target)
# train the model
clf = GaussianNB()
clf.fit(data_train, target_train)
# predict the labels of the test data
predicted = clf.predict(data_test)
expected = target_test
Previously we've done something like
print (predicted == expected)
as a rough evaluation of our model. Here we'll do something more sophisticated:
scikit-learn includes a metrics
module which contains several metrics for
evaluating classifiers like this. One of the most useful combines several of
the metrics and prints a table showing the results:
from sklearn import metrics
print metrics.classification_report(expected, predicted)
Another enlightening metric for this sort of task is a confusion matrix: it helps us visualize which labels are being interchanged in the classification errors:
print metrics.confusion_matrix(expected, predicted)
We see here that in particular, a lot of twos are being mistakenly labeled eights.
Previously we mentioned cross-validation. Here, rather than having a single training
and test set, we divide the data into K
subsets, and perform K
different classifications,
each time training on K - 1
of the subsets and validating on the one left out.
The tools to accomplish this are also in the cross_validation
submodule.
cv = cross_validation.KFold(digits.data.shape[0], 5, shuffle=True, random_state=0)
clf = GaussianNB()
print cross_validation.cross_val_score(clf, digits.data, digits.target, cv=cv)
These metrics show us that the simplistic Gaussian Naive Bayes classifier is giving us correct classifications of about 4 out of 5 digits. This is probably not sufficient: imagine the chaos at the post office if their zipcode scanning software misread one out of five digits!
We can do better... but how?
Here we come to the first major exercise of the tutorial, which will take some thought and creativity. It will combine and extend several of the ideas we've worked through today. The question is this:
invest effort in gathering more training samples, or seeking a more sophisticated model?**
Some things to keep in mind:
Once you're finished with this, you may wish to try out some other classifiers on the data
and see how the results compare. Another extremely simple and fast classifier is
sklearn.tree.DecisionTreeClassifier
. Several more sophisticated (and therefore
slower) classifiers can be found in the Support Vector Machines module, sklearn.svm
.