SuperModelingFactory 0.3.7__tar.gz → 0.3.9__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.
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Explainability/Coalition_Structure.py +6 -4
- supermodelingfactory-0.3.9/Modeling_Tool/Feature/Feature_Screen.py +394 -0
- supermodelingfactory-0.3.9/Modeling_Tool/Feature/Weighted_Screen.py +544 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Feature/__init__.py +20 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/credit_model.py +56 -66
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Plot_Tool.py +1 -2
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/__init__.py +17 -1
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/PKG-INFO +1 -1
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/SuperModelingFactory.egg-info/PKG-INFO +1 -1
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/SuperModelingFactory.egg-info/SOURCES.txt +2 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/pyproject.toml +1 -1
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/setup.py +1 -1
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/ExcelMaster/ExcelFormatTool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/ExcelMaster/ExcelMaster.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/ExcelMaster/Template.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/ExcelMaster/Utility.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/ExcelMaster/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/LICENSE +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/MANIFEST.in +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Binning_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Binning_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Check_DuckDB_Compatibility.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Json_Data_Converter.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Model_Registry_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/ODPS_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Parallel_Engine.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Parallel_ODPS_Manager.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Proc_Compare.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Slope_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/Slope_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/XOR_Encryptor.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/XOR_Encryptor.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/kDataFrame.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/kDataFrame.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/sample_weight_utils.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Core/utils.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Eval/Evaluation_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Eval/Evaluation_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Eval/Model_Eval_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Eval/Model_Eval_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Eval/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Eval/evaluate_model.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Eval/evaluate_model.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Eval/weighted_eval_utils.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Explainability/Model_Explainer.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Explainability/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Feature/Distribution_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Feature/Distribution_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Feature/Feature_Insights.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Feature/Feature_Insights.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Feature/PSI_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Feature/PSI_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Feature/WOE_Engine_Feature_Patch.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Model/Backward_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Model/Backward_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Model/GBM_Search_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Model/GBM_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Model/GBM_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Model/LRM_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Model/LRM_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Model/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/_common.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/feature_validation.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/mock_sample.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/reject_inference.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/sample_analysis.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/score_comparison.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Pipeline/score_consistency_uat.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Sample/Distribution_Adaptation.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Sample/Distribution_Adaptation.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Sample/Reject_Infer.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Sample/Reject_Infer.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Sample/Sample_Split.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Sample/Sample_Split.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/Sample/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/UAT/UAT_Consistency_Checker.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/UAT/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Adapter.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Adapter.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Master.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Master.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Monotone_Binner.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Monotone_Binner.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Plot_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Report_Builder.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Report_Builder.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/WOE_Tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/plot_woe_tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/WOE/plot_woe_tool.pyi +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/ref_font/KaiTi.ttf +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/ref_font/WeiRuanYaHei.ttf +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/ref_font/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Modeling_Tool/ref_font/simsun.ttc +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/README.md +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Report/Report_Tool.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/Report/__init__.py +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/SuperModelingFactory.egg-info/dependency_links.txt +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/SuperModelingFactory.egg-info/not-zip-safe +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/SuperModelingFactory.egg-info/requires.txt +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/SuperModelingFactory.egg-info/top_level.txt +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/requirements.txt +0 -0
- {supermodelingfactory-0.3.7 → supermodelingfactory-0.3.9}/setup.cfg +0 -0
|
@@ -108,8 +108,9 @@ def _association_matrix(frame: pd.DataFrame, corr_method: str = "spearman") -> p
|
|
|
108
108
|
assoc = frame.corr(method=corr_method).abs()
|
|
109
109
|
assoc = assoc.reindex(index=frame.columns, columns=frame.columns).fillna(0.0)
|
|
110
110
|
assoc = assoc.clip(lower=0.0, upper=1.0)
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
arr = assoc.to_numpy(dtype=float, copy=True)
|
|
112
|
+
np.fill_diagonal(arr, 1.0)
|
|
113
|
+
return pd.DataFrame(arr, index=assoc.index, columns=assoc.columns)
|
|
113
114
|
|
|
114
115
|
|
|
115
116
|
def _as_numeric_frame(X: pd.DataFrame) -> pd.DataFrame:
|
|
@@ -136,8 +137,9 @@ def _correlation_distance(X: pd.DataFrame, corr_method: str = "spearman") -> pd.
|
|
|
136
137
|
frame = _as_numeric_frame(X)
|
|
137
138
|
assoc = _association_matrix(frame, corr_method=corr_method)
|
|
138
139
|
dist = 1.0 - assoc
|
|
139
|
-
|
|
140
|
-
|
|
140
|
+
arr = dist.to_numpy(dtype=float, copy=True)
|
|
141
|
+
np.fill_diagonal(arr, 0.0)
|
|
142
|
+
return pd.DataFrame(arr, index=dist.index, columns=dist.columns)
|
|
141
143
|
|
|
142
144
|
|
|
143
145
|
def compute_correlation_linkage(
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"""Unified feature screening: PSI -> IV -> correlation dedup."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import warnings
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, Mapping
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
import pandas as pd
|
|
12
|
+
|
|
13
|
+
from Modeling_Tool.Pipeline._common import apply_woe_fit_query, as_list
|
|
14
|
+
|
|
15
|
+
from .Weighted_Screen import (
|
|
16
|
+
WeightedScreenResult,
|
|
17
|
+
_legacy_unweighted_screen,
|
|
18
|
+
_resolve_splits,
|
|
19
|
+
_summary_row,
|
|
20
|
+
_weighted_screen_impl,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
FeatureScreenResult = WeightedScreenResult
|
|
24
|
+
|
|
25
|
+
_MONOTONE_INIT_KEYS = frozenset({
|
|
26
|
+
"n_init_bins",
|
|
27
|
+
"min_bin_size",
|
|
28
|
+
"min_n_bins",
|
|
29
|
+
"eps",
|
|
30
|
+
"missing_woe",
|
|
31
|
+
"special_values",
|
|
32
|
+
"bin_label_decimals",
|
|
33
|
+
})
|
|
34
|
+
_MONOTONE_FIT_KEYS = frozenset({"chi2_binning", "chi2_p", "chi2_init_size", "n_jobs"})
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass
|
|
38
|
+
class FeatureScreenConfig:
|
|
39
|
+
psi_enabled: bool = True
|
|
40
|
+
psi_threshold: float = 0.2
|
|
41
|
+
psi_compare_splits: list[str] = field(default_factory=lambda: ["oos"])
|
|
42
|
+
psi_buckets: int = 10
|
|
43
|
+
psi_use_woe_bins: bool = False
|
|
44
|
+
iv_enabled: bool = True
|
|
45
|
+
iv_threshold: float = 0.02
|
|
46
|
+
iv_bins: int = 10
|
|
47
|
+
iv_min_bin_prop: float = 0.05
|
|
48
|
+
iv_equal_freq: bool = True
|
|
49
|
+
iv_use_woe_bins: bool = False
|
|
50
|
+
corr_enabled: bool = True
|
|
51
|
+
corr_threshold: float = 0.75
|
|
52
|
+
corr_max_iterations: int = 10
|
|
53
|
+
corr_use_woe_bins: bool = False
|
|
54
|
+
woe_engine: str = "equal_freq"
|
|
55
|
+
woe_fit_query: str | None = None
|
|
56
|
+
woe_params: dict[str, Any] = field(
|
|
57
|
+
default_factory=lambda: {"nbins": 10, "equal_freq": True, "min_bin_prop": 0.05}
|
|
58
|
+
)
|
|
59
|
+
monotone_woe_params: dict[str, Any] = field(
|
|
60
|
+
default_factory=lambda: {"n_init_bins": 20, "min_bin_size": 0.03, "min_n_bins": 2}
|
|
61
|
+
)
|
|
62
|
+
categorical_features: list[str] | None = None
|
|
63
|
+
plot_path: str | None = None
|
|
64
|
+
plot_outputs: bool = False
|
|
65
|
+
content: float = 1e-6
|
|
66
|
+
precision: int = 5
|
|
67
|
+
min_bin_prop: float = 0.05
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def screen_config_from_mapping(
|
|
71
|
+
mapping: Mapping[str, Any] | None,
|
|
72
|
+
*,
|
|
73
|
+
woe_engine: str | None = None,
|
|
74
|
+
woe_fit_query: str | None = None,
|
|
75
|
+
woe_params: Mapping[str, Any] | None = None,
|
|
76
|
+
monotone_woe_params: Mapping[str, Any] | None = None,
|
|
77
|
+
plot_path: str | None = None,
|
|
78
|
+
plot_outputs: bool = False,
|
|
79
|
+
) -> FeatureScreenConfig:
|
|
80
|
+
"""Build ``FeatureScreenConfig`` from a CM-style ``feature_selection`` dict."""
|
|
81
|
+
cfg = dict(mapping or {})
|
|
82
|
+
iv_nbins = int(cfg.get("iv_nbins", cfg.get("iv_bins", 10)))
|
|
83
|
+
return FeatureScreenConfig(
|
|
84
|
+
psi_enabled=bool(cfg.get("psi_enabled", True)),
|
|
85
|
+
psi_threshold=float(cfg.get("psi_threshold", 0.2)),
|
|
86
|
+
psi_compare_splits=list(cfg.get("psi_compare_splits", ["oos"])),
|
|
87
|
+
psi_buckets=int(cfg.get("psi_buckets", iv_nbins)),
|
|
88
|
+
psi_use_woe_bins=bool(cfg.get("psi_use_woe_bins", False)),
|
|
89
|
+
iv_enabled=bool(cfg.get("iv_enabled", True)),
|
|
90
|
+
iv_threshold=float(cfg.get("iv_threshold", 0.02)),
|
|
91
|
+
iv_bins=iv_nbins,
|
|
92
|
+
iv_min_bin_prop=float(cfg.get("iv_min_bin_prop", 0.05)),
|
|
93
|
+
iv_equal_freq=bool(cfg.get("iv_equal_freq", True)),
|
|
94
|
+
iv_use_woe_bins=bool(cfg.get("iv_use_woe_bins", False)),
|
|
95
|
+
corr_enabled=bool(cfg.get("corr_enabled", True)),
|
|
96
|
+
corr_threshold=float(cfg.get("corr_threshold", 0.75)),
|
|
97
|
+
corr_max_iterations=int(cfg.get("corr_max_iterations", 10)),
|
|
98
|
+
corr_use_woe_bins=bool(cfg.get("corr_use_woe_bins", False)),
|
|
99
|
+
woe_engine=str(woe_engine or cfg.get("woe_engine", "equal_freq")),
|
|
100
|
+
woe_fit_query=woe_fit_query if woe_fit_query is not None else cfg.get("woe_fit_query"),
|
|
101
|
+
woe_params=dict(FeatureScreenConfig().woe_params | dict(woe_params or cfg.get("woe_params") or {})),
|
|
102
|
+
monotone_woe_params=dict(
|
|
103
|
+
FeatureScreenConfig().monotone_woe_params | dict(monotone_woe_params or cfg.get("monotone_woe_params") or {})
|
|
104
|
+
),
|
|
105
|
+
categorical_features=list(cfg["categorical_features"]) if cfg.get("categorical_features") else None,
|
|
106
|
+
plot_path=plot_path,
|
|
107
|
+
plot_outputs=plot_outputs,
|
|
108
|
+
content=float(cfg.get("content", 1e-6)),
|
|
109
|
+
precision=int(cfg.get("precision", 5)),
|
|
110
|
+
min_bin_prop=float(cfg.get("min_bin_prop", cfg.get("iv_min_bin_prop", 0.05))),
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def fit_screening_woe_engine(
|
|
115
|
+
train: pd.DataFrame,
|
|
116
|
+
features: list[str],
|
|
117
|
+
target_col: str,
|
|
118
|
+
*,
|
|
119
|
+
woe_engine: str = "monotone",
|
|
120
|
+
woe_fit_query: str | None = None,
|
|
121
|
+
woe_params: Mapping[str, Any] | None = None,
|
|
122
|
+
monotone_woe_params: Mapping[str, Any] | None = None,
|
|
123
|
+
categorical_features: list[str] | None = None,
|
|
124
|
+
) -> Any:
|
|
125
|
+
"""Fit a WOE engine on INS for screening steps that reuse bin boundaries."""
|
|
126
|
+
fit_ins, _ = apply_woe_fit_query(train, woe_fit_query, target=target_col)
|
|
127
|
+
if woe_engine.lower() == "monotone":
|
|
128
|
+
from Modeling_Tool import MonotoneWOEBinner
|
|
129
|
+
|
|
130
|
+
params = dict(monotone_woe_params or {})
|
|
131
|
+
init_params = {k: v for k, v in params.items() if k in _MONOTONE_INIT_KEYS}
|
|
132
|
+
fit_params = {k: v for k, v in params.items() if k in _MONOTONE_FIT_KEYS}
|
|
133
|
+
categorical = [col for col in as_list(categorical_features) if col in features]
|
|
134
|
+
numeric = [col for col in features if col not in set(categorical)]
|
|
135
|
+
binner = MonotoneWOEBinner(
|
|
136
|
+
feature_cols=numeric,
|
|
137
|
+
target_col=target_col,
|
|
138
|
+
cate_feats=categorical,
|
|
139
|
+
**init_params,
|
|
140
|
+
)
|
|
141
|
+
binner.fit(fit_ins, **fit_params)
|
|
142
|
+
return binner
|
|
143
|
+
|
|
144
|
+
from Modeling_Tool import WOE_Master
|
|
145
|
+
|
|
146
|
+
master_params = dict(woe_params or {})
|
|
147
|
+
woe_suffix = master_params.pop("woe_suffix", "_woe")
|
|
148
|
+
missing_ref_value = master_params.pop("missing_ref_value", -999999)
|
|
149
|
+
master = WOE_Master(
|
|
150
|
+
train_data=fit_ins,
|
|
151
|
+
varlist=features,
|
|
152
|
+
dep=target_col,
|
|
153
|
+
graph_save_dir=None,
|
|
154
|
+
woe_suffix=woe_suffix,
|
|
155
|
+
missing_ref_value=missing_ref_value,
|
|
156
|
+
)
|
|
157
|
+
master.fit(**master_params)
|
|
158
|
+
return master
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _needs_woe_bins(config: FeatureScreenConfig) -> bool:
|
|
162
|
+
return config.psi_use_woe_bins or config.iv_use_woe_bins or config.corr_use_woe_bins
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _resolve_screening_binner(
|
|
166
|
+
splits: dict[str, pd.DataFrame],
|
|
167
|
+
feature_cols: list[str],
|
|
168
|
+
target_col: str,
|
|
169
|
+
config: FeatureScreenConfig,
|
|
170
|
+
prefit_woe_engine: Any | None,
|
|
171
|
+
) -> Any | None:
|
|
172
|
+
if prefit_woe_engine is not None:
|
|
173
|
+
return prefit_woe_engine
|
|
174
|
+
if not _needs_woe_bins(config):
|
|
175
|
+
return None
|
|
176
|
+
return fit_screening_woe_engine(
|
|
177
|
+
splits["ins"],
|
|
178
|
+
feature_cols,
|
|
179
|
+
target_col,
|
|
180
|
+
woe_engine=config.woe_engine,
|
|
181
|
+
woe_fit_query=config.woe_fit_query,
|
|
182
|
+
woe_params=config.woe_params,
|
|
183
|
+
monotone_woe_params=config.monotone_woe_params,
|
|
184
|
+
categorical_features=config.categorical_features,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _woe_bins_unweighted_screen(
|
|
189
|
+
splits: dict[str, pd.DataFrame],
|
|
190
|
+
feature_cols: list[str],
|
|
191
|
+
target_col: str,
|
|
192
|
+
config: FeatureScreenConfig,
|
|
193
|
+
*,
|
|
194
|
+
prefit_woe_engine: Any | None = None,
|
|
195
|
+
) -> FeatureScreenResult:
|
|
196
|
+
from Modeling_Tool import CorrelationFilter, PSICalculator, VarExtractionInsights
|
|
197
|
+
|
|
198
|
+
ins, oos, oot = splits["ins"], splits["oos"], splits["oot"]
|
|
199
|
+
current = list(feature_cols)
|
|
200
|
+
summary_rows = [_summary_row("initial", len(feature_cols), len(current), None, None)]
|
|
201
|
+
binner = _resolve_screening_binner(splits, feature_cols, target_col, config, prefit_woe_engine)
|
|
202
|
+
|
|
203
|
+
psi_table = pd.DataFrame(columns=["var", "psi_ins_oos", "psi_ins_oot", "psi_max"])
|
|
204
|
+
if config.psi_enabled:
|
|
205
|
+
psi_calc = (
|
|
206
|
+
PSICalculator(buckets=config.psi_buckets, binning_engine=binner)
|
|
207
|
+
if config.psi_use_woe_bins and binner is not None
|
|
208
|
+
else PSICalculator(buckets=config.psi_buckets)
|
|
209
|
+
)
|
|
210
|
+
psi_frames = []
|
|
211
|
+
if "oos" in config.psi_compare_splits and len(oos) > 0:
|
|
212
|
+
psi_oos = psi_calc.calculate(expected_df=ins, current_data=oos, varlist=current)
|
|
213
|
+
psi_oos = psi_oos.rename(columns={"psi": "psi_ins_oos"})[["var", "psi_ins_oos"]]
|
|
214
|
+
psi_frames.append(psi_oos)
|
|
215
|
+
if "oot" in config.psi_compare_splits and len(oot) > 0:
|
|
216
|
+
psi_oot = psi_calc.calculate(expected_df=ins, current_data=oot, varlist=current)
|
|
217
|
+
psi_oot = psi_oot.rename(columns={"psi": "psi_ins_oot"})[["var", "psi_ins_oot"]]
|
|
218
|
+
psi_frames.append(psi_oot)
|
|
219
|
+
|
|
220
|
+
if psi_frames:
|
|
221
|
+
psi_table = psi_frames[0]
|
|
222
|
+
for frame in psi_frames[1:]:
|
|
223
|
+
psi_table = psi_table.merge(frame, on="var", how="outer")
|
|
224
|
+
for col in ("psi_ins_oos", "psi_ins_oot"):
|
|
225
|
+
if col not in psi_table.columns:
|
|
226
|
+
psi_table[col] = np.nan
|
|
227
|
+
compare_cols = [c for c in ("psi_ins_oos", "psi_ins_oot") if c in psi_table.columns]
|
|
228
|
+
psi_table["psi_max"] = psi_table[compare_cols].max(axis=1, skipna=True)
|
|
229
|
+
keep = psi_table.loc[psi_table["psi_max"] < config.psi_threshold, "var"].tolist()
|
|
230
|
+
n_before = len(current)
|
|
231
|
+
current = keep or current
|
|
232
|
+
summary_rows.append(_summary_row("psi", n_before, len(current), config.psi_threshold, None))
|
|
233
|
+
|
|
234
|
+
iv_table = pd.DataFrame(columns=["var", "iv_weighted", "n_bins", "missing_rate"])
|
|
235
|
+
if config.iv_enabled:
|
|
236
|
+
use_binner = config.iv_use_woe_bins and binner is not None
|
|
237
|
+
vi = VarExtractionInsights(
|
|
238
|
+
data=ins,
|
|
239
|
+
dep=target_col,
|
|
240
|
+
plot_path=config.plot_path or "",
|
|
241
|
+
nbins=config.iv_bins,
|
|
242
|
+
equal_freq=config.iv_equal_freq,
|
|
243
|
+
min_bin_prop=config.iv_min_bin_prop,
|
|
244
|
+
woe_binner=binner if use_binner else None,
|
|
245
|
+
woe_engine="monotone" if use_binner else "master",
|
|
246
|
+
)
|
|
247
|
+
iv = vi.get_var_analysis_report(data=ins, varlist=current, dep=target_col, iv_cut=0.0)
|
|
248
|
+
if config.plot_path and config.plot_outputs and current:
|
|
249
|
+
Path(config.plot_path, "overall").mkdir(parents=True, exist_ok=True)
|
|
250
|
+
plot_data = ins.copy()
|
|
251
|
+
plot_data["_smf_plot_group"] = "overall"
|
|
252
|
+
vi.plot_woe(
|
|
253
|
+
data=plot_data,
|
|
254
|
+
varlist=current,
|
|
255
|
+
plot_group="_smf_plot_group",
|
|
256
|
+
plot_dirname="overall",
|
|
257
|
+
plot_path=config.plot_path,
|
|
258
|
+
)
|
|
259
|
+
if iv is not None and not iv.empty:
|
|
260
|
+
iv_table = iv.rename(columns={"iv": "iv_weighted"})[["var", "iv_weighted", "n_bins", "missing_rate"]]
|
|
261
|
+
keep = iv.loc[iv["iv"] >= config.iv_threshold, "var"].tolist()
|
|
262
|
+
n_before = len(current)
|
|
263
|
+
current = keep or current
|
|
264
|
+
summary_rows.append(_summary_row("iv", n_before, len(current), config.iv_threshold, None))
|
|
265
|
+
|
|
266
|
+
corr_dropped = pd.DataFrame(columns=["var_a", "var_b", "corr", "iv_a", "iv_b", "kept", "dropped"])
|
|
267
|
+
if config.corr_enabled and len(current) > 1:
|
|
268
|
+
use_binner = config.corr_use_woe_bins and binner is not None
|
|
269
|
+
cf = CorrelationFilter(
|
|
270
|
+
data=ins[current + [target_col]],
|
|
271
|
+
dep=target_col,
|
|
272
|
+
corr_cutpoint=config.corr_threshold,
|
|
273
|
+
woe_binner=binner if use_binner else None,
|
|
274
|
+
woe_engine="monotone" if use_binner else "master",
|
|
275
|
+
)
|
|
276
|
+
n_before = len(current)
|
|
277
|
+
current = cf.remove_highly_correlated(current, max_iterations=config.corr_max_iterations)
|
|
278
|
+
summary_rows.append(_summary_row("corr", n_before, len(current), config.corr_threshold, None))
|
|
279
|
+
|
|
280
|
+
summary_rows.append(_summary_row("final", len(feature_cols), len(current), None, None))
|
|
281
|
+
return FeatureScreenResult(
|
|
282
|
+
selected_features=list(current),
|
|
283
|
+
iv_table=iv_table,
|
|
284
|
+
psi_table=psi_table,
|
|
285
|
+
corr_dropped=corr_dropped,
|
|
286
|
+
summary=pd.DataFrame(summary_rows),
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def feature_screen(
|
|
291
|
+
splits: dict[str, pd.DataFrame],
|
|
292
|
+
feature_cols: list[str],
|
|
293
|
+
target_col: str,
|
|
294
|
+
*,
|
|
295
|
+
weight_col: str | None = None,
|
|
296
|
+
config: FeatureScreenConfig | None = None,
|
|
297
|
+
prefit_woe_engine: Any | None = None,
|
|
298
|
+
) -> FeatureScreenResult:
|
|
299
|
+
"""Run PSI -> IV -> correlation screening on pre-split INS/OOS/OOT frames."""
|
|
300
|
+
cfg = config or FeatureScreenConfig()
|
|
301
|
+
use_woe_bins = _needs_woe_bins(cfg)
|
|
302
|
+
|
|
303
|
+
if weight_col is not None:
|
|
304
|
+
if use_woe_bins:
|
|
305
|
+
warnings.warn(
|
|
306
|
+
"Weighted feature screening with psi/iv/corr_use_woe_bins is not fully "
|
|
307
|
+
"supported yet; falling back to weighted equal-frequency bins.",
|
|
308
|
+
stacklevel=2,
|
|
309
|
+
)
|
|
310
|
+
return _weighted_screen_impl(
|
|
311
|
+
splits,
|
|
312
|
+
feature_cols,
|
|
313
|
+
target_col,
|
|
314
|
+
weight_col,
|
|
315
|
+
psi_enabled=cfg.psi_enabled,
|
|
316
|
+
psi_threshold=cfg.psi_threshold,
|
|
317
|
+
psi_compare_splits=list(cfg.psi_compare_splits),
|
|
318
|
+
iv_enabled=cfg.iv_enabled,
|
|
319
|
+
iv_threshold=cfg.iv_threshold,
|
|
320
|
+
iv_bins=cfg.iv_bins,
|
|
321
|
+
min_bin_prop=cfg.min_bin_prop,
|
|
322
|
+
corr_enabled=cfg.corr_enabled,
|
|
323
|
+
corr_threshold=cfg.corr_threshold,
|
|
324
|
+
corr_max_iterations=cfg.corr_max_iterations,
|
|
325
|
+
content=cfg.content,
|
|
326
|
+
precision=cfg.precision,
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
if use_woe_bins or prefit_woe_engine is not None:
|
|
330
|
+
return _woe_bins_unweighted_screen(
|
|
331
|
+
splits,
|
|
332
|
+
feature_cols,
|
|
333
|
+
target_col,
|
|
334
|
+
cfg,
|
|
335
|
+
prefit_woe_engine=prefit_woe_engine,
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
return _legacy_unweighted_screen(
|
|
339
|
+
splits,
|
|
340
|
+
feature_cols,
|
|
341
|
+
target_col,
|
|
342
|
+
psi_enabled=cfg.psi_enabled,
|
|
343
|
+
psi_threshold=cfg.psi_threshold,
|
|
344
|
+
psi_compare_splits=list(cfg.psi_compare_splits),
|
|
345
|
+
iv_enabled=cfg.iv_enabled,
|
|
346
|
+
iv_threshold=cfg.iv_threshold,
|
|
347
|
+
iv_bins=cfg.iv_bins,
|
|
348
|
+
iv_min_bin_prop=cfg.iv_min_bin_prop,
|
|
349
|
+
corr_enabled=cfg.corr_enabled,
|
|
350
|
+
corr_threshold=cfg.corr_threshold,
|
|
351
|
+
corr_max_iterations=cfg.corr_max_iterations,
|
|
352
|
+
psi_buckets=cfg.psi_buckets,
|
|
353
|
+
plot_path=cfg.plot_path,
|
|
354
|
+
plot_outputs=cfg.plot_outputs,
|
|
355
|
+
iv_equal_freq=cfg.iv_equal_freq,
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def feature_screen_from_dataframe(
|
|
360
|
+
data: pd.DataFrame,
|
|
361
|
+
feature_cols: list[str],
|
|
362
|
+
target_col: str,
|
|
363
|
+
split_col: str,
|
|
364
|
+
*,
|
|
365
|
+
weight_col: str | None = None,
|
|
366
|
+
config: FeatureScreenConfig | None = None,
|
|
367
|
+
prefit_woe_engine: Any | None = None,
|
|
368
|
+
) -> FeatureScreenResult:
|
|
369
|
+
"""Convenience wrapper that resolves INS/OOS/OOT from a tagged dataframe."""
|
|
370
|
+
exclude = {target_col, split_col}
|
|
371
|
+
if weight_col:
|
|
372
|
+
exclude.add(weight_col)
|
|
373
|
+
features = [c for c in feature_cols if c not in exclude and c in data.columns]
|
|
374
|
+
splits = _resolve_splits(data, split_col)
|
|
375
|
+
if weight_col is not None and weight_col not in data.columns:
|
|
376
|
+
raise KeyError(f"Missing weight_col {weight_col!r}")
|
|
377
|
+
return feature_screen(
|
|
378
|
+
splits,
|
|
379
|
+
features,
|
|
380
|
+
target_col,
|
|
381
|
+
weight_col=weight_col,
|
|
382
|
+
config=config,
|
|
383
|
+
prefit_woe_engine=prefit_woe_engine,
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
__all__ = [
|
|
388
|
+
"FeatureScreenConfig",
|
|
389
|
+
"FeatureScreenResult",
|
|
390
|
+
"feature_screen",
|
|
391
|
+
"feature_screen_from_dataframe",
|
|
392
|
+
"fit_screening_woe_engine",
|
|
393
|
+
"screen_config_from_mapping",
|
|
394
|
+
]
|