1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# -*- coding: utf-8 -*-
"""
Unit tests for the grid_search module
"""
from __future__ import division, print_function
import numpy as np
import unittest
from sklearn.datasets import load_iris, load_digits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import maxabs_scale
from gensvm.gridsearch import (
GenSVMGridSearchCV,
_validate_param_grid,
load_default_grid,
)
class GenSVMGridSearchCVTestCase(unittest.TestCase):
def test_validate_param_grid(self):
""" GENSVM_GRID: Test parameter grid validation """
pg = {
"p": [1, 1.5, 2.0],
"kappa": [-0.9, 1.0],
"lmd": [0.1, 1.0],
"epsilon": [0.01, 0.002],
"gamma": [1.0, 2.0],
"weights": ["unit", "group"],
}
_validate_param_grid(pg)
tmp = {k: v for k, v in pg.items()}
tmp["p"] = [0.5, 1.0, 2.0]
with self.assertRaises(ValueError):
_validate_param_grid(tmp)
tmp = {k: v for k, v in pg.items()}
tmp["kappa"] = [-1.0, 0.0, 1.0]
with self.assertRaises(ValueError):
_validate_param_grid(tmp)
tmp = {k: v for k, v in pg.items()}
tmp["lmd"] = [-1.0, 0.0, 1.0]
with self.assertRaises(ValueError):
_validate_param_grid(tmp)
tmp = {k: v for k, v in pg.items()}
tmp["epsilon"] = [-1.0, 0.0, 1.0]
with self.assertRaises(ValueError):
_validate_param_grid(tmp)
tmp = {k: v for k, v in pg.items()}
tmp["gamma"] = [-1.0, 0.0, 1.0]
with self.assertRaises(ValueError):
_validate_param_grid(tmp)
tmp = {k: v for k, v in pg.items()}
tmp["weights"] = ["unit", "group", "other"]
with self.assertRaises(ValueError):
_validate_param_grid(tmp)
def test_fit_predict_strings(self):
""" GENSVM_GRID: Test fit and predict with string targets """
iris = load_iris()
X = iris.data
y = iris.target
labels = iris.target_names
yy = labels[y]
X_train, X_test, y_train, y_test = train_test_split(X, yy)
pg = {
"p": [1, 1.5, 2.0],
"kappa": [-0.9, 1.0],
"lmd": [0.1, 1.0],
"epsilon": [0.01, 0.002],
"gamma": [1.0, 2.0],
"weights": ["unit", "group"],
}
clf = GenSVMGridSearchCV(pg)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
pred_set = set(y_pred)
label_set = set(labels)
self.assertTrue(pred_set.issubset(label_set))
def test_fit_score(self):
""" GENSVM_GRID: Test fit and score """
X, y = load_iris(return_X_y=True)
X = maxabs_scale(X)
X_train, X_test, y_train, y_test = train_test_split(X, y)
pg = {
"p": [1, 1.5, 2.0],
"kappa": [-0.9, 1.0, 5.0],
"lmd": [pow(2, x) for x in range(-12, 9, 2)],
}
clf = GenSVMGridSearchCV(pg)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
# low for safety
self.assertGreaterEqual(score, 0.80)
def test_refit(self):
""" GENSVM_GRID: Test refit """
# we use the fact that large regularization parameters usually don't
# give a good fit.
X, y = load_iris(return_X_y=True)
X = maxabs_scale(X)
X_train, X_test, y_train, y_test = train_test_split(X, y)
pg = {"lmd": [1e-4, 100, 10000]}
clf = GenSVMGridSearchCV(pg)
clf.fit(X_train, y_train)
self.assertTrue(hasattr(clf, "best_params_"))
self.assertTrue(clf.best_params_ == {"lmd": 1e-4})
def test_multimetric(self):
""" GENSVM_GRID: Test multimetric """
X, y = load_iris(return_X_y=True)
X = maxabs_scale(X)
X_train, X_test, y_train, y_test = train_test_split(X, y)
pg = {"p": [1., 1.5, 2.]}
clf = GenSVMGridSearchCV(
pg, scoring=["accuracy", "adjusted_rand_score"], refit=False
)
clf.fit(X_train, y_train)
self.assertTrue(clf.multimetric_)
self.assertTrue("mean_test_accuracy" in clf.cv_results_)
self.assertTrue("mean_test_adjusted_rand_score" in clf.cv_results_)
def test_refit_multimetric(self):
""" GENSVM_GRID: Test refit with multimetric """
X, y = load_iris(return_X_y=True)
X = maxabs_scale(X)
X_train, X_test, y_train, y_test = train_test_split(X, y)
pg = {"lmd": [1e-4, 100, 10000]}
clf = GenSVMGridSearchCV(
pg, scoring=["accuracy", "adjusted_rand_score"], refit="accuracy"
)
clf.fit(X_train, y_train)
self.assertTrue(hasattr(clf, "best_params_"))
self.assertTrue(hasattr(clf, "best_estimator_"))
self.assertTrue(hasattr(clf, "best_index_"))
self.assertTrue(hasattr(clf, "best_score_"))
self.assertTrue(clf.best_params_ == {"lmd": 1e-4})
def test_params_rbf_kernel(self):
""" GENSVM_GRID: Test best params with RBF kernel """
X, y = load_iris(return_X_y=True)
X = maxabs_scale(X)
X_train, X_test, y_train, y_test = train_test_split(X, y)
pg = {"lmd": [1e-4, 100, 10000], "kernel": ["rbf"]}
clf = GenSVMGridSearchCV(pg)
clf.fit(X_train, y_train)
self.assertTrue(hasattr(clf, "best_params_"))
def test_invalid_y(self):
""" GENSVM_GRID: Check raises for invalid y type """
pg = {"lmd": [1e-4, 100, 10000], "kernel": ["rbf"]}
clf = GenSVMGridSearchCV(pg)
X = np.random.random((20, 4))
y = np.random.random((20,))
with self.assertRaises(ValueError) as err:
clf.fit(X, y)
exc = err.exception
self.assertEqual(
exc.args, ("Label type not allowed for GenSVM: 'continuous'",)
)
def slowtest_gridsearch_warnings(self):
""" GENSVM_GRID: Check grid search with warnings """
np.random.seed(123)
X, y = load_digits(4, return_X_y=True)
small = {}
for k in [1, 2, 3]:
tmp = X[y == k, :]
small[k] = tmp[np.random.choice(tmp.shape[0], 20), :]
Xs = np.vstack((small[1], small[2], small[3]))
ys = np.hstack((np.ones(20), 2 * np.ones(20), 3 * np.ones(20)))
pg = {
"p": [1.0, 2.0],
"lmd": [pow(10, x) for x in range(-4, 1, 2)],
"epsilon": [1e-6],
}
gg = GenSVMGridSearchCV(pg, verbose=True)
gg.fit(Xs, ys)
|