evidenceml 0.32.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.
- evidenceml/__init__.py +1 -0
- evidenceml/base/__init__.py +23 -0
- evidenceml/base/_beartype.py +3 -0
- evidenceml/base/classes/__init__.py +5 -0
- evidenceml/base/classes/_missing_method_error.py +4 -0
- evidenceml/base/classes/_model_not_callable_error.py +4 -0
- evidenceml/base/classes/_not_fitted_error.py +31 -0
- evidenceml/base/functions/__init__.py +13 -0
- evidenceml/base/functions/_fast_correlation.py +42 -0
- evidenceml/base/functions/_fill_na.py +65 -0
- evidenceml/base/functions/_has_parameters.py +35 -0
- evidenceml/base/functions/_merge_dicts.py +29 -0
- evidenceml/base/functions/_read_pickle.py +33 -0
- evidenceml/encoding_and_transforms/__init__.py +16 -0
- evidenceml/encoding_and_transforms/encodings/__init__.py +5 -0
- evidenceml/encoding_and_transforms/encodings/_reduce_categories.py +100 -0
- evidenceml/encoding_and_transforms/transformers/__init__.py +17 -0
- evidenceml/encoding_and_transforms/transformers/_woe_base.py +246 -0
- evidenceml/encoding_and_transforms/transformers/_woe_boolean.py +294 -0
- evidenceml/encoding_and_transforms/transformers/_woe_categorical.py +172 -0
- evidenceml/encoding_and_transforms/transformers/_woe_column_transformer.py +57 -0
- evidenceml/encoding_and_transforms/transformers/_woe_numerical.py +350 -0
- evidenceml/feature_selection/__init__.py +83 -0
- evidenceml/feature_selection/backward_selection/__init__.py +5 -0
- evidenceml/feature_selection/backward_selection/_backward_selection.py +175 -0
- evidenceml/feature_selection/descriptive_statistics/__init__.py +52 -0
- evidenceml/feature_selection/descriptive_statistics/_bootstrap_apply.py +105 -0
- evidenceml/feature_selection/descriptive_statistics/_descriptive_statistics_breakdown_gini.py +148 -0
- evidenceml/feature_selection/descriptive_statistics/_descriptive_statistics_correlation.py +207 -0
- evidenceml/feature_selection/descriptive_statistics/_descriptive_statistics_gini.py +266 -0
- evidenceml/feature_selection/descriptive_statistics/_descriptive_statistics_gini_temporal.py +172 -0
- evidenceml/feature_selection/descriptive_statistics/_descriptive_statistics_hhi.py +203 -0
- evidenceml/feature_selection/descriptive_statistics/_descriptive_statistics_psi.py +241 -0
- evidenceml/feature_selection/descriptive_statistics/_descriptive_statistics_single_factor_analyse.py +390 -0
- evidenceml/feature_selection/descriptive_statistics/_get_correlated_sets.py +40 -0
- evidenceml/feature_selection/descriptive_statistics/_gini.py +109 -0
- evidenceml/feature_selection/descriptive_statistics/_hhi.py +52 -0
- evidenceml/feature_selection/descriptive_statistics/_iv.py +46 -0
- evidenceml/feature_selection/descriptive_statistics/_ks.py +43 -0
- evidenceml/feature_selection/descriptive_statistics/_psi.py +84 -0
- evidenceml/feature_selection/descriptive_statistics/_woe.py +57 -0
- evidenceml/feature_selection/epoch/__init__.py +15 -0
- evidenceml/feature_selection/epoch/_get_backward_selection_epoch.py +127 -0
- evidenceml/feature_selection/epoch/_get_forward_selection_epoch.py +145 -0
- evidenceml/feature_selection/epoch/_iteration.py +129 -0
- evidenceml/feature_selection/epoch/_selection_epoch.py +181 -0
- evidenceml/feature_selection/forward_selection/__init__.py +15 -0
- evidenceml/feature_selection/forward_selection/_forward_selection.py +153 -0
- evidenceml/feature_selection/forward_selection/_get_forward_selection.py +192 -0
- evidenceml/feature_selection/woe_tools/__init__.py +27 -0
- evidenceml/feature_selection/woe_tools/_check_bins_monotonicity.py +85 -0
- evidenceml/feature_selection/woe_tools/_cut_series.py +13 -0
- evidenceml/feature_selection/woe_tools/_find_shares.py +7 -0
- evidenceml/feature_selection/woe_tools/_fit_tree_and_inference_params.py +82 -0
- evidenceml/feature_selection/woe_tools/_get_optimal_bins_numeric.py +80 -0
- evidenceml/feature_selection/woe_tools/_get_woe_numeric.py +309 -0
- evidenceml/feature_selection/woe_tools/_sort_woe_bins.py +43 -0
- evidenceml/feature_selection/woe_tools/_woe_bin.py +82 -0
- evidenceml/feature_selection/woe_tools/_woe_params.py +334 -0
- evidenceml/graphics/__init__.py +29 -0
- evidenceml/graphics/descriptive_statistics/__init__.py +5 -0
- evidenceml/graphics/descriptive_statistics/_plot_by_time.py +102 -0
- evidenceml/graphics/descriptive_statistics/_plot_cv.py +61 -0
- evidenceml/graphics/feature/__init__.py +5 -0
- evidenceml/graphics/feature/_features_gini.py +23 -0
- evidenceml/graphics/feature/_plot_feature.py +396 -0
- evidenceml/graphics/sample/__init__.py +3 -0
- evidenceml/graphics/sample/_plot_sample.py +397 -0
- evidenceml/graphics/scorecard/__init__.py +3 -0
- evidenceml/graphics/scorecard/_plot_grade_calibration.py +172 -0
- evidenceml/model_selection/__init__.py +21 -0
- evidenceml/model_selection/classifiers/__init__.py +8 -0
- evidenceml/model_selection/classifiers/_constrained_logistic_regression.py +259 -0
- evidenceml/model_selection/classifiers/_segmented_ensemble.py +110 -0
- evidenceml/model_selection/descriptive_statistics/__init__.py +5 -0
- evidenceml/model_selection/descriptive_statistics/_describe_sample.py +161 -0
- evidenceml/model_selection/split_by/__init__.py +18 -0
- evidenceml/model_selection/split_by/_calculate_fold_num_by_hash.py +40 -0
- evidenceml/model_selection/split_by/_check_test_set_by_id_hash.py +36 -0
- evidenceml/model_selection/split_by/_generate_m_folds.py +58 -0
- evidenceml/model_selection/split_by/_split_in_time.py +100 -0
- evidenceml/save/__init__.py +18 -0
- evidenceml/save/graphics/__init__.py +4 -0
- evidenceml/save/graphics/_append_fig_to_html.py +88 -0
- evidenceml/save/graphics/_save_figures_to_html.py +78 -0
- evidenceml/save/jsonify/__init__.py +3 -0
- evidenceml/save/jsonify/_create_json_info.py +82 -0
- evidenceml/scorecard/__init__.py +44 -0
- evidenceml/scorecard/_binomial_test_per_grade.py +138 -0
- evidenceml/scorecard/_hosmer_lemeshow_test.py +123 -0
- evidenceml/scorecard/_scorecard_transformer.py +494 -0
- evidenceml/scorecard/_simulation.py +750 -0
- evidenceml/scorecard/_simulation_config.py +166 -0
- evidenceml-0.32.1.dist-info/METADATA +64 -0
- evidenceml-0.32.1.dist-info/RECORD +97 -0
- evidenceml-0.32.1.dist-info/WHEEL +4 -0
- evidenceml-0.32.1.dist-info/licenses/LICENSE +27 -0
evidenceml/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.32.1" # managed by semantic-release
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from evidenceml.base._beartype import beartype
|
|
2
|
+
|
|
3
|
+
from evidenceml.base.functions import fast_correlation
|
|
4
|
+
from evidenceml.base.functions import merge_dicts
|
|
5
|
+
from evidenceml.base.functions import fill_na
|
|
6
|
+
from evidenceml.base.functions import has_parameters
|
|
7
|
+
from evidenceml.base.functions import read_pickle
|
|
8
|
+
|
|
9
|
+
from evidenceml.base.classes import NotFittedError
|
|
10
|
+
from evidenceml.base.classes import ModelNotCallableError
|
|
11
|
+
from evidenceml.base.classes import MissingMethodError
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"beartype",
|
|
15
|
+
"fast_correlation",
|
|
16
|
+
"merge_dicts",
|
|
17
|
+
"fill_na",
|
|
18
|
+
"has_parameters",
|
|
19
|
+
"read_pickle",
|
|
20
|
+
"NotFittedError",
|
|
21
|
+
"ModelNotCallableError",
|
|
22
|
+
"MissingMethodError",
|
|
23
|
+
]
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
from evidenceml.base.classes._not_fitted_error import NotFittedError
|
|
2
|
+
from evidenceml.base.classes._missing_method_error import MissingMethodError
|
|
3
|
+
from evidenceml.base.classes._model_not_callable_error import ModelNotCallableError
|
|
4
|
+
|
|
5
|
+
__all__ = ["NotFittedError", "MissingMethodError", "ModelNotCallableError"]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
class NotFittedError(Exception):
|
|
2
|
+
"""
|
|
3
|
+
Exception raised when a transformer is used before being fitted.
|
|
4
|
+
|
|
5
|
+
Parameters
|
|
6
|
+
----------
|
|
7
|
+
message : str, optional
|
|
8
|
+
Custom error message. Default is a standard not-fitted message.
|
|
9
|
+
|
|
10
|
+
Attributes
|
|
11
|
+
----------
|
|
12
|
+
message : str
|
|
13
|
+
The error message associated with the exception.
|
|
14
|
+
|
|
15
|
+
Examples
|
|
16
|
+
--------
|
|
17
|
+
>>> from evidenceml.base import NotFittedError
|
|
18
|
+
>>> try:
|
|
19
|
+
... raise NotFittedError()
|
|
20
|
+
... except NotFittedError as e:
|
|
21
|
+
... print(e)
|
|
22
|
+
This transformer is not fitted yet. Call 'fit' with appropriate arguments before using this method.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
message="This transformer is not fitted yet. "
|
|
28
|
+
"Call 'fit' with appropriate arguments before using this method.",
|
|
29
|
+
):
|
|
30
|
+
self.message = message
|
|
31
|
+
super().__init__(self.message)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from evidenceml.base.functions._fast_correlation import fast_correlation
|
|
2
|
+
from evidenceml.base.functions._fill_na import fill_na
|
|
3
|
+
from evidenceml.base.functions._has_parameters import has_parameters
|
|
4
|
+
from evidenceml.base.functions._merge_dicts import merge_dicts
|
|
5
|
+
from evidenceml.base.functions._read_pickle import read_pickle
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"fast_correlation",
|
|
9
|
+
"fill_na",
|
|
10
|
+
"has_parameters",
|
|
11
|
+
"merge_dicts",
|
|
12
|
+
"read_pickle",
|
|
13
|
+
]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from pandas import DataFrame
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def fast_correlation(df: DataFrame, absolute: bool = True) -> DataFrame:
|
|
7
|
+
"""
|
|
8
|
+
Compute pairwise correlation between all columns using NumPy.
|
|
9
|
+
|
|
10
|
+
Parameters
|
|
11
|
+
----------
|
|
12
|
+
df : pd.DataFrame
|
|
13
|
+
Input DataFrame; each column is treated as a variable.
|
|
14
|
+
absolute : bool, optional
|
|
15
|
+
If True, returns absolute values of correlations. Default is True.
|
|
16
|
+
|
|
17
|
+
Returns
|
|
18
|
+
-------
|
|
19
|
+
pd.DataFrame
|
|
20
|
+
Correlation matrix with the same columns and index as ``df``.
|
|
21
|
+
|
|
22
|
+
Examples
|
|
23
|
+
--------
|
|
24
|
+
>>> import pandas as pd
|
|
25
|
+
>>> from evidenceml.base import fast_correlation
|
|
26
|
+
>>> df = pd.DataFrame({"a": [1.0, 2.0, 3.0], "b": [3.0, 2.0, 1.0]})
|
|
27
|
+
>>> corr = fast_correlation(df)
|
|
28
|
+
>>> round(corr.loc["a", "b"], 6)
|
|
29
|
+
1.0
|
|
30
|
+
"""
|
|
31
|
+
if absolute:
|
|
32
|
+
corr_df = abs(np.corrcoef(df.values, rowvar=False))
|
|
33
|
+
else:
|
|
34
|
+
corr_df = np.corrcoef(df.values, rowvar=False)
|
|
35
|
+
|
|
36
|
+
corr_df = pd.DataFrame(
|
|
37
|
+
corr_df,
|
|
38
|
+
columns=pd.Index(df.columns.tolist()),
|
|
39
|
+
index=pd.Index(df.columns.tolist()),
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
return corr_df
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from pandas import DataFrame
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def fill_na(
|
|
5
|
+
df: DataFrame, category: str, column: str = "X"
|
|
6
|
+
) -> tuple[DataFrame, object]:
|
|
7
|
+
"""
|
|
8
|
+
Fill missing values in a DataFrame column based on a specified strategy.
|
|
9
|
+
|
|
10
|
+
Parameters
|
|
11
|
+
----------
|
|
12
|
+
df : pd.DataFrame
|
|
13
|
+
Input DataFrame containing the data.
|
|
14
|
+
category : {'min', 'max', 'mean', 'median', 'missing'}
|
|
15
|
+
Method to determine the fill value.
|
|
16
|
+
column : str, optional
|
|
17
|
+
Name of the column to fill. Default is ``'X'``.
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
-------
|
|
21
|
+
filled_df : pd.DataFrame
|
|
22
|
+
DataFrame with missing values filled.
|
|
23
|
+
fill_value : object
|
|
24
|
+
The value used for filling. If ``category`` is ``'missing'``, returns the
|
|
25
|
+
string ``'missing'``.
|
|
26
|
+
|
|
27
|
+
Raises
|
|
28
|
+
------
|
|
29
|
+
ValueError
|
|
30
|
+
If ``category`` is not one of the allowed values.
|
|
31
|
+
|
|
32
|
+
Examples
|
|
33
|
+
--------
|
|
34
|
+
>>> import pandas as pd
|
|
35
|
+
>>> from evidenceml.base import fill_na
|
|
36
|
+
>>> df = pd.DataFrame({"X": [1.0, None, 3.0]})
|
|
37
|
+
>>> filled, value = fill_na(df, category="mean")
|
|
38
|
+
>>> filled["X"].tolist()
|
|
39
|
+
[1.0, 2.0, 3.0]
|
|
40
|
+
>>> value
|
|
41
|
+
2.0
|
|
42
|
+
"""
|
|
43
|
+
df_ = df.copy()
|
|
44
|
+
|
|
45
|
+
valid_categories = ["min", "max", "mean", "median", "missing"]
|
|
46
|
+
|
|
47
|
+
# fill na with respect to treat parameter
|
|
48
|
+
if category == "min":
|
|
49
|
+
value = df_[column].min()
|
|
50
|
+
df_[column] = df_[column].fillna(df_[column].min())
|
|
51
|
+
elif category == "max":
|
|
52
|
+
value = df_[column].max()
|
|
53
|
+
df_[column] = df_[column].fillna(df_[column].max())
|
|
54
|
+
elif category == "mean":
|
|
55
|
+
value = df_[column].mean()
|
|
56
|
+
df_[column] = df_[column].fillna(df_[column].mean())
|
|
57
|
+
elif category == "median":
|
|
58
|
+
value = df_[column].median()
|
|
59
|
+
df_[column] = df_[column].fillna(df_[column].median())
|
|
60
|
+
elif category == "missing":
|
|
61
|
+
value = "missing"
|
|
62
|
+
else:
|
|
63
|
+
raise ValueError(f"category shall be in {valid_categories}, got {category}")
|
|
64
|
+
|
|
65
|
+
return df_, value
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def has_parameters(func, params: List[str]) -> bool:
|
|
6
|
+
"""
|
|
7
|
+
Check whether a callable accepts all of the specified parameter names.
|
|
8
|
+
|
|
9
|
+
Parameters
|
|
10
|
+
----------
|
|
11
|
+
func : callable
|
|
12
|
+
Function or method to inspect.
|
|
13
|
+
params : list of str
|
|
14
|
+
Parameter names to check for.
|
|
15
|
+
|
|
16
|
+
Returns
|
|
17
|
+
-------
|
|
18
|
+
bool
|
|
19
|
+
True if all names in ``params`` appear in ``func``'s signature.
|
|
20
|
+
|
|
21
|
+
Examples
|
|
22
|
+
--------
|
|
23
|
+
>>> from evidenceml.base import has_parameters
|
|
24
|
+
>>> def my_fn(x, y, z=0): ...
|
|
25
|
+
>>> has_parameters(my_fn, ["x", "y"])
|
|
26
|
+
True
|
|
27
|
+
>>> has_parameters(my_fn, ["x", "w"])
|
|
28
|
+
False
|
|
29
|
+
"""
|
|
30
|
+
try:
|
|
31
|
+
signature = inspect.signature(func)
|
|
32
|
+
parameters = signature.parameters
|
|
33
|
+
return all(param in parameters for param in params)
|
|
34
|
+
except ValueError:
|
|
35
|
+
return False
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from typing import Dict
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def merge_dicts(*dict_args) -> Dict:
|
|
5
|
+
"""
|
|
6
|
+
Shallow-copy and merge any number of dicts into a new dict.
|
|
7
|
+
|
|
8
|
+
Precedence goes to key-value pairs in latter dictionaries.
|
|
9
|
+
|
|
10
|
+
Parameters
|
|
11
|
+
----------
|
|
12
|
+
dict_args : dict
|
|
13
|
+
Any number of dictionaries to merge (passed as positional arguments).
|
|
14
|
+
|
|
15
|
+
Returns
|
|
16
|
+
-------
|
|
17
|
+
dict
|
|
18
|
+
Merged dictionary.
|
|
19
|
+
|
|
20
|
+
Examples
|
|
21
|
+
--------
|
|
22
|
+
>>> from evidenceml.base import merge_dicts
|
|
23
|
+
>>> merge_dicts({"a": 1}, {"b": 2}, {"a": 99})
|
|
24
|
+
{'a': 99, 'b': 2}
|
|
25
|
+
"""
|
|
26
|
+
result = {}
|
|
27
|
+
for dictionary in dict_args:
|
|
28
|
+
result.update(dictionary)
|
|
29
|
+
return result
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import pickle
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def read_pickle(path: str):
|
|
5
|
+
"""
|
|
6
|
+
Reads a serialized object from a file and returns the deserialized object.
|
|
7
|
+
|
|
8
|
+
Parameters
|
|
9
|
+
----------
|
|
10
|
+
path : str
|
|
11
|
+
The file path from which the serialized object will be read. This file should have been created
|
|
12
|
+
using the `pickle` module.
|
|
13
|
+
|
|
14
|
+
Returns
|
|
15
|
+
-------
|
|
16
|
+
object
|
|
17
|
+
The deserialized Python object.
|
|
18
|
+
|
|
19
|
+
Raises
|
|
20
|
+
------
|
|
21
|
+
FileNotFoundError
|
|
22
|
+
If the specified file does not exist.
|
|
23
|
+
IOError
|
|
24
|
+
If there is an error reading the file.
|
|
25
|
+
|
|
26
|
+
Examples
|
|
27
|
+
--------
|
|
28
|
+
>>> woe_base = read_pickle('woe_base.pkl')
|
|
29
|
+
|
|
30
|
+
This will read the serialized `WOEBase` instance from 'woe_base.pkl'.
|
|
31
|
+
"""
|
|
32
|
+
with open(path, "rb") as file:
|
|
33
|
+
return pickle.load(file)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from evidenceml.encoding_and_transforms.encodings import reduce_categories
|
|
2
|
+
|
|
3
|
+
from evidenceml.encoding_and_transforms.transformers import WOEBase
|
|
4
|
+
from evidenceml.encoding_and_transforms.transformers import WOEBoolean
|
|
5
|
+
from evidenceml.encoding_and_transforms.transformers import WOENumerical
|
|
6
|
+
from evidenceml.encoding_and_transforms.transformers import WOECategorical
|
|
7
|
+
from evidenceml.encoding_and_transforms.transformers import WOEColumnTransformer
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"reduce_categories",
|
|
11
|
+
"WOEBase",
|
|
12
|
+
"WOEBoolean",
|
|
13
|
+
"WOENumerical",
|
|
14
|
+
"WOECategorical",
|
|
15
|
+
"WOEColumnTransformer",
|
|
16
|
+
]
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
from typing import Optional, Set, Union
|
|
2
|
+
|
|
3
|
+
from pandas import Series
|
|
4
|
+
import warnings
|
|
5
|
+
from ...base import beartype
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@beartype
|
|
9
|
+
def reduce_categories(
|
|
10
|
+
data: Series,
|
|
11
|
+
min_share: Union[float, int] = 0.05,
|
|
12
|
+
fill_na: str = "missing",
|
|
13
|
+
fill_minority: str = "minority",
|
|
14
|
+
constant_names: Optional[Set[str]] = None,
|
|
15
|
+
) -> Series:
|
|
16
|
+
"""
|
|
17
|
+
Reduces the number of categories in a pandas Series by grouping low-frequency categories
|
|
18
|
+
into a single 'minority' category based on a specified minimum share threshold. It also
|
|
19
|
+
allows specifying categories that should remain unaltered.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
data : pandas.Series
|
|
24
|
+
The pandas Series object containing categorical data.
|
|
25
|
+
min_share : float or int, optional
|
|
26
|
+
The minimum share (between 0 and 1) a category must have to not be considered
|
|
27
|
+
a minority. Values outside this range are adjusted to the nearest bound, with
|
|
28
|
+
a UserWarning issued. Default is 0.05.
|
|
29
|
+
fill_na : str, optional
|
|
30
|
+
The string to use for filling NaN values in the data. Default is 'missing'.
|
|
31
|
+
fill_minority : str, optional
|
|
32
|
+
The string to assign to the grouped minority categories. Default is 'minority'.
|
|
33
|
+
constant_names : set of str, optional
|
|
34
|
+
Categories that should not be altered or included in the minority grouping.
|
|
35
|
+
If None, no category is exempt from grouping. Default is None.
|
|
36
|
+
|
|
37
|
+
Returns
|
|
38
|
+
-------
|
|
39
|
+
pandas.Series
|
|
40
|
+
A new pandas Series with reduced category set.
|
|
41
|
+
|
|
42
|
+
Raises
|
|
43
|
+
------
|
|
44
|
+
TypeError
|
|
45
|
+
If input types are not as expected.
|
|
46
|
+
UserWarning
|
|
47
|
+
If `min_share` is adjusted due to being out of the [0, 1] range.
|
|
48
|
+
|
|
49
|
+
Examples
|
|
50
|
+
--------
|
|
51
|
+
>>> data = pd.Series(['A', 'B', 'C', 'A', 'B', 'B', 'D'])
|
|
52
|
+
>>> reduce_categories(data, min_share=0.1, fill_na='missing', fill_minority='Other')
|
|
53
|
+
0 Other
|
|
54
|
+
1 B
|
|
55
|
+
2 Other
|
|
56
|
+
3 Other
|
|
57
|
+
4 B
|
|
58
|
+
5 B
|
|
59
|
+
6 Other
|
|
60
|
+
dtype: object
|
|
61
|
+
"""
|
|
62
|
+
if min_share < 0:
|
|
63
|
+
warnings.warn(
|
|
64
|
+
f"min_share cannot be less than 0 (got {min_share}), value is changed to 0",
|
|
65
|
+
UserWarning,
|
|
66
|
+
stacklevel=2,
|
|
67
|
+
)
|
|
68
|
+
min_share_ = 0
|
|
69
|
+
elif min_share > 1:
|
|
70
|
+
warnings.warn(
|
|
71
|
+
f"min_share cannot be more than 1 (got {min_share}), value is changed to 1",
|
|
72
|
+
UserWarning,
|
|
73
|
+
stacklevel=2,
|
|
74
|
+
)
|
|
75
|
+
min_share_ = 1
|
|
76
|
+
else:
|
|
77
|
+
min_share_ = min_share
|
|
78
|
+
|
|
79
|
+
if constant_names is None:
|
|
80
|
+
special_all = {fill_minority, fill_na}
|
|
81
|
+
else:
|
|
82
|
+
special_all = {fill_minority, fill_na}.union(constant_names)
|
|
83
|
+
|
|
84
|
+
data_ = data.copy()
|
|
85
|
+
|
|
86
|
+
data_ = data_.fillna(fill_na).infer_objects(copy=False)
|
|
87
|
+
|
|
88
|
+
data_ = data_.astype(str)
|
|
89
|
+
|
|
90
|
+
shares = data_.value_counts(normalize=True)
|
|
91
|
+
|
|
92
|
+
reduced_categories = shares.apply(lambda x: x <= min_share_)
|
|
93
|
+
|
|
94
|
+
reduced_categories = set(shares.loc[reduced_categories].index.to_list())
|
|
95
|
+
|
|
96
|
+
reduce_set = reduced_categories.difference(special_all)
|
|
97
|
+
|
|
98
|
+
data_ = data_.replace(to_replace=reduce_set, value=fill_minority)
|
|
99
|
+
|
|
100
|
+
return data_
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from evidenceml.encoding_and_transforms.transformers._woe_base import WOEBase
|
|
2
|
+
from evidenceml.encoding_and_transforms.transformers._woe_numerical import WOENumerical
|
|
3
|
+
from evidenceml.encoding_and_transforms.transformers._woe_categorical import (
|
|
4
|
+
WOECategorical,
|
|
5
|
+
)
|
|
6
|
+
from evidenceml.encoding_and_transforms.transformers._woe_boolean import WOEBoolean
|
|
7
|
+
from evidenceml.encoding_and_transforms.transformers._woe_column_transformer import (
|
|
8
|
+
WOEColumnTransformer,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"WOEBase",
|
|
13
|
+
"WOENumerical",
|
|
14
|
+
"WOECategorical",
|
|
15
|
+
"WOEBoolean",
|
|
16
|
+
"WOEColumnTransformer",
|
|
17
|
+
]
|