mljar-supervised 1.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mljar_supervised-1.2.1.dist-info/METADATA +657 -0
- mljar_supervised-1.2.1.dist-info/RECORD +94 -0
- mljar_supervised-1.2.1.dist-info/WHEEL +5 -0
- mljar_supervised-1.2.1.dist-info/licenses/LICENSE +21 -0
- mljar_supervised-1.2.1.dist-info/top_level.txt +1 -0
- supervised/__init__.py +14 -0
- supervised/algorithms/__init__.py +0 -0
- supervised/algorithms/algorithm.py +151 -0
- supervised/algorithms/baseline.py +90 -0
- supervised/algorithms/catboost.py +436 -0
- supervised/algorithms/decision_tree.py +305 -0
- supervised/algorithms/extra_trees.py +176 -0
- supervised/algorithms/factory.py +28 -0
- supervised/algorithms/knn.py +137 -0
- supervised/algorithms/lightgbm.py +406 -0
- supervised/algorithms/linear.py +211 -0
- supervised/algorithms/nn.py +180 -0
- supervised/algorithms/random_forest.py +181 -0
- supervised/algorithms/registry.py +76 -0
- supervised/algorithms/sklearn.py +197 -0
- supervised/algorithms/xgboost.py +413 -0
- supervised/automl.py +568 -0
- supervised/base_automl.py +2542 -0
- supervised/callbacks/__init__.py +0 -0
- supervised/callbacks/callback.py +25 -0
- supervised/callbacks/callback_list.py +33 -0
- supervised/callbacks/early_stopping.py +213 -0
- supervised/callbacks/learner_time_constraint.py +47 -0
- supervised/callbacks/max_iters_constraint.py +16 -0
- supervised/callbacks/metric_logger.py +48 -0
- supervised/callbacks/terminate_on_nan.py +28 -0
- supervised/callbacks/total_time_constraint.py +79 -0
- supervised/ensemble.py +583 -0
- supervised/exceptions.py +21 -0
- supervised/fairness/__init__.py +0 -0
- supervised/fairness/metrics.py +654 -0
- supervised/fairness/optimization.py +414 -0
- supervised/fairness/plots.py +131 -0
- supervised/fairness/report.py +116 -0
- supervised/fairness/utils.py +40 -0
- supervised/model_framework.py +734 -0
- supervised/preprocessing/__init__.py +0 -0
- supervised/preprocessing/datetime_transformer.py +106 -0
- supervised/preprocessing/encoding_selector.py +30 -0
- supervised/preprocessing/exclude_missing_target.py +46 -0
- supervised/preprocessing/goldenfeatures_transformer.py +310 -0
- supervised/preprocessing/kmeans_transformer.py +115 -0
- supervised/preprocessing/label_binarizer.py +81 -0
- supervised/preprocessing/label_encoder.py +54 -0
- supervised/preprocessing/preprocessing.py +652 -0
- supervised/preprocessing/preprocessing_categorical.py +117 -0
- supervised/preprocessing/preprocessing_missing.py +97 -0
- supervised/preprocessing/preprocessing_utils.py +136 -0
- supervised/preprocessing/scale.py +92 -0
- supervised/preprocessing/text_transformer.py +73 -0
- supervised/tuner/__init__.py +0 -0
- supervised/tuner/data_info.py +64 -0
- supervised/tuner/hill_climbing.py +80 -0
- supervised/tuner/mljar_tuner.py +1501 -0
- supervised/tuner/optuna/__init__.py +0 -0
- supervised/tuner/optuna/catboost.py +148 -0
- supervised/tuner/optuna/extra_trees.py +80 -0
- supervised/tuner/optuna/knn.py +64 -0
- supervised/tuner/optuna/lightgbm.py +165 -0
- supervised/tuner/optuna/nn.py +66 -0
- supervised/tuner/optuna/random_forest.py +77 -0
- supervised/tuner/optuna/tuner.py +335 -0
- supervised/tuner/optuna/xgboost.py +130 -0
- supervised/tuner/preprocessing_tuner.py +137 -0
- supervised/tuner/random_parameters.py +22 -0
- supervised/tuner/time_controller.py +273 -0
- supervised/utils/__init__.py +11 -0
- supervised/utils/additional_metrics.py +955 -0
- supervised/utils/additional_plots.py +228 -0
- supervised/utils/automl_plots.py +368 -0
- supervised/utils/common.py +39 -0
- supervised/utils/config.py +25 -0
- supervised/utils/constants.py +4 -0
- supervised/utils/data_validation.py +48 -0
- supervised/utils/importance.py +93 -0
- supervised/utils/jsonencoder.py +33 -0
- supervised/utils/leaderboard_plots.py +137 -0
- supervised/utils/learning_curves.py +145 -0
- supervised/utils/metric.py +450 -0
- supervised/utils/report_structured.py +525 -0
- supervised/utils/shap.py +373 -0
- supervised/utils/subsample.py +17 -0
- supervised/utils/utils.py +29 -0
- supervised/validation/__init__.py +0 -0
- supervised/validation/validation_step.py +37 -0
- supervised/validation/validator_base.py +17 -0
- supervised/validation/validator_custom.py +112 -0
- supervised/validation/validator_kfold.py +153 -0
- supervised/validation/validator_split.py +143 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import time
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
from supervised.utils.config import LOG_LEVEL
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
logger.setLevel(LOG_LEVEL)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TimeController:
|
|
13
|
+
def __init__(
|
|
14
|
+
self, start_time, total_time_limit, model_time_limit, steps, algorithms
|
|
15
|
+
):
|
|
16
|
+
self._start_time = start_time
|
|
17
|
+
self._total_time_limit = total_time_limit
|
|
18
|
+
self._model_time_limit = model_time_limit
|
|
19
|
+
self._steps = steps
|
|
20
|
+
self._algorithms = algorithms
|
|
21
|
+
self._spend = []
|
|
22
|
+
self._is_hill_climbing = "hill_climbing_1" in steps
|
|
23
|
+
self._is_stacking = "stack" in steps
|
|
24
|
+
|
|
25
|
+
def to_json(self):
|
|
26
|
+
return {
|
|
27
|
+
"total_time_limit": self._total_time_limit,
|
|
28
|
+
"model_time_limit": self._model_time_limit,
|
|
29
|
+
"steps": self._steps,
|
|
30
|
+
"algorithms": self._algorithms,
|
|
31
|
+
"spend": self._spend,
|
|
32
|
+
"is_hill_climbing": self._is_hill_climbing,
|
|
33
|
+
"is_stacking": self._is_stacking,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@staticmethod
|
|
37
|
+
def from_json(data):
|
|
38
|
+
if data is None:
|
|
39
|
+
return None
|
|
40
|
+
try:
|
|
41
|
+
total_time_limit = data.get("total_time_limit")
|
|
42
|
+
model_time_limit = data.get("model_time_limit")
|
|
43
|
+
steps = data.get("steps")
|
|
44
|
+
algorithms = data.get("algorithms")
|
|
45
|
+
|
|
46
|
+
tc = TimeController(
|
|
47
|
+
time.time(), total_time_limit, model_time_limit, steps, algorithms
|
|
48
|
+
)
|
|
49
|
+
tc._spend = data.get("spend")
|
|
50
|
+
tc._start_time -= tc.already_spend() # update time with already spend
|
|
51
|
+
return tc
|
|
52
|
+
except Exception as e:
|
|
53
|
+
logger.error(f"Cant load TimeController from json, {str(e)}")
|
|
54
|
+
pass
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
def already_spend(self):
|
|
58
|
+
return np.sum([s["train_time"] for s in self._spend])
|
|
59
|
+
|
|
60
|
+
def time_should_use(self, fit_level):
|
|
61
|
+
if self._total_time_limit is None:
|
|
62
|
+
return 7 * 24 * 3600 # 7 days
|
|
63
|
+
|
|
64
|
+
ratios = {
|
|
65
|
+
"default_algorithms": 0.3,
|
|
66
|
+
"not_so_random": 0.35,
|
|
67
|
+
"mix_encoding": 0.05,
|
|
68
|
+
"golden_features": 0.05,
|
|
69
|
+
"kmeans_features": 0.05,
|
|
70
|
+
"insert_random_feature": 0.05,
|
|
71
|
+
"features_selection": 0.05,
|
|
72
|
+
"hill_climbing_1": 0.2, # enough to have only first step from hill climbing
|
|
73
|
+
"boost_on_errors": 0.05,
|
|
74
|
+
"stack": 0.2,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (
|
|
78
|
+
fit_level
|
|
79
|
+
in [
|
|
80
|
+
"default_algorithms",
|
|
81
|
+
"not_so_random",
|
|
82
|
+
"boost_on_errors",
|
|
83
|
+
"mix_encoding",
|
|
84
|
+
"golden_features",
|
|
85
|
+
"kmeans_features",
|
|
86
|
+
"insert_random_feature",
|
|
87
|
+
"features_selection",
|
|
88
|
+
"stack",
|
|
89
|
+
]
|
|
90
|
+
or "hill_climbing" in fit_level
|
|
91
|
+
):
|
|
92
|
+
ratio = 0
|
|
93
|
+
for k, v in ratios.items():
|
|
94
|
+
if k in self._steps:
|
|
95
|
+
ratio += v
|
|
96
|
+
|
|
97
|
+
fl = fit_level
|
|
98
|
+
if "hill_climbing" in fit_level:
|
|
99
|
+
fl = "hill_climbing_1"
|
|
100
|
+
|
|
101
|
+
ratio = ratios[fl] / ratio
|
|
102
|
+
|
|
103
|
+
if "hill_climbing" in fit_level:
|
|
104
|
+
# print("before hill climbing scale", ratio)
|
|
105
|
+
hill_climbing_cnt = len(
|
|
106
|
+
[i for i in self._steps if "hill_climbing" in i]
|
|
107
|
+
)
|
|
108
|
+
ratio /= float(hill_climbing_cnt)
|
|
109
|
+
|
|
110
|
+
should_use = self._total_time_limit * ratio
|
|
111
|
+
|
|
112
|
+
return should_use
|
|
113
|
+
|
|
114
|
+
return 0
|
|
115
|
+
|
|
116
|
+
def compound_time_should_use(self, fit_level):
|
|
117
|
+
compound = 0
|
|
118
|
+
for step in self._steps:
|
|
119
|
+
if step in [
|
|
120
|
+
"adjust_validation",
|
|
121
|
+
"simple_algorithms",
|
|
122
|
+
# "default_algorithms",
|
|
123
|
+
"ensemble",
|
|
124
|
+
"ensemble_stacked",
|
|
125
|
+
]:
|
|
126
|
+
continue
|
|
127
|
+
time_should_use = self.time_should_use(step)
|
|
128
|
+
compound += time_should_use
|
|
129
|
+
|
|
130
|
+
if fit_level == step:
|
|
131
|
+
break
|
|
132
|
+
# if fit_level == "stack":
|
|
133
|
+
# compound -= 120 # leave time for ensemble
|
|
134
|
+
# maybe not needed
|
|
135
|
+
return compound
|
|
136
|
+
|
|
137
|
+
def enough_time_for_step(self, fit_level):
|
|
138
|
+
if fit_level in ["ensemble", "ensemble_stacked", "fairness"]:
|
|
139
|
+
return True
|
|
140
|
+
total_time_spend = time.time() - self._start_time
|
|
141
|
+
compound = self.compound_time_should_use(fit_level)
|
|
142
|
+
# print("Enough time for step", fit_level, np.round(total_time_spend,2), np.round(compound,2))
|
|
143
|
+
if total_time_spend > compound:
|
|
144
|
+
# dont train more
|
|
145
|
+
return False
|
|
146
|
+
|
|
147
|
+
return True
|
|
148
|
+
|
|
149
|
+
def enough_time_for_model(self, model_type):
|
|
150
|
+
if self._total_time_limit is None:
|
|
151
|
+
return True
|
|
152
|
+
|
|
153
|
+
time_left = self._total_time_limit - self.already_spend()
|
|
154
|
+
spend = [s["train_time"] for s in self._spend if s["model_type"] == model_type]
|
|
155
|
+
model_mean_spend = np.mean(spend)
|
|
156
|
+
return model_mean_spend <= time_left
|
|
157
|
+
|
|
158
|
+
def enough_time(self, model_type, step):
|
|
159
|
+
"""
|
|
160
|
+
Check if there is enough time to train the next model.
|
|
161
|
+
|
|
162
|
+
Parameters
|
|
163
|
+
----------
|
|
164
|
+
model_type : str
|
|
165
|
+
String with type of the model.
|
|
166
|
+
|
|
167
|
+
step: str
|
|
168
|
+
String with name of the step in the process of AutoML training.
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
Returns
|
|
172
|
+
-------
|
|
173
|
+
bool
|
|
174
|
+
`True` if there is time for training next model, `False` otherwise.
|
|
175
|
+
"""
|
|
176
|
+
if step in ["ensemble", "ensemble_stacked"]:
|
|
177
|
+
return True
|
|
178
|
+
# if model_time_limit is set, train every model
|
|
179
|
+
# do not apply total_time_limit
|
|
180
|
+
if self._model_time_limit is not None:
|
|
181
|
+
return True
|
|
182
|
+
# no total time limit, just train, dont ask
|
|
183
|
+
if self._total_time_limit is None:
|
|
184
|
+
return True
|
|
185
|
+
|
|
186
|
+
total_time_spend = time.time() - self._start_time
|
|
187
|
+
time_left = self._total_time_limit - total_time_spend
|
|
188
|
+
# no time left, do not train any more models, sorry ...
|
|
189
|
+
if time_left < 0:
|
|
190
|
+
# print("No time left", time_left)
|
|
191
|
+
return False
|
|
192
|
+
|
|
193
|
+
# check the fit level type
|
|
194
|
+
# we dont want to spend too much time on one step
|
|
195
|
+
if not self.enough_time_for_step(step):
|
|
196
|
+
# print("Not enough time for step", step)
|
|
197
|
+
return False
|
|
198
|
+
|
|
199
|
+
# there is still time and model_type was not tested yet
|
|
200
|
+
# we should try it
|
|
201
|
+
if time_left > 0 and self.model_spend(model_type) == 0:
|
|
202
|
+
return True
|
|
203
|
+
|
|
204
|
+
# stacked models converge faster
|
|
205
|
+
# dont need to check ...
|
|
206
|
+
if step == "stack":
|
|
207
|
+
return True
|
|
208
|
+
# check if there is enough time for model to train
|
|
209
|
+
return self.enough_time_for_model(model_type)
|
|
210
|
+
|
|
211
|
+
def learner_time_limit(self, model_type, fit_level, k_folds):
|
|
212
|
+
if self._total_time_limit is None:
|
|
213
|
+
return 7 * 24 * 3600
|
|
214
|
+
|
|
215
|
+
if self._model_time_limit is not None:
|
|
216
|
+
return self._model_time_limit / k_folds
|
|
217
|
+
|
|
218
|
+
# just train them ...
|
|
219
|
+
if fit_level == "simple_algorithms":
|
|
220
|
+
return None
|
|
221
|
+
if fit_level == "default_algorithms":
|
|
222
|
+
return None
|
|
223
|
+
|
|
224
|
+
tune_algorithms = [
|
|
225
|
+
a
|
|
226
|
+
for a in self._algorithms
|
|
227
|
+
if a not in ["Baseline", "Linear", "Decision Tree", "Nearest Neighbors"]
|
|
228
|
+
]
|
|
229
|
+
tune_algs_cnt = len(tune_algorithms)
|
|
230
|
+
if tune_algs_cnt == 0:
|
|
231
|
+
return None
|
|
232
|
+
|
|
233
|
+
time_elapsed = time.time() - self._start_time
|
|
234
|
+
time_left = self._total_time_limit - time_elapsed
|
|
235
|
+
|
|
236
|
+
if fit_level == "not_so_random":
|
|
237
|
+
tt = self.time_should_use(fit_level)
|
|
238
|
+
|
|
239
|
+
tt /= tune_algs_cnt # give time equally for each algorithm
|
|
240
|
+
tt /= k_folds # time is per learner (per fold)
|
|
241
|
+
return tt
|
|
242
|
+
|
|
243
|
+
if "hill_climbing" in fit_level:
|
|
244
|
+
tt = self.time_should_use(fit_level)
|
|
245
|
+
tt /= tune_algs_cnt # give time equally for each algorithm
|
|
246
|
+
tt /= k_folds # time is per learner (per fold)
|
|
247
|
+
return tt
|
|
248
|
+
|
|
249
|
+
if self._is_stacking and fit_level == "stack":
|
|
250
|
+
tt = time_left
|
|
251
|
+
tt /= tune_algs_cnt # give time equally for each algorithm
|
|
252
|
+
tt /= k_folds # time is per learner (per fold)
|
|
253
|
+
return tt
|
|
254
|
+
|
|
255
|
+
def log_time(self, model_name, model_type, fit_level, train_time):
|
|
256
|
+
self._spend += [
|
|
257
|
+
{
|
|
258
|
+
"model_name": model_name,
|
|
259
|
+
"model_type": model_type,
|
|
260
|
+
"fit_level": fit_level,
|
|
261
|
+
"train_time": train_time,
|
|
262
|
+
}
|
|
263
|
+
]
|
|
264
|
+
# print(pd.DataFrame(self._spend))
|
|
265
|
+
# print("Already spend", self.already_spend())
|
|
266
|
+
|
|
267
|
+
def step_spend(self, step):
|
|
268
|
+
return np.sum([s["train_time"] for s in self._spend if s["fit_level"] == step])
|
|
269
|
+
|
|
270
|
+
def model_spend(self, model_type):
|
|
271
|
+
return np.sum(
|
|
272
|
+
[s["train_time"] for s in self._spend if s["model_type"] == model_type]
|
|
273
|
+
)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from supervised.utils.jsonencoder import MLJSONEncoder
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def json_loads(data, *args, **kwargs):
|
|
7
|
+
return json.loads(data, *args, **kwargs)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def json_dumps(data, *args, **kwargs):
|
|
11
|
+
return json.dumps(data, cls=MLJSONEncoder, *args, **kwargs)
|