-
-
Notifications
You must be signed in to change notification settings - Fork 554
/
test_alphas.py
220 lines (179 loc) · 7.01 KB
/
test_alphas.py
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
210
211
212
213
214
215
216
217
218
219
220
# tests.test_regressor.test_alphas
# Tests for the alpha selection visualizations.
#
# Author: Benjamin Bengfort
# Created: Tue Mar 07 12:13:04 2017 -0500
#
# Copyright (C) 2016 The scikit-yb developers
# For license information, see LICENSE.txt
#
# ID: test_alphas.py [7d3f5e6] benjamin@bengfort.com $
"""
Tests for the alpha selection visualizations.
"""
##########################################################################
## Imports
##########################################################################
import sys
import pytest
import numpy as np
from tests.base import VisualTestCase
from numpy.testing import assert_array_equal
from yellowbrick.datasets import load_energy
from yellowbrick.exceptions import YellowbrickTypeError
from yellowbrick.exceptions import YellowbrickValueError
from yellowbrick.regressor.alphas import AlphaSelection, alphas
from yellowbrick.regressor.alphas import ManualAlphaSelection, manual_alphas
from sklearn.svm import SVR, SVC
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.datasets import make_regression
from sklearn.linear_model import Ridge, RidgeCV
from sklearn.linear_model import Lasso, LassoCV
from sklearn.linear_model import LassoLars, LassoLarsCV
from sklearn.linear_model import ElasticNet, ElasticNetCV
##########################################################################
## Alpha Selection Tests
##########################################################################
class TestAlphaSelection(VisualTestCase):
"""
Test the AlphaSelection visualizer
"""
@pytest.mark.xfail(sys.platform == "win32", reason="images not close on windows")
def test_similar_image(self):
"""
Integration test with image similarity comparison
"""
visualizer = AlphaSelection(LassoCV(random_state=0))
X, y = make_regression(random_state=0)
visualizer.fit(X, y)
visualizer.finalize()
self.assert_images_similar(visualizer)
@pytest.mark.parametrize("model", [SVR, Ridge, Lasso, LassoLars, ElasticNet])
def test_regressor_nocv(self, model):
"""
Ensure only "CV" regressors are allowed
"""
with pytest.raises(YellowbrickTypeError):
AlphaSelection(model())
@pytest.mark.parametrize("model", [RidgeCV, LassoCV, LassoLarsCV, ElasticNetCV])
def test_regressor_cv(self, model):
"""
Ensure "CV" regressors are allowed
"""
try:
AlphaSelection(model())
except YellowbrickTypeError:
pytest.fail("could not instantiate RegressorCV on alpha selection")
@pytest.mark.parametrize("model", [SVC, KMeans, PCA])
def test_only_regressors(self, model):
"""
Assert AlphaSelection only works with regressors
"""
with pytest.raises(YellowbrickTypeError):
AlphaSelection(model())
def test_store_cv_values(self):
"""
Assert that store_cv_values is true on RidgeCV
"""
model = AlphaSelection(RidgeCV())
assert model.estimator.store_cv_values
model = AlphaSelection(RidgeCV(store_cv_values=True))
assert model.estimator.store_cv_values
model = AlphaSelection(RidgeCV(store_cv_values=False))
assert model.estimator.store_cv_values
@pytest.mark.parametrize("model", [RidgeCV, LassoCV, ElasticNetCV])
def test_get_alphas_param(self, model):
"""
Assert that we can get the alphas from original CV models
"""
alphas = np.logspace(-10, -2, 100)
try:
model = AlphaSelection(model(alphas=alphas))
malphas = model._find_alphas_param()
assert_array_equal(alphas, malphas)
except YellowbrickValueError:
pytest.fail("could not find alphas on {}".format(model.name))
def test_get_alphas_param_lassolars(self):
"""
Assert that we can get alphas from lasso lars.
"""
X, y = make_regression()
model = AlphaSelection(LassoLarsCV())
model.fit(X, y)
try:
malphas = model._find_alphas_param()
assert len(malphas) > 0
except YellowbrickValueError:
pytest.fail("could not find alphas on {}".format(model.name))
@pytest.mark.parametrize("model", [RidgeCV, LassoCV, LassoLarsCV, ElasticNetCV])
def test_get_errors_param(self, model):
"""
Test known models we can get the cv errors for alpha selection
"""
try:
model = AlphaSelection(model())
X, y = make_regression()
model.fit(X, y)
errors = model._find_errors_param()
assert len(errors) > 0
except YellowbrickValueError:
pytest.fail("could not find errors on {}".format(model.name))
def test_score(self):
"""
Assert the score method returns an R2 value
"""
visualizer = AlphaSelection(RidgeCV())
X, y = make_regression(random_state=352)
visualizer.fit(X, y)
assert visualizer.score(X, y) == pytest.approx(0.9999780266590336)
def test_quick_method(self):
"""
Test the quick method producing a valid visualization
"""
X, y = load_energy(return_dataset=True).to_numpy()
visualizer = alphas(
LassoCV(random_state=0), X, y, is_fitted=False, show=False
)
assert isinstance(visualizer, AlphaSelection)
self.assert_images_similar(visualizer)
class TestManualAlphaSelection(VisualTestCase):
"""
Test the ManualAlphaSelection visualizer
"""
def test_similar_image_manual(self):
"""
Integration test with image similarity comparison
"""
visualizer = ManualAlphaSelection(Lasso(random_state=0), cv=5)
X, y = make_regression(random_state=0)
visualizer.fit(X, y)
visualizer.finalize()
# Image comparison fails on Appveyor with RMS 0.024
self.assert_images_similar(visualizer, tol=0.1)
@pytest.mark.parametrize("model", [RidgeCV, LassoCV, LassoLarsCV, ElasticNetCV])
def test_manual_with_cv(self, model):
"""
Ensure only non-CV regressors are allowed
"""
with pytest.raises(YellowbrickTypeError):
ManualAlphaSelection(model())
@pytest.mark.parametrize("model", [SVR, Ridge, Lasso, LassoLars, ElasticNet])
def test_manual_no_cv(self, model):
"""
Ensure non-CV regressors are allowed
"""
try:
ManualAlphaSelection(model())
except YellowbrickTypeError:
pytest.fail("could not instantiate Regressor on alpha selection")
def test_quick_method_manual(self):
"""
Test the manual alphas quick method producing a valid visualization
"""
X, y = load_energy(return_dataset=True).to_numpy()
visualizer = manual_alphas(
ElasticNet(random_state=0), X, y, cv=3, is_fitted=False, show=False
)
assert isinstance(visualizer, ManualAlphaSelection)
self.assert_images_similar(visualizer)