|
1 | 1 | """
|
2 |
| -=============================================== |
3 |
| -Comparison of EigenPro and SVC on Fashion-MNIST |
4 |
| -=============================================== |
| 2 | +====================================================== |
| 3 | +Comparison of EigenPro and SVC on Digit Classification |
| 4 | +====================================================== |
5 | 5 |
|
6 | 6 | Here we train a EigenPro Classifier and a Support
|
7 |
| -Vector Classifier (SVC) on subsets of MNIST of various sizes. |
8 |
| -We halt the training of EigenPro after two epochs. |
9 |
| -Experimental results on MNIST demonstrate more than 3 times |
10 |
| -speedup of EigenPro over SVC in training time. EigenPro also |
11 |
| -shows consistently lower classification error on test set. |
| 7 | +Vector Classifier (SVC) on a synthetically generated |
| 8 | +binary classification problem. We halt the training |
| 9 | +of EigenPro after two epochs. |
| 10 | +While EigenPro is slower on low dimensional datasets, as |
| 11 | +the number of features exceeds 500, it begins to outperform |
| 12 | +SVM in terms of both time and training error. |
12 | 13 | """
|
13 | 14 | print(__doc__)
|
14 | 15 |
|
|
17 | 18 | import numpy as np
|
18 | 19 | from time import time
|
19 | 20 |
|
| 21 | +from sklearn.datasets import make_classification |
20 | 22 | from sklearn_extra.kernel_methods import EigenProClassifier
|
21 | 23 | from sklearn.svm import SVC
|
22 |
| -from sklearn.datasets import fetch_openml |
23 | 24 |
|
24 | 25 | rng = np.random.RandomState(1)
|
25 | 26 |
|
26 |
| -# Generate sample data from mnist |
27 |
| -mnist = fetch_openml("Fashion-MNIST") |
28 |
| -mnist.data = mnist.data / 255.0 |
29 |
| -print("Data has loaded") |
30 |
| - |
31 |
| -p = rng.permutation(60000) |
32 |
| -x_train = mnist.data[p] |
33 |
| -y_train = np.int32(mnist.target[p]) |
34 |
| -x_test = mnist.data[60000:] |
35 |
| -y_test = np.int32(mnist.target[60000:]) |
| 27 | +train_size = 2000 |
| 28 | +test_size = 1000 |
36 | 29 |
|
37 | 30 | # Run tests comparing eig to svc
|
38 | 31 | eig_fit_times = []
|
|
42 | 35 | svc_pred_times = []
|
43 | 36 | svc_err = []
|
44 | 37 |
|
45 |
| -train_sizes = [500, 1000, 2000] |
46 |
| - |
47 |
| -print("Train Sizes: " + str(train_sizes)) |
48 |
| - |
49 |
| -bandwidth = 5.0 |
| 38 | +feature_counts = [15, 50, 150, 500, 1500] |
| 39 | +bandwidth = 8.0 |
50 | 40 |
|
51 | 41 | # Fit models to data
|
52 |
| -for train_size in train_sizes: |
| 42 | +for n_features in feature_counts: |
| 43 | + x, y = make_classification( |
| 44 | + n_samples=train_size + test_size, |
| 45 | + n_features=n_features, |
| 46 | + random_state=rng, |
| 47 | + ) |
| 48 | + |
| 49 | + x_train = x[:train_size] |
| 50 | + y_train = y[:train_size] |
| 51 | + x_test = x[train_size:] |
| 52 | + y_test = y[train_size:] |
53 | 53 | for name, estimator in [
|
54 | 54 | (
|
55 | 55 | "EigenPro",
|
56 | 56 | EigenProClassifier(
|
57 |
| - n_epoch=2, bandwidth=bandwidth, random_state=rng |
| 57 | + n_epoch=2, |
| 58 | + bandwidth=bandwidth, |
| 59 | + n_components=400, |
| 60 | + random_state=rng, |
58 | 61 | ),
|
59 | 62 | ),
|
60 | 63 | (
|
61 | 64 | "SupportVector",
|
62 |
| - SVC( |
63 |
| - C=5, gamma=1.0 / (2 * bandwidth * bandwidth), random_state=rng |
64 |
| - ), |
| 65 | + SVC(gamma=1.0 / (2 * bandwidth * bandwidth), random_state=rng), |
65 | 66 | ),
|
66 | 67 | ]:
|
67 | 68 | stime = time()
|
68 |
| - estimator.fit(x_train[:train_size], y_train[:train_size]) |
| 69 | + estimator.fit(x_train, y_train) |
69 | 70 | fit_t = time() - stime
|
70 | 71 |
|
71 | 72 | stime = time()
|
|
82 | 83 | svc_pred_times.append(pred_t)
|
83 | 84 | svc_err.append(err)
|
84 | 85 | print(
|
85 |
| - "%s Classification with %i training samples in %0.2f seconds." |
86 |
| - % (name, train_size, fit_t + pred_t) |
| 86 | + "%s Classification with %i features in %0.2f seconds. Error: %0.1f" |
| 87 | + % (name, n_features, fit_t + pred_t, err) |
87 | 88 | )
|
88 | 89 |
|
89 | 90 | # set up grid for figures
|
90 | 91 | fig = plt.figure(num=None, figsize=(6, 4), dpi=160)
|
91 | 92 | ax = plt.subplot2grid((2, 2), (0, 0), rowspan=2)
|
92 | 93 |
|
93 | 94 | # Graph fit(train) time
|
94 |
| -train_size_labels = [str(s) for s in train_sizes] |
95 |
| -ax.plot(train_sizes, svc_fit_times, "o--", color="g", label="SVC") |
| 95 | +feature_number_labels = [str(s) for s in feature_counts] |
| 96 | +ax.plot(feature_counts, svc_fit_times, "o--", color="g", label="SVC") |
96 | 97 | ax.plot(
|
97 |
| - train_sizes, eig_fit_times, "o-", color="r", label="EigenPro Classifier" |
| 98 | + feature_counts, eig_fit_times, "o-", color="r", label="EigenPro Classifier" |
98 | 99 | )
|
99 | 100 | ax.set_xscale("log")
|
100 | 101 | ax.set_yscale("log", nonposy="clip")
|
101 |
| -ax.set_xlabel("train size") |
| 102 | +ax.set_xlabel("Number of features") |
102 | 103 | ax.set_ylabel("time (seconds)")
|
103 | 104 | ax.legend()
|
104 | 105 | ax.set_title("Training Time")
|
105 |
| -ax.set_xticks(train_sizes) |
106 |
| -ax.set_xticklabels(train_size_labels) |
| 106 | +ax.set_xticks(feature_counts) |
| 107 | +ax.set_xticklabels(feature_number_labels) |
107 | 108 | ax.set_xticks([], minor=True)
|
108 | 109 | ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
|
109 | 110 |
|
110 | 111 | # Graph prediction(test) time
|
111 | 112 | ax = plt.subplot2grid((2, 2), (0, 1), rowspan=1)
|
112 |
| -ax.plot(train_sizes, eig_pred_times, "o-", color="r") |
113 |
| -ax.plot(train_sizes, svc_pred_times, "o--", color="g") |
| 113 | +ax.plot(feature_counts, eig_pred_times, "o-", color="r") |
| 114 | +ax.plot(feature_counts, svc_pred_times, "o--", color="g") |
114 | 115 | ax.set_xscale("log")
|
115 | 116 | ax.set_yscale("log", nonposy="clip")
|
116 | 117 | ax.set_ylabel("time (seconds)")
|
|
120 | 121 |
|
121 | 122 | # Graph training error
|
122 | 123 | ax = plt.subplot2grid((2, 2), (1, 1), rowspan=1)
|
123 |
| -ax.plot(train_sizes, eig_err, "o-", color="r") |
124 |
| -ax.plot(train_sizes, svc_err, "o-", color="g") |
| 124 | +ax.plot(feature_counts, eig_err, "o-", color="r") |
| 125 | +ax.plot(feature_counts, svc_err, "o-", color="g") |
125 | 126 | ax.set_xscale("log")
|
126 |
| -ax.set_xticks(train_sizes) |
127 |
| -ax.set_xticklabels(train_size_labels) |
| 127 | +ax.set_xticks(feature_counts) |
| 128 | +ax.set_xticklabels(feature_number_labels) |
128 | 129 | ax.set_xticks([], minor=True)
|
129 |
| -ax.set_xlabel("train size") |
| 130 | +ax.set_xlabel("Number of features") |
130 | 131 | ax.set_ylabel("Classification error %")
|
131 | 132 | plt.tight_layout()
|
132 | 133 | plt.show()
|
0 commit comments