2012-10-22 04:45:37 +08:00
|
|
|
"""
|
2012-11-05 20:55:41 +08:00
|
|
|
==================================
|
|
|
|
|
Comparing various online solvers
|
|
|
|
|
==================================
|
2012-10-22 04:45:37 +08:00
|
|
|
|
2012-11-05 20:55:41 +08:00
|
|
|
An example showing how different online solvers perform
|
|
|
|
|
on the hand-written digits dataset.
|
2012-10-22 04:45:37 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# Author: Rob Zinkov <rob at zinkov dot com>
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-10-22 04:45:37 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pl
|
|
|
|
|
from sklearn import datasets
|
|
|
|
|
from sklearn.cross_validation import train_test_split
|
|
|
|
|
from sklearn.linear_model import SGDClassifier, Perceptron
|
2012-10-24 22:55:51 +08:00
|
|
|
from sklearn.linear_model import PassiveAggressiveClassifier
|
2012-10-22 04:45:37 +08:00
|
|
|
|
|
|
|
|
heldout = [0.95, 0.90, 0.75, 0.50, 0.01]
|
2012-10-22 04:53:04 +08:00
|
|
|
rounds = 20
|
2012-10-22 04:45:37 +08:00
|
|
|
digits = datasets.load_digits()
|
|
|
|
|
|
|
|
|
|
classifiers = [
|
|
|
|
|
("SGD", SGDClassifier()),
|
|
|
|
|
("Perceptron", Perceptron()),
|
2012-11-05 20:55:41 +08:00
|
|
|
("Passive-Aggressive I", PassiveAggressiveClassifier(loss='hinge',
|
|
|
|
|
C=1.0)),
|
|
|
|
|
("Passive-Aggressive II", PassiveAggressiveClassifier(loss='squared_hinge',
|
|
|
|
|
C=1.0)),
|
2012-10-22 05:36:02 +08:00
|
|
|
]
|
2012-10-22 04:45:37 +08:00
|
|
|
|
2012-10-22 05:36:02 +08:00
|
|
|
xx = 1 - np.array(heldout)
|
|
|
|
|
for name, clf in classifiers:
|
2012-10-22 04:45:37 +08:00
|
|
|
yy = []
|
|
|
|
|
for i in heldout:
|
2012-10-22 04:53:04 +08:00
|
|
|
yy_ = []
|
|
|
|
|
for r in range(rounds):
|
2012-10-22 05:36:02 +08:00
|
|
|
X_train, X_test, y_train, y_test = train_test_split(digits.data,
|
|
|
|
|
digits.target,
|
2012-10-22 04:53:04 +08:00
|
|
|
test_size=i)
|
2012-10-22 05:36:02 +08:00
|
|
|
clf.fit(X_train, y_train)
|
2012-10-22 04:53:04 +08:00
|
|
|
y_pred = clf.predict(X_test)
|
2012-10-22 05:36:02 +08:00
|
|
|
yy_.append(1 - np.mean(y_pred == y_test))
|
2012-10-22 04:53:04 +08:00
|
|
|
yy.append(np.mean(yy_))
|
2012-10-22 04:45:37 +08:00
|
|
|
pl.plot(xx, yy, label=name)
|
|
|
|
|
|
|
|
|
|
pl.legend(loc="upper right")
|
|
|
|
|
pl.xlabel("Proportion train")
|
|
|
|
|
pl.ylabel("Test Error Rate")
|
|
|
|
|
pl.show()
|