Classification of Handwritten Digits the task is to predict, given an image, which digit it represents. We are given samples of each of the 10 possible classes (the digits zero through nine) on which we fit an estimator to be able to predict the classes to which unseen samples belong.
A dataset is a dictionary-like object that holds all the data and some metadata about the data.
In [1]:
from sklearn import datasets
digits = datasets.load_digits()
In [ ]:
%pylab inline
In [ ]:
digits.data
In [ ]:
digits.data.shape # n_samples, n_features
In [ ]:
digits.target
In [ ]:
digits.target.shape
In [ ]:
# show images
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]))
In scikit-learn, an estimator for classification is a Python object that implements the methods fit(X, y) and predict(T). An example of an estimator is the class sklearn.svm.SVC that implements support vector classification.
In [ ]:
from sklearn import svm
clf = svm.SVC(gamma=0.001, C=100.)
In [ ]:
clf.fit(digits.data[:-500], digits.target[:-500])
In [ ]:
clf.predict(digits.data[-1]), digits.target[-1]
In [2]:
(clf.predict(digits.data[:-500]) == digits.target[:-500]).sum() / float(len(digits.target[:-500]))
In [3]:
(clf.predict(digits.data[-500:]) == digits.target[-500:]).sum() / 500.0
In [4]:
import pickle
s = pickle.dumps(clf)
clf2 = pickle.loads(s)
In [5]:
clf2.predict(digits.data[-1])
In [ ]: