forked from pycaret/pycaret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_regression.py
220 lines (184 loc) · 6.43 KB
/
test_regression.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
import os
import sys
import uuid
sys.path.insert(0, os.path.abspath(".."))
import pandas as pd
import pytest
from mlflow.tracking import MlflowClient
import pycaret.datasets
import pycaret.regression
@pytest.fixture(scope="module")
def boston_dataframe():
return pycaret.datasets.get_data("boston")
@pytest.mark.parametrize("return_train_score", [True, False])
def test_regression(boston_dataframe, return_train_score):
# loading dataset
assert isinstance(boston_dataframe, pd.DataFrame)
# init setup
reg1 = pycaret.regression.setup(
boston_dataframe,
target="medv",
remove_multicollinearity=True,
multicollinearity_threshold=0.95,
log_experiment=True,
html=False,
session_id=123,
n_jobs=1,
experiment_name=uuid.uuid4().hex,
)
# compare models
top3 = pycaret.regression.compare_models(
n_select=100,
exclude=["catboost"],
errors="raise",
)[:3]
assert isinstance(top3, list)
# tune model
tuned_top3 = [
pycaret.regression.tune_model(
i, n_iter=3, return_train_score=return_train_score
)
for i in top3
]
assert isinstance(tuned_top3, list)
pycaret.regression.tune_model(
top3[0], n_iter=3, choose_better=True, return_train_score=return_train_score
)
# ensemble model
bagged_top3 = [
pycaret.regression.ensemble_model(i, return_train_score=return_train_score)
for i in tuned_top3
]
assert isinstance(bagged_top3, list)
# blend models
blender = pycaret.regression.blend_models(
top3, return_train_score=return_train_score
)
# stack models
stacker = pycaret.regression.stack_models(
estimator_list=top3[1:],
meta_model=top3[0],
return_train_score=return_train_score,
)
# plot model
lr = pycaret.regression.create_model("lr", return_train_score=return_train_score)
pycaret.regression.plot_model(
lr, save=True
) # scale removed because build failed due to large image size
# select best model
best = pycaret.regression.automl(optimize="MAPE")
# hold out predictions
predict_holdout = pycaret.regression.predict_model(best)
assert isinstance(predict_holdout, pd.DataFrame)
# predictions on new dataset
predict_holdout = pycaret.regression.predict_model(best, data=boston_dataframe)
assert isinstance(predict_holdout, pd.DataFrame)
# finalize model
final_best = pycaret.regression.finalize_model(best)
# save model
pycaret.regression.save_model(best, "best_model_23122019")
# load model
saved_best = pycaret.regression.load_model("best_model_23122019")
# returns table of models
all_models = pycaret.regression.models()
assert isinstance(all_models, pd.DataFrame)
# get config
X_train = pycaret.regression.get_config("X_train")
X_test = pycaret.regression.get_config("X_test")
y_train = pycaret.regression.get_config("y_train")
y_test = pycaret.regression.get_config("y_test")
assert isinstance(X_train, pd.DataFrame)
assert isinstance(X_test, pd.DataFrame)
assert isinstance(y_train, pd.Series)
assert isinstance(y_test, pd.Series)
# set config
pycaret.regression.set_config("seed", 124)
seed = pycaret.regression.get_config("seed")
assert seed == 124
assert 1 == 1
def test_regression_predict_on_unseen(boston_dataframe):
exp = pycaret.regression.RegressionExperiment()
# init setup
exp.setup(
boston_dataframe,
target="medv",
remove_multicollinearity=True,
multicollinearity_threshold=0.95,
log_experiment=True,
html=False,
session_id=123,
n_jobs=1,
experiment_name=uuid.uuid4().hex,
)
model = exp.create_model("dt", cross_validation=False)
# save model
exp.save_model(model, "best_model_23122019")
exp = pycaret.regression.RegressionExperiment()
# load model
model = exp.load_model("best_model_23122019")
exp.predict_model(model, boston_dataframe)
class TestRegressionExperimentCustomTags:
def test_regression_setup_fails_with_experiment_custom_tags(self, boston_dataframe):
with pytest.raises(Exception):
# init setup
_ = pycaret.regression.setup(
boston_dataframe,
target="medv",
log_experiment=True,
html=False,
session_id=123,
n_jobs=1,
experiment_name=uuid.uuid4().hex,
experiment_custom_tags="custom_tag",
)
@pytest.mark.parametrize("custom_tag", [1, ("pytest", "True"), True, 1000.0])
def test_regression_setup_fails_with_experiment_custom_multiples_inputs(
self, custom_tag
):
with pytest.raises(Exception):
# init setup
_ = pycaret.regression.setup(
pycaret.datasets.get_data("boston"),
target="medv",
log_experiment=True,
html=False,
session_id=123,
n_jobs=1,
experiment_name=uuid.uuid4().hex,
experiment_custom_tags=custom_tag,
)
def test_regression_models_with_experiment_custom_tags(self, boston_dataframe):
# init setup
experiment_name = uuid.uuid4().hex
_ = pycaret.regression.setup(
boston_dataframe,
target="medv",
log_experiment=True,
html=False,
session_id=123,
n_jobs=1,
experiment_name=experiment_name,
)
_ = pycaret.regression.compare_models(
n_select=100, experiment_custom_tags={"pytest": "testing"}
)[:2]
# get experiment data
tracking_api = MlflowClient()
experiment = [
e for e in tracking_api.list_experiments() if e.name == experiment_name
][0]
experiment_id = experiment.experiment_id
# get run's info
experiment_run = tracking_api.list_run_infos(experiment_id)[0]
# get run id
run_id = experiment_run.run_id
# get run data
run_data = tracking_api.get_run(run_id)
# assert that custom tag was inserted
assert "testing" == run_data.to_dictionary().get("data").get("tags").get(
"pytest"
)
if __name__ == "__main__":
test_regression()
test_regression_predict_on_unseen()
TestRegressionExperimentCustomTags()