2012-10-22 04:45:37 +08:00
|
|
|
"""
|
2012-11-05 20:55:41 +08:00
|
|
|
==================================
|
|
|
|
|
Comparing various online solvers
|
|
|
|
|
==================================
|
|
|
|
|
An example showing how different online solvers perform
|
|
|
|
|
on the hand-written digits dataset.
|
2012-10-22 04:45:37 +08:00
|
|
|
"""
|
2021-10-22 21:33:22 +08:00
|
|
|
|
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
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-10-22 04:45:37 +08:00
|
|
|
from sklearn import datasets
|
2014-09-29 18:58:30 +08:00
|
|
|
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import train_test_split
|
2015-05-19 16:24:22 +08:00
|
|
|
from sklearn.linear_model import SGDClassifier, Perceptron
|
2012-10-24 22:55:51 +08:00
|
|
|
from sklearn.linear_model import PassiveAggressiveClassifier
|
2015-05-19 16:24:22 +08:00
|
|
|
from sklearn.linear_model import LogisticRegression
|
2012-10-22 04:45:37 +08:00
|
|
|
|
|
|
|
|
heldout = [0.95, 0.90, 0.75, 0.50, 0.01]
|
2021-12-10 01:40:55 +08:00
|
|
|
# Number of rounds to fit and evaluate an estimator.
|
|
|
|
|
rounds = 10
|
2019-07-14 23:10:54 +08:00
|
|
|
X, y = datasets.load_digits(return_X_y=True)
|
2012-10-22 04:45:37 +08:00
|
|
|
|
|
|
|
|
classifiers = [
|
2021-12-10 01:40:55 +08:00
|
|
|
("SGD", SGDClassifier(max_iter=110)),
|
|
|
|
|
("ASGD", SGDClassifier(max_iter=110, average=True)),
|
|
|
|
|
("Perceptron", Perceptron(max_iter=110)),
|
2012-11-05 20:55:41 +08:00
|
|
|
(
|
|
|
|
|
"Passive-Aggressive I",
|
2021-12-10 01:40:55 +08:00
|
|
|
PassiveAggressiveClassifier(max_iter=110, loss="hinge", C=1.0, tol=1e-4),
|
2017-10-04 23:28:32 +08:00
|
|
|
),
|
2012-11-05 20:55:41 +08:00
|
|
|
(
|
|
|
|
|
"Passive-Aggressive II",
|
2021-12-10 01:40:55 +08:00
|
|
|
PassiveAggressiveClassifier(
|
|
|
|
|
max_iter=110, loss="squared_hinge", C=1.0, tol=1e-4
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"SAG",
|
|
|
|
|
LogisticRegression(max_iter=110, solver="sag", tol=1e-1, C=1.0e4 / X.shape[0]),
|
2017-10-04 23:28:32 +08:00
|
|
|
),
|
2012-10-22 05:36:02 +08:00
|
|
|
]
|
2012-10-22 04:45:37 +08:00
|
|
|
|
2014-09-11 04:40:55 +08:00
|
|
|
xx = 1.0 - np.array(heldout)
|
|
|
|
|
|
2012-10-22 05:36:02 +08:00
|
|
|
for name, clf in classifiers:
|
2014-10-30 19:00:11 +08:00
|
|
|
print("training %s" % name)
|
2014-09-29 18:58:30 +08:00
|
|
|
rng = np.random.RandomState(42)
|
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):
|
2014-09-11 04:40:55 +08:00
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
|
|
|
X, y, test_size=i, random_state=rng
|
|
|
|
|
)
|
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_))
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.plot(xx, yy, label=name)
|
2012-10-22 04:45:37 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.legend(loc="upper right")
|
|
|
|
|
plt.xlabel("Proportion train")
|
|
|
|
|
plt.ylabel("Test Error Rate")
|
|
|
|
|
plt.show()
|