InsurAutoML 0.2.9__tar.gz → 0.3.0__tar.gz
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.
- insurautoml-0.3.0/InsurAutoML/VERSION +1 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/_optional.py +32 -2
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/balancing/base.py +2 -3
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/balancing/mixed_sampling.py +26 -21
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/balancing/over_sampling.py +33 -30
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/balancing/under_sampling.py +89 -66
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/base.py +2 -4
- insurautoml-0.3.0/InsurAutoML/dashboard/__init__.py +71 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/__main__.py +121 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/callback.py +154 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/events.py +533 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/layout.py +408 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/server.py +337 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/space.py +358 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/static/app.js +872 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/static/index.html +196 -0
- insurautoml-0.3.0/InsurAutoML/dashboard/static/style.css +482 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/datasets/__init__.py +1 -1
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/datasets/image.py +3 -2
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/datasets/tabular.py +39 -39
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/datasets/text.py +3 -2
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/encoding/encoding.py +34 -15
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/ext/suppsplit/suppsplit/__init__.py +0 -1
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/ext/suppsplit/suppsplit/split.py +58 -12
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/ext/suppsplit/suppsplit_cpp/bindings.cpp +10 -4
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/ext/suppsplit/suppsplit_cpp/sp.cpp +15 -7
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/feature_selection/__init__.py +9 -9
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/feature_selection/embed.py +22 -13
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/feature_selection/filter.py +110 -45
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/feature_selection/hybrid.py +59 -47
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/feature_selection/sklearn.py +113 -116
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/feature_selection/wrapper.py +48 -31
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/DL.py +4 -4
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/ML.py +56 -20
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/__init__.py +0 -1
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/base.py +731 -493
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/ensemble.py +72 -37
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/informed/ML.py +40 -13
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/informed/base.py +76 -392
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/informed/fixed.py +123 -28
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/informed/informed_ensemble.py +38 -16
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/informed/utils.py +29 -5
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/utils.py +18 -5
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/imputation/__init__.py +1 -1
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/imputation/base.py +28 -35
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/imputation/clustering.py +100 -86
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/imputation/multiple.py +30 -19
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/imputation/nn.py +51 -46
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/FNN.py +45 -31
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/RNN.py +58 -31
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/__init__.py +1 -1
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/base.py +5 -8
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/clustering.py +0 -1
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/lightgbm.py +21 -14
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/sklearn_classifiers.py +171 -145
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/sklearn_regressors.py +173 -111
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/xgboost.py +14 -8
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/prep/pipeline.py +40 -16
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/prep/utils.py +10 -10
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/scaling/base.py +8 -4
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/scaling/scaling.py +96 -78
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/__init__.py +0 -1
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/base.py +15 -5
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/data.py +66 -41
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/eda.py +11 -5
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/file.py +3 -2
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/metadata.py +27 -14
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/optimize.py +195 -49
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/stats.py +118 -59
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/utils/tensor.py +7 -8
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML.egg-info/PKG-INFO +16 -129
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML.egg-info/SOURCES.txt +11 -41
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML.egg-info/requires.txt +4 -1
- insurautoml-0.3.0/InsurAutoML.egg-info/top_level.txt +1 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/PKG-INFO +16 -129
- insurautoml-0.3.0/README.md +171 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/pyproject.toml +56 -2
- {insurautoml-0.2.9 → insurautoml-0.3.0}/setup.py +15 -6
- insurautoml-0.2.9/InsurAutoML/VERSION +0 -1
- insurautoml-0.2.9/InsurAutoML.egg-info/top_level.txt +0 -5
- insurautoml-0.2.9/README.md +0 -291
- insurautoml-0.2.9/tests/test_balancing/test_balancing.py +0 -72
- insurautoml-0.2.9/tests/test_datasets/test_datasets_hf.py +0 -164
- insurautoml-0.2.9/tests/test_datasets/test_datasets_tabular.py +0 -343
- insurautoml-0.2.9/tests/test_encoder/test_encoder.py +0 -95
- insurautoml-0.2.9/tests/test_feature_selection/test_feature_selection.py +0 -261
- insurautoml-0.2.9/tests/test_feature_selection/test_feature_selection_more.py +0 -736
- insurautoml-0.2.9/tests/test_feature_selection/test_feature_selection_wrapper.py +0 -226
- insurautoml-0.2.9/tests/test_hpo/test_hpo.py +0 -434
- insurautoml-0.2.9/tests/test_hpo/test_hpo_2.py +0 -435
- insurautoml-0.2.9/tests/test_hpo/test_hpo_3.py +0 -531
- insurautoml-0.2.9/tests/test_hpo/test_hpo_ensemble.py +0 -212
- insurautoml-0.2.9/tests/test_hpo/test_hpo_ensemble_voting.py +0 -237
- insurautoml-0.2.9/tests/test_hpo/test_hpo_spaces.py +0 -147
- insurautoml-0.2.9/tests/test_hyperparameters/test_hyperparameters.py +0 -109
- insurautoml-0.2.9/tests/test_imputer/test_imputer.py +0 -245
- insurautoml-0.2.9/tests/test_imputer/test_imputer_clustering.py +0 -381
- insurautoml-0.2.9/tests/test_imputer/test_imputer_more.py +0 -424
- insurautoml-0.2.9/tests/test_imputer/test_imputer_nn.py +0 -257
- insurautoml-0.2.9/tests/test_informed/conftest.py +0 -110
- insurautoml-0.2.9/tests/test_informed/test_informed_automl.py +0 -172
- insurautoml-0.2.9/tests/test_informed/test_informed_ensemble.py +0 -185
- insurautoml-0.2.9/tests/test_informed/test_informed_objective.py +0 -217
- insurautoml-0.2.9/tests/test_informed/test_informed_pipeline.py +0 -138
- insurautoml-0.2.9/tests/test_informed/test_informed_prep.py +0 -175
- insurautoml-0.2.9/tests/test_model/test_model.py +0 -208
- insurautoml-0.2.9/tests/test_model/test_model_2.py +0 -350
- insurautoml-0.2.9/tests/test_model/test_model_nn.py +0 -343
- insurautoml-0.2.9/tests/test_scaling/test_scaling.py +0 -148
- insurautoml-0.2.9/tests/test_utils/test_metadata.py +0 -73
- insurautoml-0.2.9/tests/test_utils/test_optional.py +0 -151
- insurautoml-0.2.9/tests/test_utils/test_utils.py +0 -126
- insurautoml-0.2.9/tests/test_utils/test_utils_base.py +0 -223
- insurautoml-0.2.9/tests/test_utils/test_utils_data.py +0 -218
- insurautoml-0.2.9/tests/test_utils/test_utils_data_extra.py +0 -398
- insurautoml-0.2.9/tests/test_utils/test_utils_metadata_extra.py +0 -290
- insurautoml-0.2.9/tests/test_utils/test_utils_optimize.py +0 -222
- insurautoml-0.2.9/tests/test_utils/test_utils_optimize_more.py +0 -272
- insurautoml-0.2.9/tests/test_utils/test_utils_stat.py +0 -189
- insurautoml-0.2.9/tests/test_utils/test_utils_stats_extra.py +0 -288
- insurautoml-0.2.9/tests/test_utils/test_utils_tensor.py +0 -143
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/balancing/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/constant.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/encoding/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/encoding/base.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/ext/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/ext/suppsplit/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/ext/suppsplit/suppsplit/utils.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/ext/suppsplit/suppsplit_cpp/sPlit.cpp +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/feature_selection/base.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hpo/informed/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/ray/balancing_hyperparameter.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/ray/classifier_hyperparameter.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/ray/encoder_hyperparameter.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/ray/feature_selection_hyperparameter.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/ray/imputer_hyperparameter.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/ray/prep_hyperparameter.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/ray/regressor_hyperparameter.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/hyperparameters/ray/scaling_hyperparameter.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/ML.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/catboost.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/model/gam.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/prep/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/prep/prep.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/scaling/__init__.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML/version.py +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/InsurAutoML.egg-info/dependency_links.txt +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/LICENSE +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/setup.cfg +0 -0
- {insurautoml-0.2.9 → insurautoml-0.3.0}/tests/test_base.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.3.0
|
|
@@ -1,11 +1,41 @@
|
|
|
1
1
|
"""
|
|
2
|
-
File: _optional.py
|
|
2
|
+
File Name: _optional.py
|
|
3
3
|
Author: Panyi Dong
|
|
4
4
|
GitHub: https://github.com/PanyiDong/
|
|
5
|
-
|
|
5
|
+
Actuarial and Risk Management Sciences, University of Illinois at Urbana-Champaign (UIUC)
|
|
6
6
|
|
|
7
7
|
Project: InsurAutoML
|
|
8
|
+
Latest Version: 0.2.9
|
|
8
9
|
Relative Path: /InsurAutoML/_optional.py
|
|
10
|
+
File Created: Monday, 16th November 2026 7:12:56 pm
|
|
11
|
+
Author: Panyi Dong (panyid2@illinois.edu)
|
|
12
|
+
|
|
13
|
+
-----
|
|
14
|
+
Last Modified: Monday, 16th November 2026 11:40:01 pm
|
|
15
|
+
Modified By: Panyi Dong (panyid2@illinois.edu)
|
|
16
|
+
|
|
17
|
+
-----
|
|
18
|
+
MIT License
|
|
19
|
+
|
|
20
|
+
Copyright (c) 2026, Panyi Dong
|
|
21
|
+
|
|
22
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
23
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
24
|
+
in the Software without restriction, including without limitation the rights
|
|
25
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
26
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
27
|
+
furnished to do so, subject to the following conditions:
|
|
28
|
+
|
|
29
|
+
The above copyright notice and this permission notice shall be included in all
|
|
30
|
+
copies or substantial portions of the Software.
|
|
31
|
+
|
|
32
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
33
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
34
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
35
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
36
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
37
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
38
|
+
SOFTWARE.
|
|
9
39
|
|
|
10
40
|
Availability of the optional "informed" installation.
|
|
11
41
|
|
|
@@ -38,13 +38,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
38
38
|
SOFTWARE.
|
|
39
39
|
"""
|
|
40
40
|
|
|
41
|
-
from typing import Union, Tuple
|
|
41
|
+
from typing import Optional, Union, Tuple
|
|
42
42
|
import pandas as pd
|
|
43
43
|
from ..constant import MAX_ITER
|
|
44
44
|
|
|
45
45
|
|
|
46
46
|
class BaseBalancing:
|
|
47
|
-
|
|
48
47
|
"""
|
|
49
48
|
Base class for balancing methods.
|
|
50
49
|
"""
|
|
@@ -56,6 +55,6 @@ class BaseBalancing:
|
|
|
56
55
|
return MAX_ITER
|
|
57
56
|
|
|
58
57
|
def fit_transform(
|
|
59
|
-
self, X: pd.DataFrame, y: pd.DataFrame = None
|
|
58
|
+
self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None
|
|
60
59
|
) -> Union[pd.DataFrame, Tuple[pd.DataFrame, pd.DataFrame]]:
|
|
61
60
|
raise NotImplementedError
|
|
@@ -40,9 +40,10 @@ SOFTWARE.
|
|
|
40
40
|
|
|
41
41
|
from __future__ import annotations
|
|
42
42
|
|
|
43
|
-
from typing import Union, Tuple
|
|
43
|
+
from typing import Any, List, Optional, Union, Tuple, cast
|
|
44
44
|
import numpy as np
|
|
45
45
|
import pandas as pd
|
|
46
|
+
from ..utils.base import copy_frame
|
|
46
47
|
import warnings
|
|
47
48
|
|
|
48
49
|
from ..constant import MAX_ITER
|
|
@@ -61,7 +62,6 @@ balancing machine learning training data. ACM SIGKDD explorations newsletter, 6(
|
|
|
61
62
|
|
|
62
63
|
|
|
63
64
|
class Smote_TomekLink(Smote, TomekLink):
|
|
64
|
-
|
|
65
65
|
"""
|
|
66
66
|
Run Smote then run Tomek Link to balance dataset
|
|
67
67
|
"""
|
|
@@ -71,10 +71,10 @@ class Smote_TomekLink(Smote, TomekLink):
|
|
|
71
71
|
imbalance_threshold: float = 0.9,
|
|
72
72
|
norm: str = "l2",
|
|
73
73
|
all: bool = True,
|
|
74
|
-
max_iter: int = None,
|
|
74
|
+
max_iter: Optional[int] = None,
|
|
75
75
|
k: int = 5,
|
|
76
76
|
generation: str = "mean",
|
|
77
|
-
seed: int = None,
|
|
77
|
+
seed: Optional[int] = None,
|
|
78
78
|
) -> None:
|
|
79
79
|
self.imbalance_threshold = imbalance_threshold
|
|
80
80
|
self.norm = norm
|
|
@@ -87,25 +87,26 @@ class Smote_TomekLink(Smote, TomekLink):
|
|
|
87
87
|
self._fitted = False # whether the model has been fitted
|
|
88
88
|
|
|
89
89
|
def fit_transform(
|
|
90
|
-
self, X: pd.DataFrame, y: pd.DataFrame = None
|
|
90
|
+
self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None
|
|
91
91
|
) -> Union[pd.DataFrame, Tuple[pd.DataFrame, pd.DataFrame]]:
|
|
92
92
|
try: # if missing y, will be None value; or will be dataframe, use df.empty for judge
|
|
93
|
-
_empty = y.empty
|
|
93
|
+
_empty = cast(pd.DataFrame, y).empty
|
|
94
94
|
except AttributeError:
|
|
95
95
|
_empty = y is None
|
|
96
96
|
|
|
97
97
|
if (
|
|
98
98
|
not _empty
|
|
99
99
|
): # if no y input, only convert X; or, combine X and y to consider balancing
|
|
100
|
+
assert y is not None # a None y has no .empty, so _empty is True
|
|
100
101
|
features = list(X.columns)
|
|
101
|
-
response = list(y.columns)
|
|
102
|
+
response: Optional[List[str]] = list(y.columns)
|
|
102
103
|
data = pd.concat([X, y], axis=1)
|
|
103
104
|
else:
|
|
104
105
|
features = list(X.columns)
|
|
105
106
|
response = None
|
|
106
107
|
data = X
|
|
107
108
|
|
|
108
|
-
_data =
|
|
109
|
+
_data = cast(pd.DataFrame, copy_frame(data, deep=True))
|
|
109
110
|
if not is_imbalance(_data, self.imbalance_threshold):
|
|
110
111
|
warnings.warn("The dataset is balanced, no change.")
|
|
111
112
|
else:
|
|
@@ -120,7 +121,7 @@ class Smote_TomekLink(Smote, TomekLink):
|
|
|
120
121
|
k=self.k,
|
|
121
122
|
generation=self.generation,
|
|
122
123
|
)
|
|
123
|
-
_data = super().fit_transform(_data)
|
|
124
|
+
_data = cast(pd.DataFrame, super().fit_transform(_data))
|
|
124
125
|
|
|
125
126
|
super(Smote, self).__init__(
|
|
126
127
|
imbalance_threshold=_threshold,
|
|
@@ -129,19 +130,20 @@ class Smote_TomekLink(Smote, TomekLink):
|
|
|
129
130
|
max_iter=self.max_iter,
|
|
130
131
|
seed=self.seed,
|
|
131
132
|
)
|
|
132
|
-
_data = super(Smote, self).fit_transform(_data)
|
|
133
|
+
_data = cast(pd.DataFrame, super(Smote, self).fit_transform(_data))
|
|
133
134
|
self.imbalance_threshold = _threshold
|
|
134
135
|
|
|
135
136
|
self._fitted = True
|
|
136
137
|
|
|
137
138
|
if not _empty:
|
|
138
|
-
return _data[features],
|
|
139
|
+
return cast(pd.DataFrame, _data[features]), cast(
|
|
140
|
+
pd.DataFrame, _data[cast(List[str], response)]
|
|
141
|
+
)
|
|
139
142
|
else:
|
|
140
143
|
return _data
|
|
141
144
|
|
|
142
145
|
|
|
143
146
|
class Smote_ENN(Smote, EditedNearestNeighbor):
|
|
144
|
-
|
|
145
147
|
"""
|
|
146
148
|
Run Smote then run ENN to balance dataset
|
|
147
149
|
"""
|
|
@@ -151,8 +153,8 @@ class Smote_ENN(Smote, EditedNearestNeighbor):
|
|
|
151
153
|
imbalance_threshold: float = 0.9,
|
|
152
154
|
norm: str = "l2",
|
|
153
155
|
all: bool = True,
|
|
154
|
-
max_iter: int = None,
|
|
155
|
-
seed: int = None,
|
|
156
|
+
max_iter: Optional[int] = None,
|
|
157
|
+
seed: Optional[int] = None,
|
|
156
158
|
k: int = 5,
|
|
157
159
|
generation: str = "mean",
|
|
158
160
|
) -> None:
|
|
@@ -167,25 +169,26 @@ class Smote_ENN(Smote, EditedNearestNeighbor):
|
|
|
167
169
|
self._fitted = False # whether the model has been fitted
|
|
168
170
|
|
|
169
171
|
def fit_transform(
|
|
170
|
-
self, X: pd.DataFrame, y: pd.DataFrame = None
|
|
172
|
+
self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None
|
|
171
173
|
) -> Union[pd.DataFrame, Tuple[pd.DataFrame, pd.DataFrame]]:
|
|
172
174
|
try: # if missing y, will be None value; or will be dataframe, use df.empty for judge
|
|
173
|
-
_empty = y.empty
|
|
175
|
+
_empty = cast(pd.DataFrame, y).empty
|
|
174
176
|
except AttributeError:
|
|
175
177
|
_empty = y is None
|
|
176
178
|
|
|
177
179
|
if (
|
|
178
180
|
not _empty
|
|
179
181
|
): # if no y input, only convert X; or, combine X and y to consider balancing
|
|
182
|
+
assert y is not None # a None y has no .empty, so _empty is True
|
|
180
183
|
features = list(X.columns)
|
|
181
|
-
response = list(y.columns)
|
|
184
|
+
response: Optional[List[str]] = list(y.columns)
|
|
182
185
|
data = pd.concat([X, y], axis=1)
|
|
183
186
|
else:
|
|
184
187
|
features = list(X.columns)
|
|
185
188
|
response = None
|
|
186
189
|
data = X
|
|
187
190
|
|
|
188
|
-
_data =
|
|
191
|
+
_data = cast(pd.DataFrame, copy_frame(data, deep=True))
|
|
189
192
|
|
|
190
193
|
if not is_imbalance(_data, self.imbalance_threshold):
|
|
191
194
|
warnings.warn("The dataset is balanced, no change.")
|
|
@@ -201,7 +204,7 @@ class Smote_ENN(Smote, EditedNearestNeighbor):
|
|
|
201
204
|
k=self.k,
|
|
202
205
|
generation=self.generation,
|
|
203
206
|
)
|
|
204
|
-
_data = super().fit_transform(_data)
|
|
207
|
+
_data = cast(pd.DataFrame, super().fit_transform(_data))
|
|
205
208
|
|
|
206
209
|
super(Smote, self).__init__(
|
|
207
210
|
imbalance_threshold=_threshold,
|
|
@@ -210,12 +213,14 @@ class Smote_ENN(Smote, EditedNearestNeighbor):
|
|
|
210
213
|
seed=self.seed,
|
|
211
214
|
k=self.k,
|
|
212
215
|
)
|
|
213
|
-
_data = super(Smote, self).fit_transform(_data)
|
|
216
|
+
_data = cast(pd.DataFrame, super(Smote, self).fit_transform(_data))
|
|
214
217
|
self.imbalance_threshold = _threshold
|
|
215
218
|
|
|
216
219
|
self._fitted = True
|
|
217
220
|
|
|
218
221
|
if not _empty:
|
|
219
|
-
return _data[features],
|
|
222
|
+
return cast(pd.DataFrame, _data[features]), cast(
|
|
223
|
+
pd.DataFrame, _data[cast(List[str], response)]
|
|
224
|
+
)
|
|
220
225
|
else:
|
|
221
226
|
return _data
|
|
@@ -40,9 +40,10 @@ SOFTWARE.
|
|
|
40
40
|
|
|
41
41
|
from __future__ import annotations
|
|
42
42
|
|
|
43
|
-
from typing import Union, Tuple
|
|
43
|
+
from typing import Any, List, Optional, Union, Tuple, cast
|
|
44
44
|
import numpy as np
|
|
45
45
|
import pandas as pd
|
|
46
|
+
from ..utils.base import copy_frame
|
|
46
47
|
import warnings
|
|
47
48
|
import sklearn
|
|
48
49
|
import sklearn.utils
|
|
@@ -62,7 +63,6 @@ balancing machine learning training data. ACM SIGKDD explorations newsletter, 6(
|
|
|
62
63
|
|
|
63
64
|
|
|
64
65
|
class SimpleRandomOverSampling(BaseBalancing):
|
|
65
|
-
|
|
66
66
|
"""
|
|
67
67
|
Simple Random Over-Sampling
|
|
68
68
|
Randomly draw samples from minority class and replicate the sample
|
|
@@ -83,8 +83,8 @@ class SimpleRandomOverSampling(BaseBalancing):
|
|
|
83
83
|
self,
|
|
84
84
|
imbalance_threshold: float = 0.9,
|
|
85
85
|
all: bool = True,
|
|
86
|
-
max_iter: int = None,
|
|
87
|
-
seed: int = None,
|
|
86
|
+
max_iter: Optional[int] = None,
|
|
87
|
+
seed: Optional[int] = None,
|
|
88
88
|
) -> None:
|
|
89
89
|
self.imbalance_threshold = imbalance_threshold
|
|
90
90
|
self.all = all
|
|
@@ -95,25 +95,26 @@ class SimpleRandomOverSampling(BaseBalancing):
|
|
|
95
95
|
self._fitted = False # whether the model has been fitted
|
|
96
96
|
|
|
97
97
|
def fit_transform(
|
|
98
|
-
self, X: pd.DataFrame, y: pd.DataFrame = None
|
|
98
|
+
self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None
|
|
99
99
|
) -> Union[pd.DataFrame, Tuple[pd.DataFrame, pd.DataFrame]]:
|
|
100
100
|
try: # if missing y, will be None value; or will be dataframe, use df.empty for judge
|
|
101
|
-
_empty = y.empty
|
|
101
|
+
_empty = cast(pd.DataFrame, y).empty
|
|
102
102
|
except AttributeError:
|
|
103
103
|
_empty = y is None
|
|
104
104
|
|
|
105
105
|
if (
|
|
106
106
|
not _empty
|
|
107
107
|
): # if no y input, only convert X; or, combine X and y to consider balancing
|
|
108
|
+
assert y is not None # a None y has no .empty, so _empty is True
|
|
108
109
|
features = list(X.columns)
|
|
109
|
-
response = list(y.columns)
|
|
110
|
+
response: Optional[List[str]] = list(y.columns)
|
|
110
111
|
data = pd.concat([X, y], axis=1)
|
|
111
112
|
else:
|
|
112
113
|
features = list(X.columns)
|
|
113
114
|
response = None
|
|
114
115
|
data = X
|
|
115
116
|
|
|
116
|
-
_data =
|
|
117
|
+
_data = cast(pd.DataFrame, copy_frame(data, deep=True))
|
|
117
118
|
if not is_imbalance(_data, self.imbalance_threshold):
|
|
118
119
|
warnings.warn("The dataset is balanced, no change.")
|
|
119
120
|
else:
|
|
@@ -126,18 +127,20 @@ class SimpleRandomOverSampling(BaseBalancing):
|
|
|
126
127
|
self._fitted = True
|
|
127
128
|
|
|
128
129
|
if not _empty: # return balanced X and y if y is also inputted
|
|
129
|
-
return _data[features],
|
|
130
|
+
return cast(pd.DataFrame, _data[features]), cast(
|
|
131
|
+
pd.DataFrame, _data[cast(List[str], response)]
|
|
132
|
+
)
|
|
130
133
|
else:
|
|
131
134
|
return _data
|
|
132
135
|
|
|
133
136
|
def _fit_transform(
|
|
134
|
-
self, X:
|
|
137
|
+
self, X: pd.DataFrame
|
|
135
138
|
) -> (
|
|
136
139
|
pd.DataFrame
|
|
137
140
|
): # using random over-sampling to balance the first imbalanced feature
|
|
138
141
|
n, features = len(X), list(X.columns)
|
|
139
|
-
_imbalanced_feature, _majority =
|
|
140
|
-
X, self.imbalance_threshold, value=True
|
|
142
|
+
_imbalanced_feature, _majority = cast(
|
|
143
|
+
Tuple[str, Any], is_imbalance(X, self.imbalance_threshold, value=True)
|
|
141
144
|
)
|
|
142
145
|
_minority_class = X.loc[X[_imbalanced_feature] != _majority]
|
|
143
146
|
|
|
@@ -156,13 +159,14 @@ class SimpleRandomOverSampling(BaseBalancing):
|
|
|
156
159
|
]
|
|
157
160
|
)
|
|
158
161
|
# shuffle the data
|
|
159
|
-
X =
|
|
162
|
+
X = cast(
|
|
163
|
+
pd.DataFrame, sklearn.utils.shuffle(X.reset_index(drop=True))
|
|
164
|
+
).reset_index(drop=True)
|
|
160
165
|
|
|
161
166
|
return X
|
|
162
167
|
|
|
163
168
|
|
|
164
169
|
class Smote(BaseBalancing):
|
|
165
|
-
|
|
166
170
|
"""
|
|
167
171
|
Synthetic Minority Over-sampling Technique (Smote)
|
|
168
172
|
use over-sampling to generate minority class points using nearest neighbors
|
|
@@ -193,8 +197,8 @@ class Smote(BaseBalancing):
|
|
|
193
197
|
imbalance_threshold: float = 0.9,
|
|
194
198
|
norm: str = "l2",
|
|
195
199
|
all: bool = True,
|
|
196
|
-
max_iter: int = None,
|
|
197
|
-
seed: int = None,
|
|
200
|
+
max_iter: Optional[int] = None,
|
|
201
|
+
seed: Optional[int] = None,
|
|
198
202
|
k: int = 5,
|
|
199
203
|
generation: str = "mean",
|
|
200
204
|
) -> None:
|
|
@@ -210,25 +214,26 @@ class Smote(BaseBalancing):
|
|
|
210
214
|
self._fitted = False # whether the model has been fitted
|
|
211
215
|
|
|
212
216
|
def fit_transform(
|
|
213
|
-
self, X: pd.DataFrame, y: pd.DataFrame = None
|
|
217
|
+
self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None
|
|
214
218
|
) -> Union[pd.DataFrame, Tuple[pd.DataFrame, pd.DataFrame]]:
|
|
215
219
|
try: # if missing y, will be None value; or will be dataframe, use df.empty for judge
|
|
216
|
-
_empty = y.empty
|
|
220
|
+
_empty = cast(pd.DataFrame, y).empty
|
|
217
221
|
except AttributeError:
|
|
218
222
|
_empty = y is None
|
|
219
223
|
|
|
220
224
|
if (
|
|
221
225
|
not _empty
|
|
222
226
|
): # if no y input, only convert X; or, combine X and y to consider balancing
|
|
227
|
+
assert y is not None # a None y has no .empty, so _empty is True
|
|
223
228
|
features = list(X.columns)
|
|
224
|
-
response = list(y.columns)
|
|
229
|
+
response: Optional[List[str]] = list(y.columns)
|
|
225
230
|
data = pd.concat([X, y], axis=1)
|
|
226
231
|
else:
|
|
227
232
|
features = list(X.columns)
|
|
228
233
|
response = None
|
|
229
234
|
data = X
|
|
230
235
|
|
|
231
|
-
_data =
|
|
236
|
+
_data = cast(pd.DataFrame, copy_frame(data, deep=True))
|
|
232
237
|
if not is_imbalance(_data, self.imbalance_threshold):
|
|
233
238
|
warnings.warn("The dataset is balanced, no change.")
|
|
234
239
|
else:
|
|
@@ -252,14 +257,16 @@ class Smote(BaseBalancing):
|
|
|
252
257
|
self._fitted = True
|
|
253
258
|
|
|
254
259
|
if not _empty:
|
|
255
|
-
return _data[features],
|
|
260
|
+
return cast(pd.DataFrame, _data[features]), cast(
|
|
261
|
+
pd.DataFrame, _data[cast(List[str], response)]
|
|
262
|
+
)
|
|
256
263
|
else:
|
|
257
264
|
return _data
|
|
258
265
|
|
|
259
266
|
def _fit_transform(self, X: pd.DataFrame) -> pd.DataFrame:
|
|
260
267
|
n, features = len(X), list(X.columns)
|
|
261
|
-
_imbalanced_feature, _majority =
|
|
262
|
-
X, self.imbalance_threshold, value=True
|
|
268
|
+
_imbalanced_feature, _majority = cast(
|
|
269
|
+
Tuple[str, Any], is_imbalance(X, self.imbalance_threshold, value=True)
|
|
263
270
|
)
|
|
264
271
|
_minority_class = X.loc[X[_imbalanced_feature] != _majority]
|
|
265
272
|
|
|
@@ -282,14 +289,10 @@ class Smote(BaseBalancing):
|
|
|
282
289
|
].mean(axis=0)
|
|
283
290
|
elif self.generation == "random":
|
|
284
291
|
X.loc[len(X), :] = X.loc[_sample.index, :] + np.random.rand() * (
|
|
285
|
-
X.loc[X.index[_link], :] - X.
|
|
292
|
+
X.loc[X.index[_link], :] - X.loc[_sample.index, :]
|
|
286
293
|
)
|
|
287
294
|
else:
|
|
288
|
-
raise ValueError(
|
|
289
|
-
|
|
290
|
-
["mean", "random"], get {}'.format(
|
|
291
|
-
self.generation
|
|
292
|
-
)
|
|
293
|
-
)
|
|
295
|
+
raise ValueError('Not recognizing generation method! Should be in \
|
|
296
|
+
["mean", "random"], get {}'.format(self.generation))
|
|
294
297
|
|
|
295
298
|
return X
|