hscredit.core.binning.cp_sat_binning 源代码

"""CP-SAT 运筹规划分箱算法.

基于 Google OR-Tools CP-SAT 求解器的最优化分箱方法。
将分箱问题建模为约束规划问题,求解全局最优解。

算法流程:
1. 问题建模:将分箱定义为整数规划问题
2. 决策变量:每个候选分割点是否被选中
3. 约束条件:分箱数限制、单调性、样本数约束
4. 目标函数:最大化 IV、KS 或自定义指标
5. 求解:使用 CP-SAT 求解器找到全局最优解

原生求解器可通过 pip install ortools 安装。
未安装时,约束求解使用动态规划或有时限的候选枚举。
"""

from __future__ import annotations

from typing import Callable, Union, List, Dict, Optional, Any, Tuple
import numpy as np
import pandas as pd
import warnings

from importlib.util import find_spec
from ..._lazy import LazyModule

cp_model = LazyModule("ortools.sat.python.cp_model")
ORTOOLS_AVAILABLE = find_spec("ortools") is not None

from ...exceptions import NotFittedError
from .base import BaseBinning
from ._objectives import resolve_objective, make_objective_scorer
from ._candidate_search import search_candidate_splits
from ._partition_solver import solve_partition
from ...utils.parallel import resolve_native_workers


[文档] class CPSATBinning(BaseBinning): """CP-SAT 运筹规划分箱. 基于 Google OR-Tools CP-SAT 求解器的最优化分箱方法。 将分箱问题建模为约束规划问题,通过 CP-SAT 求解器找到全局最优分箱方案。 **参数** :param custom_objective: 兼容回调入口,仅与 objective='custom' 配合;也可直接传 objective=函数。 :type custom_objective: Optional[Callable] :param objective_params: 预定义目标的权重,或传给自定义目标函数的附加关键字参数,默认 None。 :type objective_params: Optional[Dict] ``objective`` 可为原生目标名称、``CustomObjectives.names()`` 中的复合目标名称, 或 ``callable(bin_stats, total_good, total_bad, **objective_params)``。回调须返回有限实数,越大越好。 回调统计复用 ``compute_bin_stats``,``iv`` 为分档 IV,``woe`` 为 log(坏分布/好分布)。 原 ``objective="custom", custom_objective=函数`` 写法继续可用。 :param constraint_policy: 约束冲突策略,默认 ``"raise"`` 报错;``"legacy"`` 在约束不可行时 保留原生尽力满足的规则(优化器无可行分区时可回退单箱)。不放宽输入校验或吞掉回调错误。 实际未满足项记录于 ``constraint_violations_``,不增加业务统计表列。 :type constraint_policy: str :param kwargs: 传递给 BaseBinning 的附加构造参数,例如 ``decimal``、``woe_clip``; 与显式参数重名或不存在的参数会被构造函数拒绝。 :param cat_cutoff: 数值字段转为类别字段的识别阈值,默认 ``None`` (数值字段保持数值型)。 ``0 < cat_cutoff < 1`` 时,非缺失唯一值数 / 非缺失样本数不超过该比例即按类别处理; ``cat_cutoff >= 1`` 时,唯一值数不超过 ``int(cat_cutoff)`` 即按类别处理。 字符串、category 和布尔字段本身按类别处理;该参数不用于合并低频类别。 :type cat_cutoff: Optional[Union[float, int]] :param category_order: 类别顺序,默认 ``None`` 按训练集坏样本率升序排列,同率按首次出现顺序。 可传 ``{字段名: 完整类别序列}`` 或 ``callable(feature, x, y)`` ; 顺序必须覆盖该字段全部非缺失训练类别,不能重复或包含未知类别。 :param handle_unknown: 预测期未知类别策略,默认 ``-3`` ; ``"value"`` 等价于 ``-3`` , 返回未知箱(标签 ``"unknown"`` ,WOE 为 ``0.0`` ); ``"raise"`` 抛出错误。 也可传入已记录的整数箱号,将未知类别归入该箱;不接受布尔值。 :type handle_unknown: Union[int, str] :param n_jobs: 并行总预算,默认 ``-1`` 自动按物理核与工作量调度; ``1`` 或 ``None`` 为串行, 正整数为 worker 上限, ``0 < n_jobs < 1`` 为物理核比例(向上取整)。 不接受布尔值、 ``0`` 或小于 ``-1`` 的值;嵌套任务共享总预算。 :type n_jobs: Union[int, float] :param parallel_backend: joblib 后端名称,默认 ``None`` 由模块选择;常用 ``"threading"`` 、 ``"loky"`` 、 ``"multiprocessing"`` ,也可使用已注册后端。 显式后端必须与 ``parallel_config`` 中的共享内存要求兼容。 :type parallel_backend: Optional[str] :param parallel_config: 默认值为 ``None`` 。并行配置字典或 ``None`` 。支持 ``adaptive`` 、 ``prefer`` 、 ``require`` 、 ``batch_size`` 、 ``pre_dispatch`` 、 ``max_nbytes`` 、 ``mmap_mode`` 、 ``temp_folder`` 、 ``timeout`` 、 ``verbose`` 、 ``inner_max_num_threads`` 、 ``backend_kwargs`` ;并行数和后端应分别通过对应公共参数传入。 :type parallel_config: Optional[Dict[str, Any]] :param user_splits: 自定义规则字典或返回规则的可调用对象,默认 ``None`` 。数值规则为严格递增的 有限内部切点(不含正负无穷);类别规则为完整覆盖训练类别的嵌套列表。 缺失标记最多出现一次,可独立成箱或加入指定箱。 :type user_splits: Optional[Dict[str, List]] :param user_splits_fixed: 固定用户节点,默认 ``None`` 表示不固定;可传全局布尔值或 ``{字段名: 布尔值或布尔序列}`` 。数值序列长度须等于有限切点数; 类别序列长度须等于用户分组数。 ``True`` 保留节点, ``False`` 允许合并。 :type user_splits_fixed: Optional[Union[bool, Dict[str, Union[bool, List[bool]]]]] :param target: 目标变量列名,默认为'target'。在scorecardpipeline风格中使用, 当fit时只传入df且y为None时,从df中提取该列作为目标变量。 :type target: str :param max_n_bins: 最大分箱数,默认为5 :type max_n_bins: int :param min_n_bins: 最小分箱数,默认为2 :type min_n_bins: int :param min_bin_size: 每箱最小样本数或占比,默认为0.02 - 如果 < 1, 表示占比 (如 0.02 表示 2%) - 如果 >= 1, 表示绝对数量 (如 100 表示最少100个样本) :type min_bin_size: Union[float, int] :param max_bin_size: 每箱最大样本数或占比,默认为None :type max_bin_size: Optional[Union[float, int]] :param min_bad_rate: 每箱最小坏样本率,默认为0.0 :type min_bad_rate: float :param monotonic: 坏样本率单调性约束,默认为'auto' - False: 不要求单调性 - True 或 'auto': 自动检测并应用最佳单调方向 - 'ascending': 强制坏样本率递增 - 'descending': 强制坏样本率递减 :type monotonic: Union[bool, str] :param objective: 优化目标,默认为'iv' - 'iv': 最大化 IV 值(Information Value) - 'ks': 最大化 KS 统计量 - 'gini': 最大化箱纯度评分 ``1 - sum(箱占比 * 坏率 * 好率)``,不等同于排序 Gini :type objective: Union[str, Callable] :param n_prebins: 预分箱数量(候选分割点数),默认为50 - 候选点越多,求解越精确,但计算时间越长 :type n_prebins: int :param max_candidates: 最大候选分割点数,默认为100 - 如果唯一值超过此数,将使用分位数采样 :type max_candidates: int :param time_limit: 求解时间限制(秒),默认为30 - 超过此时间将返回当前找到的最优解 :type time_limit: int :param num_workers: 原生求解器线程数;默认 None,继承统一 n_jobs 预算 - 可以设置为大于1以加速求解 :type num_workers: Optional[int] :param missing_separate: 缺失值是否单独分箱,默认为True :type missing_separate: bool :param special_codes: 特殊值列表,默认为None :type special_codes: Optional[List] :param random_state: 随机种子,默认为None :type random_state: Optional[int] **参考样例** sklearn风格 (推荐): >>> from hscredit.core.binning import CPSATBinning >>> # 最大化 IV >>> binner = CPSATBinning(max_n_bins=5, objective='iv', monotonic='auto') >>> binner.fit(X_train, y_train) >>> X_binned = binner.transform(X_test) >>> bin_table = binner.get_bin_table('feature_name') scorecardpipeline风格 (目标列在DataFrame中): >>> from hscredit.core.binning import CPSATBinning >>> binner = CPSATBinning(target='target', max_n_bins=5, objective='ks', time_limit=60) >>> binner.fit(df) >>> X_binned = binner.transform(df.drop(columns=['target'])) **注意** CP-SAT 分箱的特点: 1. 能够找到全局最优解(而非贪心算法的局部最优) 2. 支持复杂的约束条件组合 3. 计算时间可控,可设置时间限制 4. 适合对分箱质量要求高的场景 5. 优先使用 ``ortools``;未安装时使用相同候选空间上的动态规划或有时限枚举, 通过 ``solver_info_`` 查看实际后端和求解状态。 **引用** 将最优分箱建模为数学规划问题参考 optbinning:Navas-Palencia, G. (2020). *Optimal binning: mathematical programming formulation.* arXiv:2001.08025. https://arxiv.org/abs/2001.08025 ;求解器为 Google OR-Tools CP-SAT https://developers.google.com/optimization/cp/cp_solver """ def __init__( self, target: str = "target", max_n_bins: int = 5, min_n_bins: int = 2, min_bin_size: Union[float, int] = 0.02, max_bin_size: Optional[Union[float, int]] = None, min_bad_rate: float = 0.0, monotonic: Union[bool, str] = "auto", objective: Union[str, Callable] = "iv", n_prebins: int = 50, max_candidates: int = 100, time_limit: int = 30, num_workers: Optional[int] = None, missing_separate: bool = True, special_codes: Optional[List] = None, cat_cutoff: Optional[Union[float, int]] = None, category_order=None, handle_unknown: Union[int, str] = -3, random_state: Optional[int] = None, n_jobs: Union[int, float] = -1, parallel_backend: Optional[str] = None, parallel_config: Optional[Dict[str, Any]] = None, user_splits: Optional[Dict[str, List]] = None, user_splits_fixed: Optional[Union[bool, Dict[str, Union[bool, List[bool]]]]] = None, objective_params: Optional[Dict[str, Any]] = None, custom_objective: Optional[Callable] = None, constraint_policy: str = "raise", **kwargs, ): super().__init__( target=target, max_n_bins=max_n_bins, min_n_bins=min_n_bins, min_bin_size=min_bin_size, max_bin_size=max_bin_size, min_bad_rate=min_bad_rate, monotonic=monotonic, missing_separate=missing_separate, special_codes=special_codes, cat_cutoff=cat_cutoff, user_splits=user_splits, user_splits_fixed=user_splits_fixed, category_order=category_order, handle_unknown=handle_unknown, random_state=random_state, n_jobs=n_jobs, parallel_backend=parallel_backend, parallel_config=parallel_config, **kwargs, constraint_policy=constraint_policy, ) self.objective = objective self.objective_params = objective_params self.custom_objective = custom_objective self._objective_name, self._objective_callback = resolve_objective(objective, objective_params, custom_objective, native_names=("iv", "ks", "gini")) self.n_prebins = n_prebins self.max_candidates = max_candidates self.time_limit = time_limit self.num_workers = num_workers
[文档] def fit(self, X: Union[pd.DataFrame, np.ndarray], y: Optional[Union[pd.Series, np.ndarray]] = None, **kwargs) -> "CPSATBinning": """拟合 CP-SAT 运筹规划分箱. :param kwargs: 其他参数,传递给具体的分箱算法 :param X: 训练数据 :type X: Union[pd.DataFrame, np.ndarray] :param y: 默认值为 ``None`` 。目标变量(可选) :type y: Optional[Union[pd.Series, np.ndarray]] :return: 拟合后的分箱器 """ X, y = self._check_input(X, y) self._n_total_samples = len(X) self._fit_features(X, y, "_fit_feature") self._apply_post_fit_constraints(X, y, enforce_monotonic=True) self._finalize_categorical_fit() self._finalize_reserved_bins(X, y) self._is_fitted = True return self
def _fit_feature(self, feature: str, X: pd.Series, y: pd.Series) -> None: """对单个特征进行分箱.""" feature_type = self._detect_feature_type(X) self.feature_types_[feature] = feature_type missing_mask = X.isna() special_mask = pd.Series(False, index=X.index) if self.special_codes: special_mask = X.isin(self.special_codes) valid_mask = ~(missing_mask | special_mask) X_valid = X[valid_mask] y_valid = y[valid_mask] if feature_type == "categorical": splits = self._categorical_binning(X_valid, y_valid) self.splits_[feature] = np.array(splits) if splits else np.array([]) self.n_bins_[feature] = len(splits) + 1 if splits else len(X_valid.unique()) else: splits = self._cp_sat_numerical(X_valid, y_valid) self.splits_[feature] = self._round_splits(splits) self.n_bins_[feature] = len(splits) + 1 bins = self._assign_bins(X, feature) bin_table = self._compute_bin_stats(feature, X, y, bins) self.bin_tables_[feature] = bin_table def _get_candidate_splits(self, X: pd.Series, y: pd.Series) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: """获取候选分割点和预计算的统计数据. :return: (candidates, positions, prefix_stats) - candidates: 候选分割点数组 - positions: 包含边界的位置数组,长度为 n_candidates + 2 positions[0] = 0, positions[1...n_candidates] = 分割点位置, positions[-1] = n_samples - prefix_stats: 前缀和统计 (prefix_bad, prefix_good) """ x_vals = X.values y_vals = y.values n_samples = len(x_vals) # 排序 sorted_indices = np.argsort(x_vals) x_sorted = x_vals[sorted_indices] y_sorted = y_vals[sorted_indices] # 前缀和预计算 is_bad = (y_sorted == 1).astype(np.int64) prefix_bad = np.empty(n_samples + 1, dtype=np.int64) prefix_bad[0] = 0 np.cumsum(is_bad, out=prefix_bad[1:]) prefix_good = np.arange(n_samples + 1, dtype=np.int64) - prefix_bad # 获取候选分割点 unique_values = np.unique(x_sorted) if len(unique_values) <= 1: return np.array([]), np.array([]), (prefix_bad, prefix_good) # 使用分位数生成候选点 preset = getattr(self, "_candidate_splits_", {}).get(X.name) if preset is not None: candidates = np.asarray(preset, dtype=float) elif len(unique_values) > min(self.max_candidates, self.n_prebins): quantiles = np.linspace(0, 1, min(self.n_prebins, self.max_candidates + 1) + 1) candidates = np.percentile(x_sorted, quantiles[1:-1] * 100) else: # 使用相邻唯一值的中点 candidates = unique_values[:-1] / 2.0 + unique_values[1:] / 2.0 # 去重并过滤边界 x_min, x_max = np.min(x_sorted), np.max(x_sorted) candidates = np.unique(candidates) candidates = candidates[(candidates > x_min) & (candidates <= x_max)] # 位置映射 - 构建包含边界的位置数组 inner_positions = np.searchsorted(x_sorted, candidates, side="left") positions = np.concatenate([[0], inner_positions, [n_samples]]) return candidates, positions, (prefix_bad, prefix_good) def _cp_sat_numerical(self, X: pd.Series, y: pd.Series) -> List[float]: """使用 CP-SAT 求解器对数值型变量进行最优化分箱. :param X: 特征数据 :param y: 目标变量 :return: 最优分割点列表 """ x_vals = X.values y_vals = y.values n_samples = len(x_vals) if n_samples == 0: return [] total_good = int(np.sum(y_vals == 0)) total_bad = int(np.sum(y_vals == 1)) if (total_good == 0 or total_bad == 0) and self._objective_callback is None: return [] # 获取候选点和前缀和 candidates, positions, (prefix_bad, prefix_good) = self._get_candidate_splits(X, y) n_candidates = len(candidates) if n_candidates == 0 and self._objective_callback is None: return [] n_total = getattr(self, "_n_total_samples", n_samples) min_samples = self._get_min_samples(n_total) if self._objective_callback is not None: # 任意 Python 回调不能直接写入 CP 整数目标,使用同一候选空间的有时限枚举。 from time import perf_counter started = perf_counter() callback = make_objective_scorer(self._objective_callback, x_vals, y_vals, woe_clip=self.woe_clip) with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always", RuntimeWarning) result = search_candidate_splits( x_vals, y_vals, candidates, objective="custom", score_callback=callback, min_n_bins=min(self.min_n_bins, X.nunique()), max_n_bins=self.max_n_bins, min_samples=min_samples, max_samples=self._get_max_samples(n_total), min_bad_rate=self.min_bad_rate, monotonic=self.monotonic, time_limit=self.time_limit, ) limited = any("time_limit" in str(item.message) for item in caught) for item in caught: warnings.warn(str(item.message), item.category, stacklevel=2) self.solver_info_ = getattr(self, "solver_info_", {}) self.solver_info_[X.name] = { "后端": "enumeration", "求解状态": "FEASIBLE" if limited else "OPTIMAL", "目标函数": self._objective_name, "目标值": callback(result), "耗时秒": perf_counter() - started, } return result # 所有目标都从任意候选边界组合中求解;IV 使用动态规划,KS/Gini 与 # 单调约束使用有时限的确定性候选搜索。 info = {} result = solve_partition( x_vals, y_vals, candidates, objective=self._objective_name, min_n_bins=self.min_n_bins, max_n_bins=self.max_n_bins, min_samples=min_samples, max_samples=self._get_max_samples(n_total), monotonic=self.monotonic, time_limit=self.time_limit, min_bad_rate=self.min_bad_rate, num_workers=resolve_native_workers(self.n_jobs, native_workers=self.num_workers), random_state=self.random_state, info=info, ) self.solver_info_ = getattr(self, "solver_info_", {}) self.solver_info_[X.name] = info return result def _add_iv_objective_cp_sat( self, model: cp_model.CpModel, x: List[cp_model.IntVar], candidates: np.ndarray, positions: np.ndarray, prefix_bad: np.ndarray, prefix_good: np.ndarray, total_good: int, total_bad: int, ) -> None: """添加 IV 最大化目标函数到 CP-SAT 模型. IV = Σ (bad_dist - good_dist) * log(bad_dist / good_dist) 其中 bad_dist = bad / total_bad, good_dist = good / total_good positions 数组结构: [0, pos1, pos2, ..., posN, n_samples] 长度 = n_candidates + 2 段 i 覆盖 positions[i] 到 positions[i+1] """ n_candidates = len(candidates) n_segments = n_candidates + 1 # 段数 eps = 1e-10 # 缩放因子:将浮点数 IV 转换为整数 scale = 1e6 # 预计算每个段的 IV 贡献 seg_iv = {} for i in range(n_segments): start = int(positions[i]) end = int(positions[i + 1]) bad = int(prefix_bad[end] - prefix_bad[start]) good = int(prefix_good[end] - prefix_good[start]) count = bad + good if count > 0: bad_dist = bad / total_bad if total_bad > 0 else 0 good_dist = good / total_good if total_good > 0 else 0 if bad_dist > eps and good_dist > eps: iv = (bad_dist - good_dist) * np.log(bad_dist / good_dist) seg_iv[i] = int(iv * scale) # 创建目标变量 max_iv = sum(seg_iv.values()) if seg_iv else int(1e9) objective_var = model.NewIntVar(-max_iv, max_iv, "iv_objective") # 构建目标函数: objective_var = Σ selected_segments IV # 段 0 和段 n_segments-1 始终被选中(首尾箱) # 中间段: 如果前一个分割点被选中,则该段被选中 iv_terms = [] # 第一个段:始终被选中 first_iv = seg_iv.get(0, 0) iv_terms.append(first_iv) # 中间段:如果 x[i-1] = 1 则该段被选中 for i in range(1, n_segments - 1): seg_iv_val = seg_iv.get(i, 0) if i - 1 < n_candidates: term = model.NewIntVar(-int(1e6), int(1e6), f"iv_term_{i}") model.Add(term == seg_iv_val).OnlyEnforceIf(x[i - 1]) model.Add(term == 0).OnlyEnforceIf(x[i - 1].Not()) iv_terms.append(term) else: iv_terms.append(seg_iv_val) # 最后一个段:始终被选中 last_iv = seg_iv.get(n_segments - 1, 0) iv_terms.append(last_iv) model.Add(objective_var == sum(iv_terms)) model.Maximize(objective_var) def _resolve_monotonic_direction(self, X: pd.Series, y: pd.Series) -> Optional[str]: """解析单调性方向. :return: 'ascending', 'descending', 或 None """ mono = self.monotonic if mono in (False, None, "none"): return None if mono in (True, "auto", "auto_asc_desc", "auto_heuristic"): # 自动检测:计算坏样本率与特征值的相关性 x_vals = X.values y_vals = y.values # 简单相关性检测 corr = np.corrcoef(x_vals, y_vals)[0, 1] if np.isnan(corr): return None return "descending" if corr > 0 else "ascending" if mono == "ascending": return "ascending" if mono == "descending": return "descending" return None def _heuristic_fallback( self, candidates: np.ndarray, positions: np.ndarray, prefix_bad: np.ndarray, prefix_good: np.ndarray, total_good: int, total_bad: int, min_samples: int, ) -> List[float]: """当 CP-SAT 求解失败时使用的启发式备选方案. 使用贪心 + 局部搜索策略。 """ if len(candidates) == 0: return [] n_candidates = len(candidates) n_splits_needed = min(self.max_n_bins - 1, n_candidates) # 贪心选择 selected: List[int] = [] remaining = list(range(n_candidates)) for _ in range(n_splits_needed): best_idx = -1 best_score = -np.inf for i in remaining: test_splits = sorted(selected + [i]) score = self._calc_iv_fast(positions, prefix_bad, prefix_good, total_good, total_bad, test_splits) if score > best_score: best_score = score best_idx = i if best_idx >= 0: selected.append(best_idx) remaining.remove(best_idx) else: break # 局部搜索优化 selected = self._local_search(selected, candidates, positions, prefix_bad, prefix_good, total_good, total_bad, min_samples) return [candidates[i] for i in selected] def _calc_iv_fast( self, positions: np.ndarray, prefix_bad: np.ndarray, prefix_good: np.ndarray, total_good: int, total_bad: int, split_indices: List[int], ) -> float: """快速计算 IV 值.""" if not split_indices: return 0.0 eps = 1e-10 n_pos = len(prefix_bad) - 1 boundaries = [0] + [idx + 1 for idx in split_indices] + [n_pos] iv = 0.0 for i in range(len(boundaries) - 1): s, e = boundaries[i], boundaries[i + 1] bad = int(prefix_bad[e] - prefix_bad[s]) good = int(prefix_good[e] - prefix_good[s]) bad_dist = bad / total_bad if total_bad > 0 else 0 good_dist = good / total_good if total_good > 0 else 0 if bad_dist > eps and good_dist > eps: iv += (bad_dist - good_dist) * np.log(bad_dist / good_dist) return iv def _local_search( self, selected: List[int], candidates: np.ndarray, positions: np.ndarray, prefix_bad: np.ndarray, prefix_good: np.ndarray, total_good: int, total_bad: int, min_samples: int, ) -> List[int]: """局部搜索优化.""" current = sorted(selected) C = len(candidates) for _ in range(5): improved = False best = current[:] best_score = self._calc_iv_fast(positions, prefix_bad, prefix_good, total_good, total_bad, current) # 尝试替换 for i in range(len(current)): for j in range(C): if j in current: continue trial = current[:] trial[i] = j trial = sorted(set(trial)) score = self._calc_iv_fast(positions, prefix_bad, prefix_good, total_good, total_bad, trial) if score > best_score + 1e-10: best_score = score best = trial improved = True # 尝试添加 if len(current) < self.max_n_bins - 1: for j in range(C): if j in current: continue trial = sorted(current + [j]) score = self._calc_iv_fast(positions, prefix_bad, prefix_good, total_good, total_bad, trial) if score > best_score + 1e-10: best_score = score best = trial improved = True current = best if not improved: break return current def _categorical_binning(self, X: pd.Series, y: pd.Series) -> List[float]: """对类别型变量进行分箱.""" total_good = (y == 0).sum() total_bad = (y == 1).sum() if total_good == 0 or total_bad == 0: return [] df = pd.DataFrame({"X": X, "y": y}) category_stats = df.groupby("X")["y"].agg(["sum", "count"]).reset_index() category_stats.columns = ["category", "bad_count", "count"] category_stats["good_count"] = category_stats["count"] - category_stats["bad_count"] category_stats["bad_rate"] = category_stats["bad_count"] / category_stats["count"] category_stats = category_stats.sort_values("bad_rate") min_samples = self._get_min_samples(len(X)) category_stats = category_stats[category_stats["count"] >= min_samples] if len(category_stats) <= self.max_n_bins: return [] n_categories = len(category_stats) return [i - 0.5 for i in range(1, min(n_categories, self.max_n_bins))] def _assign_bins(self, X: pd.Series, feature: str) -> np.ndarray: """为数据分配分箱索引.""" x_vals = X.values if self.feature_types_[feature] == "categorical" and feature in self._cat_bins_: return self._assign_categorical_bins(feature, X) if self.feature_types_[feature] == "categorical": codes = pd.Categorical(X).codes return np.where(X.isna(), -1, codes) else: splits = self.splits_[feature] n = len(x_vals) bins = np.zeros(n, dtype=int) missing_mask = X.isna() bins[missing_mask] = -1 if self.special_codes: for code in self.special_codes: bins[x_vals == code] = -2 valid_mask = ~missing_mask if self.special_codes: for code in self.special_codes: valid_mask = valid_mask & (x_vals != code) if valid_mask.any() and len(splits) > 0: bins[valid_mask] = np.searchsorted(splits, x_vals[valid_mask], side="right") return bins
[文档] def transform(self, X: Union[pd.DataFrame, np.ndarray], metric: str = "indices", **kwargs) -> Union[pd.DataFrame, np.ndarray]: """应用分箱转换. **参数** :param X: 待转换的数据,shape (n_samples, n_features) - 支持DataFrame或numpy数组 - 列名必须与fit时的特征名一致 :param metric: 转换类型,默认为'indices' - 'indices': 返回分箱索引 (0, 1, 2, ...) * 用途: 后续处理、特征工程 * 示例: [0, 1, 2, 0, 1, ...] - 'bins': 返回分箱标签字符串 * 用途: 可视化、报告展示 * 示例: ['(-inf, 25]', '(25, 35]', '(35, 45]', ...] * 类别型: ['北京,上海', '广州,深圳', ...] - 'woe': 返回WOE值 * 用途: 逻辑回归建模 * 示例: [0.234, -0.456, 0.123, ...] :param kwargs: 其他参数 """ if not self._is_fitted: raise NotFittedError("分箱器尚未拟合,请先调用fit方法") if not isinstance(X, pd.DataFrame): if isinstance(X, np.ndarray): X = pd.DataFrame(X) else: X = pd.DataFrame(X) return self._transform_binning_features( X, metric, lambda feature: self._assign_bins(X[feature], feature), woe_default=0.0, )