Introduction to scikit-learn

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.

1. Data collection

2. Data preprocessing

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


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-04c05bfd0633> in <module>()
----> 1 from sklearn import datasets
      2 digits = datasets.load_digits()

ImportError: No module named sklearn

In [ ]:
%pylab inline

In [ ]:
digits.data

In [ ]:
digits.data.shape  # n_samples, n_features

digits.images.shape


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

3. Build a model on training data

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

learning


In [ ]:
clf.fit(digits.data[:-500], digits.target[:-500])

predicting


In [ ]:
clf.predict(digits.data[-1]), digits.target[-1]

4. Evaluate the model on the test data

learning dataset


In [2]:
(clf.predict(digits.data[:-500]) == digits.target[:-500]).sum() / float(len(digits.target[:-500]))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-4f5ec909637c> in <module>()
----> 1 (clf.predict(digits.data[:-500]) == digits.target[:-500]).sum() / float(len(digits.target[:-500]))

NameError: name 'clf' is not defined

test dataset


In [3]:
(clf.predict(digits.data[-500:]) == digits.target[-500:]).sum() / 500.0


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-36487318fc74> in <module>()
----> 1 (clf.predict(digits.data[-500:]) == digits.target[-500:]).sum() / 500.0

NameError: name 'clf' is not defined

5. Deploy to the real system


In [4]:
import pickle
s = pickle.dumps(clf)
clf2 = pickle.loads(s)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-0745166e9ccb> in <module>()
      1 import pickle
----> 2 s = pickle.dumps(clf)
      3 clf2 = pickle.loads(s)

NameError: name 'clf' is not defined

In [5]:
clf2.predict(digits.data[-1])


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-07f846f1fc5d> in <module>()
----> 1 clf2.predict(digits.data[-1])

NameError: name 'clf2' is not defined

Reference


In [ ]: