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,180 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import warnings
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import sklearn
|
|
7
|
+
from sklearn.base import ClassifierMixin, RegressorMixin
|
|
8
|
+
from sklearn.neural_network import MLPClassifier, MLPRegressor
|
|
9
|
+
|
|
10
|
+
from supervised.algorithms.registry import (
|
|
11
|
+
BINARY_CLASSIFICATION,
|
|
12
|
+
MULTICLASS_CLASSIFICATION,
|
|
13
|
+
REGRESSION,
|
|
14
|
+
AlgorithmsRegistry,
|
|
15
|
+
)
|
|
16
|
+
from supervised.algorithms.sklearn import SklearnAlgorithm
|
|
17
|
+
from supervised.utils.config import LOG_LEVEL
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
logger.setLevel(LOG_LEVEL)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class NNFit(SklearnAlgorithm):
|
|
24
|
+
def file_extension(self):
|
|
25
|
+
return "neural_network"
|
|
26
|
+
|
|
27
|
+
def is_fitted(self):
|
|
28
|
+
return (
|
|
29
|
+
hasattr(self.model, "n_iter_")
|
|
30
|
+
and self.model.n_iter_ is not None
|
|
31
|
+
and self.model.n_iter_ > 0
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def fit(
|
|
35
|
+
self,
|
|
36
|
+
X,
|
|
37
|
+
y,
|
|
38
|
+
sample_weight=None,
|
|
39
|
+
X_validation=None,
|
|
40
|
+
y_validation=None,
|
|
41
|
+
sample_weight_validation=None,
|
|
42
|
+
log_to_file=None,
|
|
43
|
+
max_time=None,
|
|
44
|
+
):
|
|
45
|
+
with warnings.catch_warnings():
|
|
46
|
+
warnings.simplefilter(action="ignore")
|
|
47
|
+
# filter
|
|
48
|
+
# X does not have valid feature names, but MLPClassifier was fitted with feature names
|
|
49
|
+
self.model.fit(X, y)
|
|
50
|
+
|
|
51
|
+
if log_to_file is not None:
|
|
52
|
+
loss_curve = self.model.loss_curve_
|
|
53
|
+
result = pd.DataFrame(
|
|
54
|
+
{
|
|
55
|
+
"iteration": range(len(loss_curve)),
|
|
56
|
+
"train": loss_curve,
|
|
57
|
+
"validation": None,
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
result.to_csv(log_to_file, index=False, header=False)
|
|
61
|
+
|
|
62
|
+
if self.params["ml_task"] != REGRESSION:
|
|
63
|
+
self.classes_ = np.unique(y)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class MLPAlgorithm(ClassifierMixin, NNFit):
|
|
67
|
+
algorithm_name = "Neural Network"
|
|
68
|
+
algorithm_short_name = "Neural Network"
|
|
69
|
+
|
|
70
|
+
def __init__(self, params):
|
|
71
|
+
super(MLPAlgorithm, self).__init__(params)
|
|
72
|
+
logger.debug("MLPAlgorithm.__init__")
|
|
73
|
+
self.max_iters = 1
|
|
74
|
+
self.library_version = sklearn.__version__
|
|
75
|
+
h1 = params.get("dense_1_size", 32)
|
|
76
|
+
h2 = params.get("dense_2_size", 16)
|
|
77
|
+
learning_rate = params.get("learning_rate", 0.05)
|
|
78
|
+
|
|
79
|
+
max_iter = 500
|
|
80
|
+
self.model = MLPClassifier(
|
|
81
|
+
hidden_layer_sizes=(h1, h2),
|
|
82
|
+
activation="relu",
|
|
83
|
+
solver="adam",
|
|
84
|
+
learning_rate=params.get("learning_rate_type", "constant"),
|
|
85
|
+
learning_rate_init=learning_rate,
|
|
86
|
+
alpha=params.get("alpha", 0.0001),
|
|
87
|
+
early_stopping=True,
|
|
88
|
+
n_iter_no_change=50,
|
|
89
|
+
max_iter=max_iter,
|
|
90
|
+
random_state=params.get("seed", 123),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
def get_metric_name(self):
|
|
94
|
+
return "logloss"
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class MLPRegressorAlgorithm(RegressorMixin, NNFit):
|
|
98
|
+
algorithm_name = "Neural Network"
|
|
99
|
+
algorithm_short_name = "Neural Network"
|
|
100
|
+
|
|
101
|
+
def __init__(self, params):
|
|
102
|
+
super(MLPRegressorAlgorithm, self).__init__(params)
|
|
103
|
+
logger.debug("MLPRegressorAlgorithm.__init__")
|
|
104
|
+
self.max_iters = 1
|
|
105
|
+
self.library_version = sklearn.__version__
|
|
106
|
+
h1 = params.get("dense_1_size", 32)
|
|
107
|
+
h2 = params.get("dense_2_size", 16)
|
|
108
|
+
learning_rate = params.get("learning_rate", 0.05)
|
|
109
|
+
momentum = params.get("momentum", 0.9)
|
|
110
|
+
early_stopping = True
|
|
111
|
+
max_iter = 500
|
|
112
|
+
self.model = MLPRegressor(
|
|
113
|
+
hidden_layer_sizes=(h1, h2),
|
|
114
|
+
activation="relu",
|
|
115
|
+
solver="adam",
|
|
116
|
+
learning_rate="constant",
|
|
117
|
+
learning_rate_init=learning_rate,
|
|
118
|
+
momentum=momentum,
|
|
119
|
+
early_stopping=early_stopping,
|
|
120
|
+
max_iter=max_iter,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
def get_metric_name(self):
|
|
124
|
+
return "mse"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
nn_params = {
|
|
128
|
+
"dense_1_size": [16, 32, 64],
|
|
129
|
+
"dense_2_size": [4, 8, 16, 32],
|
|
130
|
+
"learning_rate": [0.01, 0.05, 0.08, 0.1],
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
default_nn_params = {"dense_1_size": 32, "dense_2_size": 16, "learning_rate": 0.05}
|
|
134
|
+
|
|
135
|
+
additional = {"max_rows_limit": None, "max_cols_limit": None}
|
|
136
|
+
|
|
137
|
+
required_preprocessing = [
|
|
138
|
+
"missing_values_inputation",
|
|
139
|
+
"convert_categorical",
|
|
140
|
+
"datetime_transform",
|
|
141
|
+
"text_transform",
|
|
142
|
+
"scale",
|
|
143
|
+
"target_as_integer",
|
|
144
|
+
]
|
|
145
|
+
|
|
146
|
+
AlgorithmsRegistry.add(
|
|
147
|
+
BINARY_CLASSIFICATION,
|
|
148
|
+
MLPAlgorithm,
|
|
149
|
+
nn_params,
|
|
150
|
+
required_preprocessing,
|
|
151
|
+
additional,
|
|
152
|
+
default_nn_params,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
AlgorithmsRegistry.add(
|
|
156
|
+
MULTICLASS_CLASSIFICATION,
|
|
157
|
+
MLPAlgorithm,
|
|
158
|
+
nn_params,
|
|
159
|
+
required_preprocessing,
|
|
160
|
+
additional,
|
|
161
|
+
default_nn_params,
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
required_preprocessing = [
|
|
165
|
+
"missing_values_inputation",
|
|
166
|
+
"convert_categorical",
|
|
167
|
+
"datetime_transform",
|
|
168
|
+
"text_transform",
|
|
169
|
+
"scale",
|
|
170
|
+
"target_scale",
|
|
171
|
+
]
|
|
172
|
+
|
|
173
|
+
AlgorithmsRegistry.add(
|
|
174
|
+
REGRESSION,
|
|
175
|
+
MLPRegressorAlgorithm,
|
|
176
|
+
nn_params,
|
|
177
|
+
required_preprocessing,
|
|
178
|
+
additional,
|
|
179
|
+
default_nn_params,
|
|
180
|
+
)
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import sklearn
|
|
4
|
+
from sklearn.base import ClassifierMixin, RegressorMixin
|
|
5
|
+
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
|
|
6
|
+
|
|
7
|
+
from supervised.algorithms.registry import (
|
|
8
|
+
BINARY_CLASSIFICATION,
|
|
9
|
+
MULTICLASS_CLASSIFICATION,
|
|
10
|
+
REGRESSION,
|
|
11
|
+
AlgorithmsRegistry,
|
|
12
|
+
)
|
|
13
|
+
from supervised.algorithms.sklearn import (
|
|
14
|
+
SklearnTreesEnsembleClassifierAlgorithm,
|
|
15
|
+
SklearnTreesEnsembleRegressorAlgorithm,
|
|
16
|
+
)
|
|
17
|
+
from supervised.utils.config import LOG_LEVEL
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
logger.setLevel(LOG_LEVEL)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class RandomForestAlgorithm(ClassifierMixin, SklearnTreesEnsembleClassifierAlgorithm):
|
|
24
|
+
algorithm_name = "Random Forest"
|
|
25
|
+
algorithm_short_name = "Random Forest"
|
|
26
|
+
|
|
27
|
+
def __init__(self, params):
|
|
28
|
+
super(RandomForestAlgorithm, self).__init__(params)
|
|
29
|
+
logger.debug("RandomForestAlgorithm.__init__")
|
|
30
|
+
|
|
31
|
+
self.library_version = sklearn.__version__
|
|
32
|
+
self.trees_in_step = additional.get("trees_in_step", 5)
|
|
33
|
+
self.max_steps = additional.get("max_steps", 3)
|
|
34
|
+
self.early_stopping_rounds = additional.get("early_stopping_rounds", 50)
|
|
35
|
+
self.model = RandomForestClassifier(
|
|
36
|
+
n_estimators=self.trees_in_step,
|
|
37
|
+
criterion=params.get("criterion", "gini"),
|
|
38
|
+
max_features=params.get("max_features", 0.8),
|
|
39
|
+
max_depth=params.get("max_depth", 6),
|
|
40
|
+
min_samples_split=params.get("min_samples_split", 4),
|
|
41
|
+
min_samples_leaf=params.get("min_samples_leaf", 1),
|
|
42
|
+
warm_start=True,
|
|
43
|
+
n_jobs=params.get("n_jobs", -1),
|
|
44
|
+
random_state=params.get("seed", 1),
|
|
45
|
+
)
|
|
46
|
+
self.max_steps = self.params.get("max_steps", self.max_steps)
|
|
47
|
+
|
|
48
|
+
def file_extension(self):
|
|
49
|
+
return "random_forest"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class RandomForestRegressorAlgorithm(
|
|
53
|
+
RegressorMixin, SklearnTreesEnsembleRegressorAlgorithm
|
|
54
|
+
):
|
|
55
|
+
algorithm_name = "Random Forest"
|
|
56
|
+
algorithm_short_name = "Random Forest"
|
|
57
|
+
|
|
58
|
+
def __init__(self, params):
|
|
59
|
+
super(RandomForestRegressorAlgorithm, self).__init__(params)
|
|
60
|
+
logger.debug("RandomForestRegressorAlgorithm.__init__")
|
|
61
|
+
|
|
62
|
+
self.library_version = sklearn.__version__
|
|
63
|
+
self.trees_in_step = regression_additional.get("trees_in_step", 5)
|
|
64
|
+
self.max_steps = regression_additional.get("max_steps", 3)
|
|
65
|
+
self.early_stopping_rounds = regression_additional.get(
|
|
66
|
+
"early_stopping_rounds", 50
|
|
67
|
+
)
|
|
68
|
+
self.model = RandomForestRegressor(
|
|
69
|
+
n_estimators=self.trees_in_step,
|
|
70
|
+
criterion=params.get("criterion", "squared_error"),
|
|
71
|
+
max_features=params.get("max_features", 0.8),
|
|
72
|
+
max_depth=params.get("max_depth", 6),
|
|
73
|
+
min_samples_split=params.get("min_samples_split", 4),
|
|
74
|
+
min_samples_leaf=params.get("min_samples_leaf", 1),
|
|
75
|
+
warm_start=True,
|
|
76
|
+
n_jobs=params.get("n_jobs", -1),
|
|
77
|
+
random_state=params.get("seed", 1),
|
|
78
|
+
)
|
|
79
|
+
self.max_steps = self.params.get("max_steps", self.max_steps)
|
|
80
|
+
|
|
81
|
+
def file_extension(self):
|
|
82
|
+
return "random_forest"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# For binary classification target should be 0, 1. There should be no NaNs in target.
|
|
86
|
+
rf_params = {
|
|
87
|
+
"criterion": ["gini", "entropy"],
|
|
88
|
+
"max_features": [0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
|
|
89
|
+
"min_samples_split": [10, 20, 30, 40, 50],
|
|
90
|
+
"max_depth": [3, 4, 5, 6, 7],
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
classification_default_params = {
|
|
94
|
+
"criterion": "gini",
|
|
95
|
+
"max_features": 0.9,
|
|
96
|
+
"min_samples_split": 30,
|
|
97
|
+
"max_depth": 4,
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
additional = {
|
|
102
|
+
"trees_in_step": 100,
|
|
103
|
+
"train_cant_improve_limit": 1,
|
|
104
|
+
"min_steps": 1,
|
|
105
|
+
"max_steps": 50,
|
|
106
|
+
"early_stopping_rounds": 50,
|
|
107
|
+
"max_rows_limit": None,
|
|
108
|
+
"max_cols_limit": None,
|
|
109
|
+
}
|
|
110
|
+
required_preprocessing = [
|
|
111
|
+
"missing_values_inputation",
|
|
112
|
+
"convert_categorical",
|
|
113
|
+
"datetime_transform",
|
|
114
|
+
"text_transform",
|
|
115
|
+
"target_as_integer",
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
AlgorithmsRegistry.add(
|
|
119
|
+
BINARY_CLASSIFICATION,
|
|
120
|
+
RandomForestAlgorithm,
|
|
121
|
+
rf_params,
|
|
122
|
+
required_preprocessing,
|
|
123
|
+
additional,
|
|
124
|
+
classification_default_params,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
AlgorithmsRegistry.add(
|
|
128
|
+
MULTICLASS_CLASSIFICATION,
|
|
129
|
+
RandomForestAlgorithm,
|
|
130
|
+
rf_params,
|
|
131
|
+
required_preprocessing,
|
|
132
|
+
additional,
|
|
133
|
+
classification_default_params,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
#
|
|
138
|
+
# REGRESSION
|
|
139
|
+
#
|
|
140
|
+
|
|
141
|
+
regression_rf_params = {
|
|
142
|
+
"criterion": [
|
|
143
|
+
"squared_error"
|
|
144
|
+
], # remove "mae" because it slows down a lot https://github.com/scikit-learn/scikit-learn/issues/9626
|
|
145
|
+
"max_features": [0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
|
|
146
|
+
"min_samples_split": [10, 20, 30, 40, 50],
|
|
147
|
+
"max_depth": [3, 4, 5, 6, 7],
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
regression_default_params = {
|
|
151
|
+
"criterion": "squared_error",
|
|
152
|
+
"max_features": 0.9,
|
|
153
|
+
"min_samples_split": 30,
|
|
154
|
+
"max_depth": 4,
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
regression_additional = {
|
|
158
|
+
"trees_in_step": 100,
|
|
159
|
+
"train_cant_improve_limit": 1,
|
|
160
|
+
"min_steps": 1,
|
|
161
|
+
"max_steps": 50,
|
|
162
|
+
"early_stopping_rounds": 50,
|
|
163
|
+
"max_rows_limit": None,
|
|
164
|
+
"max_cols_limit": None,
|
|
165
|
+
}
|
|
166
|
+
regression_required_preprocessing = [
|
|
167
|
+
"missing_values_inputation",
|
|
168
|
+
"convert_categorical",
|
|
169
|
+
"datetime_transform",
|
|
170
|
+
"text_transform",
|
|
171
|
+
"target_scale",
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
AlgorithmsRegistry.add(
|
|
175
|
+
REGRESSION,
|
|
176
|
+
RandomForestRegressorAlgorithm,
|
|
177
|
+
regression_rf_params,
|
|
178
|
+
regression_required_preprocessing,
|
|
179
|
+
regression_additional,
|
|
180
|
+
regression_default_params,
|
|
181
|
+
)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# tasks that can be handled by the package
|
|
2
|
+
BINARY_CLASSIFICATION = "binary_classification"
|
|
3
|
+
MULTICLASS_CLASSIFICATION = "multiclass_classification"
|
|
4
|
+
REGRESSION = "regression"
|
|
5
|
+
|
|
6
|
+
class AlgorithmsRegistry:
|
|
7
|
+
registry = {
|
|
8
|
+
BINARY_CLASSIFICATION: {},
|
|
9
|
+
MULTICLASS_CLASSIFICATION: {},
|
|
10
|
+
REGRESSION: {},
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
@staticmethod
|
|
14
|
+
def add(
|
|
15
|
+
task_name,
|
|
16
|
+
model_class,
|
|
17
|
+
model_params,
|
|
18
|
+
required_preprocessing,
|
|
19
|
+
additional,
|
|
20
|
+
default_params,
|
|
21
|
+
):
|
|
22
|
+
model_information = {
|
|
23
|
+
"class": model_class,
|
|
24
|
+
"params": model_params,
|
|
25
|
+
"required_preprocessing": required_preprocessing,
|
|
26
|
+
"additional": additional,
|
|
27
|
+
"default_params": default_params,
|
|
28
|
+
}
|
|
29
|
+
AlgorithmsRegistry.registry[task_name][
|
|
30
|
+
model_class.algorithm_short_name
|
|
31
|
+
] = model_information
|
|
32
|
+
|
|
33
|
+
@staticmethod
|
|
34
|
+
def get_supported_ml_tasks():
|
|
35
|
+
return AlgorithmsRegistry.registry.keys()
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def get_algorithm_class(ml_task, algorithm_name):
|
|
39
|
+
return AlgorithmsRegistry.registry[ml_task][algorithm_name]["class"]
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def get_long_name(ml_task, algorithm_name):
|
|
43
|
+
return AlgorithmsRegistry.registry[ml_task][algorithm_name][
|
|
44
|
+
"class"
|
|
45
|
+
].algorithm_name
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def get_max_rows_limit(ml_task, algorithm_name):
|
|
49
|
+
return AlgorithmsRegistry.registry[ml_task][algorithm_name]["additional"][
|
|
50
|
+
"max_rows_limit"
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def get_max_cols_limit(ml_task, algorithm_name):
|
|
55
|
+
return AlgorithmsRegistry.registry[ml_task][algorithm_name]["additional"][
|
|
56
|
+
"max_cols_limit"
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def get_eval_metric(algorithm_name, ml_task, automl_eval_metric):
|
|
61
|
+
if algorithm_name == "Xgboost":
|
|
62
|
+
return xgboost_eval_metric(ml_task, automl_eval_metric)
|
|
63
|
+
|
|
64
|
+
return automl_eval_metric
|
|
65
|
+
|
|
66
|
+
# Import algorithm to be registered
|
|
67
|
+
import supervised.algorithms.baseline
|
|
68
|
+
import supervised.algorithms.catboost
|
|
69
|
+
import supervised.algorithms.decision_tree
|
|
70
|
+
import supervised.algorithms.extra_trees
|
|
71
|
+
import supervised.algorithms.knn
|
|
72
|
+
import supervised.algorithms.lightgbm
|
|
73
|
+
import supervised.algorithms.linear
|
|
74
|
+
import supervised.algorithms.nn
|
|
75
|
+
import supervised.algorithms.random_forest
|
|
76
|
+
import supervised.algorithms.xgboost
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
import logging
|
|
3
|
+
import time
|
|
4
|
+
import warnings
|
|
5
|
+
|
|
6
|
+
import joblib
|
|
7
|
+
import numpy as np
|
|
8
|
+
import pandas as pd
|
|
9
|
+
|
|
10
|
+
from supervised.algorithms.algorithm import BaseAlgorithm
|
|
11
|
+
from supervised.algorithms.registry import (
|
|
12
|
+
BINARY_CLASSIFICATION,
|
|
13
|
+
MULTICLASS_CLASSIFICATION,
|
|
14
|
+
REGRESSION,
|
|
15
|
+
)
|
|
16
|
+
from supervised.utils.config import LOG_LEVEL
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger(__name__)
|
|
19
|
+
logger.setLevel(LOG_LEVEL)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class SklearnAlgorithm(BaseAlgorithm):
|
|
23
|
+
def __init__(self, params):
|
|
24
|
+
super(SklearnAlgorithm, self).__init__(params)
|
|
25
|
+
|
|
26
|
+
def fit(
|
|
27
|
+
self,
|
|
28
|
+
X,
|
|
29
|
+
y,
|
|
30
|
+
sample_weight=None,
|
|
31
|
+
X_validation=None,
|
|
32
|
+
y_validation=None,
|
|
33
|
+
sample_weight_validation=None,
|
|
34
|
+
log_to_file=None,
|
|
35
|
+
max_time=None,
|
|
36
|
+
):
|
|
37
|
+
with warnings.catch_warnings():
|
|
38
|
+
warnings.simplefilter(action="ignore")
|
|
39
|
+
self.model.fit(X, y, sample_weight=sample_weight)
|
|
40
|
+
if self.params["ml_task"] != REGRESSION:
|
|
41
|
+
self.classes_ = np.unique(y)
|
|
42
|
+
|
|
43
|
+
def copy(self):
|
|
44
|
+
return copy.deepcopy(self)
|
|
45
|
+
|
|
46
|
+
def save(self, model_file_path):
|
|
47
|
+
logger.debug("SklearnAlgorithm save to {0}".format(model_file_path))
|
|
48
|
+
joblib.dump(self.model, model_file_path, compress=True)
|
|
49
|
+
self.model_file_path = model_file_path
|
|
50
|
+
|
|
51
|
+
def load(self, model_file_path):
|
|
52
|
+
logger.debug("SklearnAlgorithm loading model from {0}".format(model_file_path))
|
|
53
|
+
self.model = joblib.load(model_file_path)
|
|
54
|
+
self.model_file_path = model_file_path
|
|
55
|
+
|
|
56
|
+
def is_fitted(self):
|
|
57
|
+
return (
|
|
58
|
+
hasattr(self.model, "n_features_in_")
|
|
59
|
+
and self.model.n_features_in_ is not None
|
|
60
|
+
and self.model.n_features_in_ > 0
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
def predict(self, X):
|
|
64
|
+
self.reload()
|
|
65
|
+
if self.params["ml_task"] == BINARY_CLASSIFICATION:
|
|
66
|
+
return self.model.predict_proba(X)[:, 1]
|
|
67
|
+
elif self.params["ml_task"] == MULTICLASS_CLASSIFICATION:
|
|
68
|
+
return self.model.predict_proba(X)
|
|
69
|
+
return self.model.predict(X)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
from supervised.utils.metric import Metric
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def predict_proba_function_binary(estimator, X):
|
|
76
|
+
return estimator.predict_proba(X)[:, 1]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def predict_proba_function_multiclass(estimator, X):
|
|
80
|
+
return estimator.predict_proba(X)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class SklearnTreesEnsembleClassifierAlgorithm(SklearnAlgorithm):
|
|
84
|
+
def __init__(self, params):
|
|
85
|
+
super(SklearnTreesEnsembleClassifierAlgorithm, self).__init__(params)
|
|
86
|
+
self.log_metric = Metric(
|
|
87
|
+
{"name": self.params.get("eval_metric_name", "logloss")}
|
|
88
|
+
)
|
|
89
|
+
self.max_iters = (
|
|
90
|
+
1 # max iters is used by model_framework, max_steps is used internally
|
|
91
|
+
)
|
|
92
|
+
if params.get("ml_task") == BINARY_CLASSIFICATION:
|
|
93
|
+
self.predict_function = predict_proba_function_binary
|
|
94
|
+
else:
|
|
95
|
+
self.predict_function = predict_proba_function_multiclass
|
|
96
|
+
|
|
97
|
+
def fit(
|
|
98
|
+
self,
|
|
99
|
+
X,
|
|
100
|
+
y,
|
|
101
|
+
sample_weight=None,
|
|
102
|
+
X_validation=None,
|
|
103
|
+
y_validation=None,
|
|
104
|
+
sample_weight_validation=None,
|
|
105
|
+
log_to_file=None,
|
|
106
|
+
max_time=None,
|
|
107
|
+
):
|
|
108
|
+
max_steps = self.max_steps
|
|
109
|
+
n_estimators = 0
|
|
110
|
+
|
|
111
|
+
min_val = 10e12
|
|
112
|
+
min_e = 0
|
|
113
|
+
|
|
114
|
+
p_tr, p_vd = None, None
|
|
115
|
+
result = {"iteration": [], "train": [], "validation": []}
|
|
116
|
+
|
|
117
|
+
start_time = time.time()
|
|
118
|
+
with warnings.catch_warnings():
|
|
119
|
+
warnings.simplefilter(action="ignore")
|
|
120
|
+
|
|
121
|
+
for i in range(max_steps):
|
|
122
|
+
self.model.fit(X, np.ravel(y), sample_weight=sample_weight)
|
|
123
|
+
self.model.n_estimators += self.trees_in_step
|
|
124
|
+
|
|
125
|
+
if X_validation is None or y_validation is None:
|
|
126
|
+
continue
|
|
127
|
+
estimators = self.model.estimators_
|
|
128
|
+
|
|
129
|
+
stop = False
|
|
130
|
+
for e in range(n_estimators, len(estimators)):
|
|
131
|
+
p = self.predict_function(estimators[e], X)
|
|
132
|
+
if p_tr is None:
|
|
133
|
+
p_tr = p
|
|
134
|
+
else:
|
|
135
|
+
p_tr += p
|
|
136
|
+
|
|
137
|
+
p = self.predict_function(estimators[e], X_validation)
|
|
138
|
+
if p_vd is None:
|
|
139
|
+
p_vd = p
|
|
140
|
+
else:
|
|
141
|
+
p_vd += p
|
|
142
|
+
|
|
143
|
+
tr = self.log_metric(
|
|
144
|
+
y, p_tr / float(e + 1), sample_weight=sample_weight
|
|
145
|
+
)
|
|
146
|
+
vd = self.log_metric(
|
|
147
|
+
y_validation,
|
|
148
|
+
p_vd / float(e + 1),
|
|
149
|
+
sample_weight=sample_weight_validation,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if vd < min_val: # optimize direction
|
|
153
|
+
min_val = vd
|
|
154
|
+
min_e = e
|
|
155
|
+
|
|
156
|
+
if e - min_e >= self.early_stopping_rounds:
|
|
157
|
+
stop = True
|
|
158
|
+
break
|
|
159
|
+
|
|
160
|
+
result["iteration"] += [e]
|
|
161
|
+
result["train"] += [tr]
|
|
162
|
+
result["validation"] += [vd]
|
|
163
|
+
|
|
164
|
+
# disable for now ...
|
|
165
|
+
# if max_time is not None and time.time()-start_time > max_time:
|
|
166
|
+
# stop = True
|
|
167
|
+
|
|
168
|
+
if stop:
|
|
169
|
+
self.model.estimators_ = estimators[: (min_e + 1)]
|
|
170
|
+
break
|
|
171
|
+
n_estimators = len(estimators)
|
|
172
|
+
|
|
173
|
+
if log_to_file is not None:
|
|
174
|
+
df_result = pd.DataFrame(result)
|
|
175
|
+
if self.log_metric.is_negative():
|
|
176
|
+
df_result["train"] *= -1.0
|
|
177
|
+
df_result["validation"] *= -1.0
|
|
178
|
+
df_result.to_csv(log_to_file, index=False, header=False)
|
|
179
|
+
|
|
180
|
+
self.classes_ = np.unique(y)
|
|
181
|
+
|
|
182
|
+
def get_metric_name(self):
|
|
183
|
+
return self.params.get("eval_metric_name", "logloss")
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def predict_function(estimator, X):
|
|
187
|
+
return estimator.predict(X)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class SklearnTreesEnsembleRegressorAlgorithm(SklearnTreesEnsembleClassifierAlgorithm):
|
|
191
|
+
def __init__(self, params):
|
|
192
|
+
super(SklearnTreesEnsembleRegressorAlgorithm, self).__init__(params)
|
|
193
|
+
self.log_metric = Metric({"name": self.params.get("eval_metric_name", "rmse")})
|
|
194
|
+
self.predict_function = predict_function
|
|
195
|
+
|
|
196
|
+
def get_metric_name(self):
|
|
197
|
+
return self.params.get("eval_metric_name", "rmse")
|