1
|
|
|
import pytest |
2
|
|
|
import numpy as np |
3
|
|
|
|
4
|
|
|
from sklearn import svm, datasets |
5
|
|
|
from sklearn.naive_bayes import GaussianNB |
6
|
|
|
from sklearn.decomposition import PCA |
7
|
|
|
from sklearn.datasets import make_blobs |
8
|
|
|
from sklearn.exceptions import NotFittedError |
9
|
|
|
|
10
|
|
|
from sklearn.utils.validation import check_is_fitted |
11
|
|
|
|
12
|
|
|
from hyperactive.integrations import HyperactiveSearchCV |
13
|
|
|
from hyperactive.optimizers import RandomSearchOptimizer |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
iris = datasets.load_iris() |
17
|
|
|
X, y = iris.data, iris.target |
18
|
|
|
|
19
|
|
|
nb = GaussianNB() |
20
|
|
|
svc = svm.SVC() |
21
|
|
|
pca = PCA() |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
nb_params = { |
25
|
|
|
"var_smoothing": [1e-9, 1e-8], |
26
|
|
|
} |
27
|
|
|
svc_params = {"kernel": ["linear", "rbf"], "C": [1, 10]} |
28
|
|
|
pca_params = { |
29
|
|
|
"n_components": [2, 3], |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
opt = RandomSearchOptimizer() |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_fit(): |
37
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
38
|
|
|
search.fit(X, y) |
39
|
|
|
|
40
|
|
|
check_is_fitted(search) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def test_not_fitted(): |
44
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
45
|
|
|
assert not search.fit_successful |
46
|
|
|
|
47
|
|
|
with pytest.raises(NotFittedError): |
48
|
|
|
check_is_fitted(search) |
49
|
|
|
|
50
|
|
|
assert not search.fit_successful |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_false_params(): |
54
|
|
|
search = HyperactiveSearchCV(svc, nb_params, opt) |
55
|
|
|
with pytest.raises(ValueError): |
56
|
|
|
search.fit(X, y) |
57
|
|
|
|
58
|
|
|
assert not search.fit_successful |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def test_score(): |
62
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
63
|
|
|
search.fit(X, y) |
64
|
|
|
score = search.score(X, y) |
65
|
|
|
|
66
|
|
|
assert isinstance(score, float) |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def test_classes_(): |
70
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
71
|
|
|
search.fit(X, y) |
72
|
|
|
|
73
|
|
|
assert [0, 1, 2] == list(search.classes_) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_score_samples(): |
77
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
78
|
|
|
search.fit(X, y) |
79
|
|
|
|
80
|
|
|
with pytest.raises(AttributeError): |
81
|
|
|
search.score_samples(X) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def test_predict(): |
85
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
86
|
|
|
search.fit(X, y) |
87
|
|
|
result = search.predict(X) |
88
|
|
|
|
89
|
|
|
assert isinstance(result, np.ndarray) |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def test_predict_proba(): |
93
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
94
|
|
|
search.fit(X, y) |
95
|
|
|
|
96
|
|
|
with pytest.raises(AttributeError): |
97
|
|
|
search.predict_proba(X) |
98
|
|
|
|
99
|
|
|
search = HyperactiveSearchCV(nb, nb_params, opt) |
100
|
|
|
search.fit(X, y) |
101
|
|
|
result = search.predict(X) |
102
|
|
|
|
103
|
|
|
assert isinstance(result, np.ndarray) |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
def test_predict_log_proba(): |
107
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
108
|
|
|
search.fit(X, y) |
109
|
|
|
|
110
|
|
|
with pytest.raises(AttributeError): |
111
|
|
|
search.predict_log_proba(X) |
112
|
|
|
|
113
|
|
|
search = HyperactiveSearchCV(nb, nb_params, opt) |
114
|
|
|
search.fit(X, y) |
115
|
|
|
result = search.predict_log_proba(X) |
116
|
|
|
|
117
|
|
|
assert isinstance(result, np.ndarray) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
def test_decision_function(): |
121
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
122
|
|
|
search.fit(X, y) |
123
|
|
|
result = search.decision_function(X) |
124
|
|
|
|
125
|
|
|
assert isinstance(result, np.ndarray) |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def test_transform(): |
129
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
130
|
|
|
search.fit(X, y) |
131
|
|
|
|
132
|
|
|
with pytest.raises(AttributeError): |
133
|
|
|
search.transform(X) |
134
|
|
|
|
135
|
|
|
search = HyperactiveSearchCV(pca, pca_params, opt) |
136
|
|
|
search.fit(X, y) |
137
|
|
|
result = search.transform(X) |
138
|
|
|
|
139
|
|
|
assert isinstance(result, np.ndarray) |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
def test_inverse_transform(): |
143
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
144
|
|
|
search.fit(X, y) |
145
|
|
|
|
146
|
|
|
with pytest.raises(AttributeError): |
147
|
|
|
search.inverse_transform(X) |
148
|
|
|
|
149
|
|
|
search = HyperactiveSearchCV(pca, pca_params, opt) |
150
|
|
|
search.fit(X, y) |
151
|
|
|
result = search.inverse_transform(search.transform(X)) |
152
|
|
|
|
153
|
|
|
assert isinstance(result, np.ndarray) |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
def test_best_params_and_score(): |
157
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt) |
158
|
|
|
search.fit(X, y) |
159
|
|
|
|
160
|
|
|
best_params = search.best_params_ |
161
|
|
|
best_score = search.best_score_ |
162
|
|
|
|
163
|
|
|
assert "kernel" in best_params and "C" in best_params |
164
|
|
|
assert isinstance(best_score, float) |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
def test_search_data(): |
168
|
|
|
n_iter = 50 |
169
|
|
|
search = HyperactiveSearchCV(svc, svc_params, opt, n_iter=n_iter) |
170
|
|
|
search.fit(X, y) |
171
|
|
|
|
172
|
|
|
search_data = search.search_data_ |
173
|
|
|
columns = search_data.columns |
174
|
|
|
|
175
|
|
|
assert len(search_data) == n_iter |
176
|
|
|
assert "kernel" in columns and "C" in columns |
177
|
|
|
|