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.
Files changed (94) hide show
  1. mljar_supervised-1.2.1.dist-info/METADATA +657 -0
  2. mljar_supervised-1.2.1.dist-info/RECORD +94 -0
  3. mljar_supervised-1.2.1.dist-info/WHEEL +5 -0
  4. mljar_supervised-1.2.1.dist-info/licenses/LICENSE +21 -0
  5. mljar_supervised-1.2.1.dist-info/top_level.txt +1 -0
  6. supervised/__init__.py +14 -0
  7. supervised/algorithms/__init__.py +0 -0
  8. supervised/algorithms/algorithm.py +151 -0
  9. supervised/algorithms/baseline.py +90 -0
  10. supervised/algorithms/catboost.py +436 -0
  11. supervised/algorithms/decision_tree.py +305 -0
  12. supervised/algorithms/extra_trees.py +176 -0
  13. supervised/algorithms/factory.py +28 -0
  14. supervised/algorithms/knn.py +137 -0
  15. supervised/algorithms/lightgbm.py +406 -0
  16. supervised/algorithms/linear.py +211 -0
  17. supervised/algorithms/nn.py +180 -0
  18. supervised/algorithms/random_forest.py +181 -0
  19. supervised/algorithms/registry.py +76 -0
  20. supervised/algorithms/sklearn.py +197 -0
  21. supervised/algorithms/xgboost.py +413 -0
  22. supervised/automl.py +568 -0
  23. supervised/base_automl.py +2542 -0
  24. supervised/callbacks/__init__.py +0 -0
  25. supervised/callbacks/callback.py +25 -0
  26. supervised/callbacks/callback_list.py +33 -0
  27. supervised/callbacks/early_stopping.py +213 -0
  28. supervised/callbacks/learner_time_constraint.py +47 -0
  29. supervised/callbacks/max_iters_constraint.py +16 -0
  30. supervised/callbacks/metric_logger.py +48 -0
  31. supervised/callbacks/terminate_on_nan.py +28 -0
  32. supervised/callbacks/total_time_constraint.py +79 -0
  33. supervised/ensemble.py +583 -0
  34. supervised/exceptions.py +21 -0
  35. supervised/fairness/__init__.py +0 -0
  36. supervised/fairness/metrics.py +654 -0
  37. supervised/fairness/optimization.py +414 -0
  38. supervised/fairness/plots.py +131 -0
  39. supervised/fairness/report.py +116 -0
  40. supervised/fairness/utils.py +40 -0
  41. supervised/model_framework.py +734 -0
  42. supervised/preprocessing/__init__.py +0 -0
  43. supervised/preprocessing/datetime_transformer.py +106 -0
  44. supervised/preprocessing/encoding_selector.py +30 -0
  45. supervised/preprocessing/exclude_missing_target.py +46 -0
  46. supervised/preprocessing/goldenfeatures_transformer.py +310 -0
  47. supervised/preprocessing/kmeans_transformer.py +115 -0
  48. supervised/preprocessing/label_binarizer.py +81 -0
  49. supervised/preprocessing/label_encoder.py +54 -0
  50. supervised/preprocessing/preprocessing.py +652 -0
  51. supervised/preprocessing/preprocessing_categorical.py +117 -0
  52. supervised/preprocessing/preprocessing_missing.py +97 -0
  53. supervised/preprocessing/preprocessing_utils.py +136 -0
  54. supervised/preprocessing/scale.py +92 -0
  55. supervised/preprocessing/text_transformer.py +73 -0
  56. supervised/tuner/__init__.py +0 -0
  57. supervised/tuner/data_info.py +64 -0
  58. supervised/tuner/hill_climbing.py +80 -0
  59. supervised/tuner/mljar_tuner.py +1501 -0
  60. supervised/tuner/optuna/__init__.py +0 -0
  61. supervised/tuner/optuna/catboost.py +148 -0
  62. supervised/tuner/optuna/extra_trees.py +80 -0
  63. supervised/tuner/optuna/knn.py +64 -0
  64. supervised/tuner/optuna/lightgbm.py +165 -0
  65. supervised/tuner/optuna/nn.py +66 -0
  66. supervised/tuner/optuna/random_forest.py +77 -0
  67. supervised/tuner/optuna/tuner.py +335 -0
  68. supervised/tuner/optuna/xgboost.py +130 -0
  69. supervised/tuner/preprocessing_tuner.py +137 -0
  70. supervised/tuner/random_parameters.py +22 -0
  71. supervised/tuner/time_controller.py +273 -0
  72. supervised/utils/__init__.py +11 -0
  73. supervised/utils/additional_metrics.py +955 -0
  74. supervised/utils/additional_plots.py +228 -0
  75. supervised/utils/automl_plots.py +368 -0
  76. supervised/utils/common.py +39 -0
  77. supervised/utils/config.py +25 -0
  78. supervised/utils/constants.py +4 -0
  79. supervised/utils/data_validation.py +48 -0
  80. supervised/utils/importance.py +93 -0
  81. supervised/utils/jsonencoder.py +33 -0
  82. supervised/utils/leaderboard_plots.py +137 -0
  83. supervised/utils/learning_curves.py +145 -0
  84. supervised/utils/metric.py +450 -0
  85. supervised/utils/report_structured.py +525 -0
  86. supervised/utils/shap.py +373 -0
  87. supervised/utils/subsample.py +17 -0
  88. supervised/utils/utils.py +29 -0
  89. supervised/validation/__init__.py +0 -0
  90. supervised/validation/validation_step.py +37 -0
  91. supervised/validation/validator_base.py +17 -0
  92. supervised/validation/validator_custom.py +112 -0
  93. supervised/validation/validator_kfold.py +153 -0
  94. supervised/validation/validator_split.py +143 -0
@@ -0,0 +1,153 @@
1
+ import gc
2
+ import logging
3
+ import os
4
+ import warnings
5
+
6
+ import numpy as np
7
+
8
+ log = logging.getLogger(__name__)
9
+
10
+ from sklearn.model_selection import KFold, StratifiedKFold
11
+
12
+ from supervised.exceptions import AutoMLException
13
+ from supervised.utils.utils import load_data
14
+ from supervised.validation.validator_base import BaseValidator
15
+
16
+
17
+ class KFoldValidator(BaseValidator):
18
+ def __init__(self, params):
19
+ BaseValidator.__init__(self, params)
20
+
21
+ self.k_folds = self.params.get("k_folds", 5)
22
+ self.shuffle = self.params.get("shuffle", True)
23
+ self.stratify = self.params.get("stratify", False)
24
+ self.random_seed = self.params.get("random_seed", 1906)
25
+ self.repeats = self.params.get("repeats", 1)
26
+
27
+ if not self.shuffle and self.repeats > 1:
28
+ warnings.warn(
29
+ "Disable repeats in validation because shuffle is disabled", UserWarning
30
+ )
31
+ self.repeats = 1
32
+
33
+ self.skf = []
34
+
35
+ for r in range(self.repeats):
36
+ random_seed = self.random_seed + r if self.shuffle else None
37
+ if self.stratify:
38
+ if self.shuffle:
39
+ self.skf += [
40
+ StratifiedKFold(
41
+ n_splits=self.k_folds,
42
+ shuffle=self.shuffle,
43
+ random_state=random_seed,
44
+ )
45
+ ]
46
+ else:
47
+ self.skf += [
48
+ StratifiedKFold(
49
+ n_splits=self.k_folds,
50
+ shuffle=self.shuffle,
51
+ random_state=random_seed,
52
+ )
53
+ ]
54
+ else:
55
+ self.skf += [
56
+ KFold(
57
+ n_splits=self.k_folds,
58
+ shuffle=self.shuffle,
59
+ random_state=random_seed,
60
+ )
61
+ ]
62
+
63
+ self._results_path = self.params.get("results_path")
64
+ self._X_path = self.params.get("X_path")
65
+ self._y_path = self.params.get("y_path")
66
+ self._sample_weight_path = self.params.get("sample_weight_path")
67
+ self._sensitive_features_path = self.params.get("sensitive_features_path")
68
+
69
+ if self._X_path is None or self._y_path is None:
70
+ raise AutoMLException("No data path set in KFoldValidator params")
71
+
72
+ folds_path = os.path.join(self._results_path, "folds")
73
+
74
+ if not os.path.exists(folds_path):
75
+ os.mkdir(folds_path)
76
+ X = load_data(self._X_path)
77
+ y = load_data(self._y_path)
78
+ y = y["target"]
79
+
80
+ if isinstance(y[0], bytes):
81
+ # see https://github.com/scikit-learn/scikit-learn/issues/16980
82
+ y = y.astype(str)
83
+
84
+ for repeat_cnt, skf in enumerate(self.skf):
85
+ for fold_cnt, (train_index, validation_index) in enumerate(
86
+ skf.split(X, y)
87
+ ):
88
+ repeat_str = f"_repeat_{repeat_cnt}" if len(self.skf) > 1 else ""
89
+ train_index_file = os.path.join(
90
+ self._results_path,
91
+ "folds",
92
+ f"fold_{fold_cnt}{repeat_str}_train_indices.npy",
93
+ )
94
+ validation_index_file = os.path.join(
95
+ self._results_path,
96
+ "folds",
97
+ f"fold_{fold_cnt}{repeat_str}_validation_indices.npy",
98
+ )
99
+
100
+ np.save(train_index_file, train_index)
101
+ np.save(validation_index_file, validation_index)
102
+ del X
103
+ del y
104
+ gc.collect()
105
+
106
+ else:
107
+ log.debug("Folds split already done, reuse it")
108
+
109
+ def get_split(self, k, repeat=0):
110
+ repeat_str = f"_repeat_{repeat}" if self.repeats > 1 else ""
111
+
112
+ train_index_file = os.path.join(
113
+ self._results_path, "folds", f"fold_{k}{repeat_str}_train_indices.npy"
114
+ )
115
+ validation_index_file = os.path.join(
116
+ self._results_path, "folds", f"fold_{k}{repeat_str}_validation_indices.npy"
117
+ )
118
+
119
+ train_index = np.load(train_index_file)
120
+ validation_index = np.load(validation_index_file)
121
+
122
+ X = load_data(self._X_path)
123
+ y = load_data(self._y_path)
124
+ y = y["target"]
125
+
126
+ sample_weight = None
127
+ if self._sample_weight_path is not None:
128
+ sample_weight = load_data(self._sample_weight_path)
129
+ sample_weight = sample_weight["sample_weight"]
130
+
131
+ sensitive_features = None
132
+ if self._sensitive_features_path is not None:
133
+ sensitive_features = load_data(self._sensitive_features_path)
134
+
135
+ train_data = {"X": X.loc[train_index], "y": y.loc[train_index]}
136
+ validation_data = {"X": X.loc[validation_index], "y": y.loc[validation_index]}
137
+ if sample_weight is not None:
138
+ train_data["sample_weight"] = sample_weight.loc[train_index]
139
+ validation_data["sample_weight"] = sample_weight.loc[validation_index]
140
+
141
+ if sensitive_features is not None:
142
+ train_data["sensitive_features"] = sensitive_features.loc[train_index]
143
+ validation_data["sensitive_features"] = sensitive_features.loc[
144
+ validation_index
145
+ ]
146
+
147
+ return (train_data, validation_data)
148
+
149
+ def get_n_splits(self):
150
+ return self.k_folds
151
+
152
+ def get_repeats(self):
153
+ return self.repeats
@@ -0,0 +1,143 @@
1
+ import logging
2
+ import os
3
+ import warnings
4
+
5
+ import numpy as np
6
+
7
+ log = logging.getLogger(__name__)
8
+
9
+ from sklearn.model_selection import train_test_split
10
+
11
+ from supervised.exceptions import AutoMLException
12
+ from supervised.utils.utils import load_data
13
+ from supervised.validation.validator_base import BaseValidator
14
+
15
+
16
+ class SplitValidator(BaseValidator):
17
+ def __init__(self, params):
18
+ BaseValidator.__init__(self, params)
19
+
20
+ self.train_ratio = self.params.get("train_ratio", 0.8)
21
+ self.shuffle = self.params.get("shuffle", True)
22
+ self.stratify = self.params.get("stratify", False)
23
+ self.random_seed = self.params.get("random_seed", 1234)
24
+ self.repeats = self.params.get("repeats", 1)
25
+
26
+ if not self.shuffle and self.repeats > 1:
27
+ warnings.warn(
28
+ "Disable repeats in validation because shuffle is disabled", UserWarning
29
+ )
30
+ self.repeats = 1
31
+
32
+ self._results_path = self.params.get("results_path")
33
+ self._X_path = self.params.get("X_path")
34
+ self._y_path = self.params.get("y_path")
35
+ self._sample_weight_path = self.params.get("sample_weight_path")
36
+ self._sensitive_features_path = self.params.get("sensitive_features_path")
37
+
38
+ if self._X_path is None or self._y_path is None:
39
+ raise AutoMLException("No data path set in SplitValidator params")
40
+
41
+ def get_split(self, k=0, repeat=0):
42
+ X = load_data(self._X_path)
43
+ y = load_data(self._y_path)
44
+ y = y["target"]
45
+
46
+ sample_weight = None
47
+ if self._sample_weight_path is not None:
48
+ sample_weight = load_data(self._sample_weight_path)
49
+ sample_weight = sample_weight["sample_weight"]
50
+
51
+ sensitive_features = None
52
+ if self._sensitive_features_path is not None:
53
+ sensitive_features = load_data(self._sensitive_features_path)
54
+
55
+ stratify = None
56
+ if self.stratify:
57
+ stratify = y
58
+ if self.shuffle == False:
59
+ stratify = None
60
+
61
+ input_data = [X, y]
62
+ if sample_weight is not None:
63
+ input_data += [sample_weight]
64
+ if sensitive_features is not None:
65
+ input_data += [sensitive_features]
66
+
67
+ output_data = train_test_split(
68
+ *input_data,
69
+ train_size=self.train_ratio,
70
+ test_size=1.0 - self.train_ratio,
71
+ shuffle=self.shuffle,
72
+ stratify=stratify,
73
+ random_state=self.random_seed + repeat,
74
+ )
75
+
76
+ X_train = output_data[0]
77
+ X_validation = output_data[1]
78
+ y_train = output_data[2]
79
+ y_validation = output_data[3]
80
+ if sample_weight is not None:
81
+ sample_weight_train = output_data[4]
82
+ sample_weight_validation = output_data[5]
83
+ if sensitive_features is not None:
84
+ sensitive_features_train = output_data[6]
85
+ sensitive_features_validation = output_data[7]
86
+ else:
87
+ if sensitive_features is not None:
88
+ sensitive_features_train = output_data[4]
89
+ sensitive_features_validation = output_data[5]
90
+
91
+ train_data = {"X": X_train, "y": y_train}
92
+ validation_data = {"X": X_validation, "y": y_validation}
93
+ if sample_weight is not None:
94
+ train_data["sample_weight"] = sample_weight_train
95
+ validation_data["sample_weight"] = sample_weight_validation
96
+ if sensitive_features is not None:
97
+ train_data["sensitive_features"] = sensitive_features_train
98
+ validation_data["sensitive_features"] = sensitive_features_validation
99
+
100
+ repeat_str = f"repeat_{repeat}_" if self.repeats > 1 else ""
101
+
102
+ train_data_file = os.path.join(
103
+ self._results_path, f"split_{repeat_str}train_indices.npy"
104
+ )
105
+ validation_data_file = os.path.join(
106
+ self._results_path, f"split_{repeat_str}validation_indices.npy"
107
+ )
108
+
109
+ np.save(train_data_file, X_train.index)
110
+ np.save(validation_data_file, X_validation.index)
111
+
112
+ return train_data, validation_data
113
+
114
+ def get_n_splits(self):
115
+ return 1
116
+
117
+ def get_repeats(self):
118
+ return self.repeats
119
+
120
+
121
+ """
122
+ import numpy as np
123
+ import pandas as pd
124
+
125
+ from sklearn.utils.fixes import bincount
126
+ from sklearn.model_selection import train_test_split
127
+
128
+ import logging
129
+ logger = logging.getLogger('mljar')
130
+
131
+
132
+ def validation_split(train, validation_train_split, stratify, shuffle, random_seed):
133
+
134
+ if shuffle:
135
+ else:
136
+ if stratify is None:
137
+ train, validation = data_split(validation_train_split, train)
138
+ else:
139
+ train, validation = data_split_stratified(validation_train_split, train, stratify)
140
+ return train, validation
141
+
142
+
143
+ """