-
Notifications
You must be signed in to change notification settings - Fork 83
/
test_interoperability.py
191 lines (157 loc) · 6.37 KB
/
test_interoperability.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
# Copyright 2019 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from test import EnableSchemaValidation
from jsonschema.exceptions import ValidationError
import lale.type_checking
from lale.lib.lale import ConcatFeatures, NoOp
from lale.lib.sklearn import PCA, LogisticRegression, Nystroem
class TestResamplers(unittest.TestCase):
def setUp(self):
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(
n_classes=2,
class_sep=2,
weights=[0.1, 0.9],
n_informative=3,
n_redundant=1,
flip_y=0,
n_features=20,
n_clusters_per_class=1,
n_samples=1000,
random_state=10,
)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y)
def create_function_test_resampler(res_name):
def test_resampler(self):
from lale.lib.sklearn import PCA, LogisticRegression
X_train, y_train = self.X_train, self.y_train
X_test = self.X_test
import importlib
module_name = ".".join(res_name.split(".")[0:-1])
class_name = res_name.split(".")[-1]
module = importlib.import_module(module_name)
class_ = getattr(module, class_name)
with EnableSchemaValidation():
with self.assertRaises(ValidationError):
_ = class_()
# test_schemas_are_schemas
lale.type_checking.validate_is_schema(class_.input_schema_fit())
lale.type_checking.validate_is_schema(class_.input_schema_predict())
lale.type_checking.validate_is_schema(class_.output_schema_predict())
lale.type_checking.validate_is_schema(class_.hyperparam_schema())
# test_init_fit_predict
from lale.operators import make_pipeline
pipeline1 = PCA() >> class_(operator=make_pipeline(LogisticRegression()))
trained = pipeline1.fit(X_train, y_train)
_ = trained.predict(X_test)
pipeline2 = class_(operator=make_pipeline(PCA(), LogisticRegression()))
trained = pipeline2.fit(X_train, y_train)
_ = trained.predict(X_test)
# test_with_hyperopt
from lale.lib.lale import Hyperopt
optimizer = Hyperopt(
estimator=PCA >> class_(operator=make_pipeline(LogisticRegression())),
max_evals=1,
show_progressbar=False,
)
trained_optimizer = optimizer.fit(X_train, y_train)
_ = trained_optimizer.predict(X_test)
pipeline3 = class_(
operator=PCA()
>> (Nystroem & NoOp)
>> ConcatFeatures
>> LogisticRegression()
)
optimizer = Hyperopt(estimator=pipeline3, max_evals=1, show_progressbar=False)
trained_optimizer = optimizer.fit(X_train, y_train)
_ = trained_optimizer.predict(X_test)
pipeline4 = (
(
PCA >> class_(operator=make_pipeline(Nystroem()))
& class_(operator=make_pipeline(Nystroem()))
)
>> ConcatFeatures
>> LogisticRegression()
)
optimizer = Hyperopt(
estimator=pipeline4, max_evals=1, scoring="roc_auc", show_progressbar=False
)
trained_optimizer = optimizer.fit(X_train, y_train)
_ = trained_optimizer.predict(X_test)
# test_cross_validation
from lale.helpers import cross_val_score
cv_results = cross_val_score(pipeline1, X_train, y_train, cv=2)
self.assertEqual(len(cv_results), 2)
# test_to_json
pipeline1.to_json()
test_resampler.__name__ = "test_{0}".format(res_name.split(".")[-1])
return test_resampler
resamplers = [
"lale.lib.imblearn.SMOTE",
"lale.lib.imblearn.SMOTEENN",
"lale.lib.imblearn.ADASYN",
"lale.lib.imblearn.BorderlineSMOTE",
"lale.lib.imblearn.SVMSMOTE",
"lale.lib.imblearn.RandomOverSampler",
"lale.lib.imblearn.CondensedNearestNeighbour",
"lale.lib.imblearn.EditedNearestNeighbours",
"lale.lib.imblearn.RepeatedEditedNearestNeighbours",
"lale.lib.imblearn.AllKNN",
"lale.lib.imblearn.InstanceHardnessThreshold",
]
for res in resamplers:
setattr(
TestResamplers,
"test_{0}".format(res.split(".")[-1]),
create_function_test_resampler(res),
)
class TestImblearn(unittest.TestCase):
def setUp(self):
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(
n_classes=2,
class_sep=2,
weights=[0.1, 0.9],
n_informative=3,
n_redundant=1,
flip_y=0,
n_features=20,
n_clusters_per_class=1,
n_samples=1000,
random_state=10,
)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y)
def test_decision_function(self):
from lale.lib.imblearn import SMOTE
from lale.lib.sklearn import RandomForestClassifier
from lale.operators import make_pipeline
smote = SMOTE(operator=make_pipeline(RandomForestClassifier()))
trained = smote.fit(self.X_train, self.y_train)
trained.predict(self.X_test)
with self.assertRaises(AttributeError):
trained.decision_function(self.X_test)
def test_string_labels(self):
from lale.lib.imblearn import CondensedNearestNeighbour
print(type(CondensedNearestNeighbour))
from lale.operators import make_pipeline
y_train = ["low" if label == 0 else "high" for label in self.y_train]
pipeline = CondensedNearestNeighbour(
operator=make_pipeline(PCA(), LogisticRegression()),
sampling_strategy=["high"],
)
trained = pipeline.fit(self.X_train, y_train)
_ = trained.predict(self.X_test)