-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathtest_autogen_lib.py
187 lines (148 loc) · 4.74 KB
/
test_autogen_lib.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
import inspect
import logging
import pytest
from sklearn import datasets
from lale.lib import autogen
from lale.lib.lale import Hyperopt
from lale.lib.lale.hyperopt import logger
from lale.lib.sklearn import LogisticRegression
from lale.operators import Operator, make_choice
logger.setLevel(logging.ERROR)
def load_iris():
iris = datasets.load_iris()
return iris.data, iris.target
def load_regression():
return datasets.make_regression(
n_features=4, n_informative=2, random_state=0, shuffle=False
)
def base_test(name, pipeline, data_loader, max_evals=250, scoring="accuracy"):
def test(i):
if i > max_evals:
assert False
try:
X, y = data_loader()
clf = Hyperopt(estimator=pipeline, max_evals=i, scoring=scoring)
trained_pipeline = clf.fit(X, y)
trained_pipeline.predict(X)
return
except Exception:
test(3 * i)
test(1)
kls = inspect.getmembers(autogen, lambda m: isinstance(m, Operator))
LR = LogisticRegression.customize_schema(relevantToOptimizer=[])
classifiers = [
"BernoulliNB",
"CalibratedClassifierCV",
"ComplementNB",
"GaussianProcessClassifier",
"LGBMClassifier",
"LabelPropagation",
"LabelSpreading",
"LogisticRegressionCV",
"NearestCentroid",
"NuSVC",
"Perceptron",
"RadiusNeighborsClassifier",
"RidgeClassifierCV",
]
@pytest.mark.parametrize("name, Op", [(n, Op) for (n, Op) in kls if n in classifiers])
def test_classifier(name, Op):
base_test(name, Op, load_iris)
multi = [
"MultiTaskElasticNet",
"MultiTaskElasticNetCV",
"MultiTaskLasso",
"MultiTaskLassoCV",
]
@pytest.mark.parametrize("name, Op", [(n, Op) for (n, Op) in kls if n in multi])
def test_multi(name, Op):
def load_multi():
X_multi = [[i, i] for i in range(100)]
return X_multi, X_multi
pytest.xfail(reason="Documentation error predict output type is 2D")
base_test(name, Op, load_multi)
regressors = [
"ARDRegression",
"BayesianRidge",
"ElasticNet",
"ElasticNetCV",
"GaussianProcessRegressor",
"HuberRegressor",
"Lars",
"LarsCV",
"Lasso",
"LassoCV",
"LassoLars",
"LassoLarsCV",
"LassoLarsIC",
"LGBMRegressor",
"NuSVR",
"OrthogonalMatchingPursuit",
"OrthogonalMatchingPursuitCV",
"PassiveAggressiveRegressor",
"RANSACRegressor",
"KernelRidge",
"RidgeCV",
"TheilSenRegressor",
"TransformedTargetRegressor",
]
failed_regressors = [
("MLPRegressor", "Input predict type (matrix with one column)"),
("RadiusNeighborsRegressor", "Radius argument is data dependent"),
]
@pytest.mark.parametrize("name, Op", [(n, Op) for (n, Op) in kls if n in regressors])
def test_regressors(name, Op):
base_test(name, Op, load_regression, scoring="r2")
@pytest.mark.parametrize("name, reason", failed_regressors)
def test_failed_regressor(name, reason):
pytest.xfail(reason)
transformers = [
"AdditiveChi2Sampler",
"BernoulliRBM",
"Binarizer",
"Birch",
"DictionaryLearning",
# "FactorAnalysis",
"FastICA",
"GaussianRandomProjection",
"IncrementalPCA",
"KBinsDiscretizer",
"KernelPCA",
"LinearDiscriminantAnalysis",
"LocallyLinearEmbedding",
"MaxAbsScaler",
"MiniBatchDictionaryLearning",
"MiniBatchKMeans",
"MiniBatchSparsePCA",
"PowerTransformer",
# "RandomTreesEmbedding",
"RBFSampler",
"SkewedChi2Sampler",
"SparsePCA",
"SparseRandomProjection",
"TruncatedSVD",
]
failed_transformers = [
("CCA", "Fit required Y (not y)"),
("LabelBinarizer", "operates on labels (not supported by lale yet)"),
("LabelEncoder", "operates on labels (not supported by lale yet)"),
("LatentDirichletAllocation", "Failed 2D array output"),
("MultiLabelBinarizer", "operates on labels (not supported by lale yet)"),
("PLSCanonical", "Fit required Y (not y)"),
("PLSRegression", "Fit required Y (not y)"),
("PLSSVD", "Fit required Y (not y)"),
]
@pytest.mark.parametrize("name, Op", [(n, Op) for (n, Op) in kls if n in transformers])
def test_transformer(name, Op):
base_test(name, Op >> LR, load_iris)
@pytest.mark.parametrize("name, reason", failed_transformers)
def test_failed_transformer(name, reason):
pytest.xfail(reason)
def test_2_steps_classifier():
T = make_choice(*[Op for (n, Op) in kls if n in transformers])
C = make_choice(*[Op for (n, Op) in kls if n in classifiers])
base_test("transformer_classifier", T >> C, load_iris)
def test_2_steps_regressor():
T = make_choice(*[Op for (n, Op) in kls if n in transformers])
R = make_choice(*[Op for (n, Op) in kls if n in regressors])
base_test("transformer_regressor", T >> R, load_regression, scoring="r2")