"""MDLP (Minimum Description Length Principle) 分箱.
基于信息论的递归分箱算法,由 Fayyad 和 Irani 于 1993 年提出。
使用 MDLP 准则作为停止条件,自动确定最优分箱数。
优化版本V3(针对平滑分布):
- 放宽终止条件,避免过早停止
- 支持强制最小分箱数
- 改进候选切分点选择,优先选择IV增益大的点
- 针对平滑分布数据优化
"""
import logging
from typing import Union, List, Dict, Optional, Any
import numpy as np
import pandas as pd
from scipy import special
from ...exceptions import NotFittedError
from .base import BaseBinning
logger = logging.getLogger(__name__)
def _iv_from_counts(bad, good, total_bad, total_good):
"""由充分统计量计算各箱IV;保持旧实现忽略纯好/纯坏箱的语义。"""
bad, good = np.broadcast_arrays(np.asarray(bad, dtype=float), np.asarray(good, dtype=float))
result = np.zeros(bad.shape, dtype=float)
if total_bad <= 0 or total_good <= 0:
return result
valid = (bad > 0) & (good > 0)
bad_rate = bad[valid] / total_bad
good_rate = good[valid] / total_good
result[valid] = (good_rate - bad_rate) * np.log((good_rate + 1e-10) / (bad_rate + 1e-10))
return result
def _split_scores_from_prefix(prefix_bad, positions, n):
"""全部候选只读取前缀计数,不反复切片扫描标签。"""
positions = np.asarray(positions, dtype=np.intp)
total_bad = prefix_bad[n]
total_good = n - total_bad
left_bad = prefix_bad[positions]
right_bad = total_bad - left_bad
left_good = positions - left_bad
right_good = n - positions - right_bad
valid = (left_bad > 0) & (right_bad > 0) & (left_good > 0) & (right_good > 0)
scores = _iv_from_counts(left_bad, left_good, total_bad, total_good)
scores += _iv_from_counts(right_bad, right_good, total_bad, total_good)
return np.where(valid, scores, 0.0)
[文档]
class MDLPBinning(BaseBinning):
"""MDLP 分箱算法.
基于最小描述长度原理的递归分箱方法,自动确定最优分箱数。
使用信息增益和 MDLP 准则决定是否继续分割。
优化版本V3特点(针对平滑分布):
- 放宽终止条件,支持force_min_bins强制最小分箱数
- 改进候选切分点选择,结合IV值评估
- 更好的平滑分布处理
**参数**
:param constraint_policy: 约束冲突策略,默认 ``"raise"`` 报错;``"legacy"`` 在约束不可行时
保留原生尽力满足的规则(优化器无可行分区时可回退单箱)。不放宽输入校验或吞掉回调错误。
实际未满足项记录于 ``constraint_violations_``,不增加业务统计表列。
:type constraint_policy: str
:param kwargs: 转发给 BaseBinning 的公共参数,包括 ``decimal`` 和 ``woe_clip``,
并在 sklearn clone 与再次拟合时完整保留。
:param min_bin_size: 每箱最小样本数或占比,默认为0.01;None表示不限制最小样本数
- 如果 < 1, 表示占比 (如 0.01 表示 1%)
- 如果 >= 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: 坏样本率单调性约束,默认为False
- False: 不要求单调性
- True 或 'auto': 自动检测最佳趋势(允许单增、单减、正U、倒U)
- 'auto_asc_desc': 自动检测,但只允许单增或单减(不允许U型)
- 'auto_heuristic': 使用启发式方法自动确定单调方向
- 'ascending': 强制坏样本率递增(分箱索引增大时坏样本率增大)
- 'descending': 强制坏样本率递减(分箱索引增大时坏样本率减小)
- 'peak': 倒U型/峰值(先增后降)
- 'valley': U型/谷值(先降后增)
- 'peak_heuristic': 使用启发式方法检测峰值
- 'valley_heuristic': 使用启发式方法检测谷值
:type monotonic: Union[bool, str]
: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'
:type target: str
:param max_n_bins: 最大分箱数,默认为10
:type max_n_bins: int
:param min_n_bins: 最小分箱数,默认为2
:type min_n_bins: int
:param min_samples_split: 分割内部节点所需的最小样本数,默认为2
:type min_samples_split: int
:param min_samples_leaf: 叶子节点所需的最小样本数,默认为2
:type min_samples_leaf: int
:param max_candidates: 每次评估的最大候选切分点数,默认为32
:type max_candidates: int
:param min_iv_gain: 最小IV增益阈值,默认为0.0001(降低以允许更多分箱)
:type min_iv_gain: float
:param force_min_bins: 是否强制满足最小分箱数,默认为True
:type force_min_bins: bool
:param mdlp_weight: MDLP准则权重(0-1之间),默认为0.7(降低以放宽终止条件)
:type mdlp_weight: float
:param special_codes: 特殊值列表,默认为None
:type special_codes: Optional[List]
:param missing_separate: 是否将缺失值单独分为一箱,默认为True
:type missing_separate: bool
:param random_state: 随机种子,默认为None
:type random_state: Optional[int]
:param verbose: 是否输出详细信息,默认为False
:type verbose: Union[bool, int]
**参考样例**
>>> from hscredit.core.binning import MDLPBinning
>>> binner = MDLPBinning(max_n_bins=5, min_n_bins=2)
>>> binner.fit(X_train, y_train)
>>> X_binned = binner.transform(X_test)
**引用**
Fayyad, U. M., & Irani, K. B. (1993). *Multi-interval discretization of
continuous-valued attributes for classification learning.* IJCAI-93.
https://trs.jpl.nasa.gov/handle/2014/35171 ;
MDLP(最小描述长度原理)参见 https://en.wikipedia.org/wiki/Minimum_description_length
"""
def __init__(
self,
target: str = "target",
max_n_bins: int = 10,
min_n_bins: int = 2,
min_samples_split: int = 2,
min_samples_leaf: int = 2,
max_candidates: int = 32,
min_iv_gain: float = 0.0001,
force_min_bins: bool = True,
mdlp_weight: float = 0.7,
min_bin_size: Union[float, int] = 0.01,
max_bin_size: Optional[Union[float, int]] = None,
min_bad_rate: float = 0.0,
monotonic: Union[bool, str] = False,
special_codes: Optional[List] = None,
missing_separate: bool = True,
cat_cutoff: Optional[Union[float, int]] = None,
category_order=None,
handle_unknown: Union[int, str] = -3,
random_state: Optional[int] = None,
verbose: Union[bool, int] = False,
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,
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,
special_codes=special_codes,
missing_separate=missing_separate,
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,
verbose=verbose,
n_jobs=n_jobs,
parallel_backend=parallel_backend,
parallel_config=parallel_config,
**kwargs,
constraint_policy=constraint_policy,
)
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.max_candidates = max_candidates
self.min_iv_gain = min_iv_gain
self.force_min_bins = force_min_bins
self.mdlp_weight = mdlp_weight
[文档]
def fit(self, X: Union[pd.DataFrame, np.ndarray], y: Optional[Union[pd.Series, np.ndarray]] = None, **kwargs) -> "MDLPBinning":
"""拟合 MDLP 分箱.
:param kwargs: 其他参数,传递给具体的分箱算法
:param X: 特征数据
:type X: Union[pd.DataFrame, np.ndarray]
:param y: 默认值为 ``None`` 。目标变量
:type y: Optional[Union[pd.Series, np.ndarray]]
:return: self
"""
X, y = self._check_input(X, y)
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:
"""拟合单个特征。"""
if self.verbose:
logger.info(f"处理特征: {feature}")
feature_type = self._detect_feature_type(X)
self.feature_types_[feature] = feature_type
if feature_type == "categorical":
splits = self._fit_categorical(X, y)
self.splits_[feature] = splits
self.n_bins_[feature] = len(splits) + 1
bins = self._apply_splits(X, splits, "categorical")
else:
x_numeric = pd.to_numeric(X, errors="coerce")
mask = x_numeric.notna() & ~self._special_mask(X)
x_clean = x_numeric[mask]
y_clean = y[mask]
if len(x_clean) >= self.min_samples_split:
splits = self._mdlp_split_v3(x_clean.values, y_clean.values)
self.splits_[feature] = self._round_splits(np.sort(splits))
self.n_bins_[feature] = len(splits) + 1
else:
self.splits_[feature] = np.array([])
self.n_bins_[feature] = 1
bins = self._apply_splits(X, self.splits_[feature], "numerical")
self.bin_tables_[feature] = self._compute_bin_stats(feature, X, y, bins)
def _fit_categorical(self, x: pd.Series, y: pd.Series) -> list:
"""对类别型特征进行分箱(按坏样本率排序).
MDLP 算法仅适用于数值型特征,对于类别型特征回退到按坏样本率排序的方式。
:param x: 特征数据
:param y: 目标变量
:return: 类别列表(按坏样本率排序)
"""
x_clean = x.copy()
mask = x_clean.notna()
if self.special_codes:
for code in self.special_codes:
mask = mask & (x_clean != code)
x_valid = x_clean[mask]
y_valid = y[mask]
if len(x_valid) == 0:
return []
# 计算每个类别的坏样本率并按其排序
cat_stats = pd.DataFrame({"category": x_valid, "target": y_valid}).groupby("category")["target"].agg(["mean", "count"])
cat_stats = cat_stats.sort_values("mean")
return cat_stats.index.tolist()
def _mdlp_split_v3(self, x: np.ndarray, y: np.ndarray) -> List[float]:
"""MDLP 递归分箱算法 - V3版本(针对平滑分布优化).
:param x: 特征值数组
:param y: 目标变量数组
:return: 切分点列表
"""
# 按特征值排序
idx = np.argsort(x)
x_sorted = x[idx]
y_sorted = y[idx]
splits = []
# 首先尝试找到所有候选分割点
all_candidates = self._find_all_candidates_v3(x_sorted, y_sorted)
# 使用递归分割
self._recurse_v3(x_sorted, y_sorted, splits, all_candidates, 0)
# 如果分箱数不足且force_min_bins为True,只补足到 min_n_bins
if self.force_min_bins and len(splits) < self.min_n_bins - 1:
splits = self._force_additional_splits_v3(x_sorted, y_sorted, splits, all_candidates)
return sorted(list(set(splits)))
def _find_all_candidates_v3(self, x: np.ndarray, y: np.ndarray) -> List[int]:
"""候选只取不同数值之间的边界,重复值内部不能切开。"""
return (np.flatnonzero(x[1:] > x[:-1]) + 1).tolist()
def _recurse_v3(
self,
x: np.ndarray,
y: np.ndarray,
splits: List[float],
all_candidates: List[int],
depth: int = 0,
start_idx: int = 0,
) -> None:
"""递归分割 - V3版本.
:param x: 特征值数组(已排序)
:param y: 目标变量数组
:param splits: 切分点列表(原地修改)
:param all_candidates: 全局候选切分点位置
:param depth: 当前递归深度
:param start_idx: 当前区间在全局数组中的起始索引
"""
# 检查最大分箱数限制
if len(splits) >= self.max_n_bins - 1:
return
# 获取唯一特征值数量和目标类别数
u_x = np.unique(x)
n_x = len(u_x)
n_y = len(np.unique(y))
# 基本可分割性检查
if n_x < self.min_samples_split or n_y < 2 or len(x) < self.min_samples_split:
return
# 在当前区间内筛选候选切分点
local_candidates = [i - start_idx for i in all_candidates if start_idx + self.min_samples_leaf <= i < start_idx + len(x) - self.min_samples_leaf]
local_candidates = sorted(list(set(local_candidates)))
# 限制候选切分点数量
if len(local_candidates) > self.max_candidates:
indices = np.linspace(0, len(local_candidates) - 1, self.max_candidates, dtype=int)
local_candidates = [local_candidates[i] for i in indices]
# 找到最优切分点
split, split_idx = self._find_best_split_v3(x, y, local_candidates)
if split is not None and split_idx is not None:
y_left, y_right = y[:split_idx], y[split_idx:]
# 检查终止条件(V3:放宽条件)
if not self._terminate_v3(len(x), y, y_left, y_right):
splits.append(split)
if len(splits) >= self.max_n_bins - 1:
return
# 递归处理左右子区间
x_left, x_right = x[:split_idx], x[split_idx:]
if len(x_left) >= self.min_samples_split:
self._recurse_v3(x_left, y_left, splits, all_candidates, depth + 1, start_idx)
if len(x_right) >= self.min_samples_split:
self._recurse_v3(x_right, y_right, splits, all_candidates, depth + 1, start_idx + split_idx)
def _find_best_split_v3(self, x: np.ndarray, y: np.ndarray, candidates: List[int]) -> tuple:
"""找到最优切分点 - V3版本.
结合IV增益和MDLP准则。
:param x: 特征值数组(已排序)
:param y: 目标变量数组
:param candidates: 候选切分点位置列表
:return: (最优切分点值, 切分点位置) 或 (None, None)
"""
if not candidates:
return None, None
n = len(x)
positions = np.asarray(candidates, dtype=np.intp)
positions = positions[(positions >= self.min_samples_leaf) & (n - positions >= self.min_samples_leaf)]
if not len(positions):
return None, None
prefix_bad = np.r_[0, np.cumsum(y, dtype=np.int64)]
scores = _split_scores_from_prefix(prefix_bad, positions, n)
eligible = np.flatnonzero(scores >= self.min_iv_gain)
if not len(eligible):
return None, None
# argmax返回首次最大值,保持原for循环的平局选择顺序。
best = eligible[np.argmax(scores[eligible])]
idx = int(positions[best])
return (x[idx - 1] + x[idx]) / 2, idx
def _calculate_iv_gain_v3(self, y_left: np.ndarray, y_right: np.ndarray, total_bad: int, total_good: int) -> float:
"""计算IV增益 - V3版本."""
epsilon = 1e-10
left_bad, left_n = y_left.sum(), len(y_left)
right_bad, right_n = y_right.sum(), len(y_right)
left_good = left_n - left_bad
right_good = right_n - right_bad
if left_bad == 0 or left_good == 0 or right_bad == 0 or right_good == 0:
return 0.0
left_woe = np.log((left_good / total_good + epsilon) / (left_bad / total_bad + epsilon))
right_woe = np.log((right_good / total_good + epsilon) / (right_bad / total_bad + epsilon))
left_iv = (left_good / total_good - left_bad / total_bad) * left_woe
right_iv = (right_good / total_good - right_bad / total_bad) * right_woe
return left_iv + right_iv
def _terminate_v3(self, n: int, y: np.ndarray, y_left: np.ndarray, y_right: np.ndarray) -> bool:
"""MDLP 终止条件 - V3版本(放宽条件).
:param n: 当前节点样本数
:param y: 分割前的目标变量
:param y_left: 左子区间的目标变量
:param y_right: 右子区间的目标变量
:return: 是否应该终止分割
"""
# 计算信息增益
gain = self._entropy_gain(y, y_left, y_right)
# 统计实际出现的类别,兼容浮点型 0/1 标签和仅含一个类别的子区间。
k = len(np.unique(y))
k_left = len(np.unique(y_left))
k_right = len(np.unique(y_right))
# 计算熵
ent_y = self._entropy(y)
ent_left = self._entropy(y_left)
ent_right = self._entropy(y_right)
# MDLP 准则计算
delta = np.log(3**k - 2) - (k * ent_y - k_left * ent_left - k_right * ent_right)
threshold = (np.log(n - 1) + delta) / n
# V3:使用权重调整终止条件
adjusted_threshold = threshold * self.mdlp_weight
return gain <= adjusted_threshold
def _force_additional_splits_v3(self, x: np.ndarray, y: np.ndarray, existing_splits: List[float], all_candidates: List[int]) -> List[float]:
"""强制添加额外的切分点以满足分箱数要求 - V3改进版(目标是max_n_bins)."""
splits = list(existing_splits)
n = len(x)
prefix_bad = np.r_[0, np.cumsum(y, dtype=np.int64)]
total_bad = prefix_bad[-1]
total_good = n - total_bad
min_samples = self._get_min_samples(n)
# 目标是达到max_n_bins个分箱(而不仅仅是min_n_bins)
target_n_bins = self.min_n_bins - 1
# 首先找到所有可能的候选切分点及其IV
positions = np.asarray(all_candidates, dtype=np.intp)
positions = positions[(positions >= min_samples) & (n - positions >= min_samples)]
scores = _split_scores_from_prefix(prefix_bad, positions, n)
candidate_splits = (x[positions - 1] + x[positions]) / 2
candidate_splits = candidate_splits[np.argsort(-scores, kind="stable")]
# 贪心选择:每次选择能最大化总IV的切分点
while len(splits) < target_n_bins:
best_total_iv = -np.inf
best_split = None
candidates = candidate_splits[~np.isin(candidate_splits, splits)]
# 有界候选批次:工作区随4096×箱数增长,不构造候选数×样本数的mask。
for offset in range(0, len(candidates), 4096):
chunk = candidates[offset : offset + 4096]
proposed = np.sort(np.column_stack([np.broadcast_to(splits, (len(chunk), len(splits))), chunk]), axis=1)
cuts = np.searchsorted(x, proposed, side="left")
bounds = np.column_stack([np.zeros(len(chunk), dtype=np.intp), cuts, np.full(len(chunk), n, dtype=np.intp)])
bad = np.diff(prefix_bad[bounds], axis=1)
good = np.diff(bounds, axis=1) - bad
contributions = _iv_from_counts(bad, good, total_bad, total_good)
totals = np.zeros(len(chunk))
# 按旧实现的箱顺序逐列累加,避免改变浮点平局的优先候选。
for column in range(contributions.shape[1]):
totals += contributions[:, column]
best = int(np.argmax(totals))
if totals[best] > best_total_iv:
best_total_iv = totals[best]
best_split = chunk[best]
if best_split is not None:
splits.append(best_split)
else:
# 如果没有找到好的切分点,使用等间距
if len(splits) < target_n_bins:
x_min, x_max = x.min(), x.max()
additional = np.linspace(x_min, x_max, self.max_n_bins + 1)[1:-1]
for s in additional:
if s not in splits:
splits.append(s)
if len(splits) >= target_n_bins:
break
break
return sorted(list(set(splits)))
def _entropy(self, y: np.ndarray) -> float:
"""计算熵.
:param y: 目标变量数组
:return: 熵值
"""
n = len(y)
if n == 0:
return 0.0
n_pos = np.sum(y)
n_neg = n - n_pos
if n_pos == 0 or n_neg == 0:
return 0.0
p = np.array([n_neg, n_pos]) / n
return -np.sum(special.xlogy(p, p))
def _entropy_gain(self, y: np.ndarray, y_left: np.ndarray, y_right: np.ndarray) -> float:
"""计算信息增益.
:param y: 分割前的目标变量
:param y_left: 左子区间的目标变量
:param y_right: 右子区间的目标变量
:return: 信息增益
"""
n = len(y)
n_left = len(y_left)
n_right = n - n_left
if n_left == 0 or n_right == 0:
return 0.0
ent_y = self._entropy(y)
ent_left = self._entropy(y_left)
ent_right = self._entropy(y_right)
return ent_y - (n_left * ent_left + n_right * ent_right) / n
def _get_min_samples(self, n_total: int) -> int:
"""使用统一的最小样本数取整口径。"""
return super()._get_min_samples(n_total)
def _apply_splits(self, x: pd.Series, splits, feature_type: str) -> np.ndarray:
"""应用切分点.
:param x: 特征数据
:param splits: 切分点数组(数值型为np.ndarray, 类别型为list)
:param feature_type: 特征类型
:return: 分箱标签
"""
feature = x.name
if feature in self._cat_bins_ and self.feature_types_.get(feature) == "categorical":
return self._assign_categorical_bins(feature, x)
if feature_type == "categorical":
# 基于 splits 列表中的顺序映射类别到分箱索引
x_str = x.astype(str).where(x.notna(), other=np.nan)
cat_to_bin = {str(cat): i for i, cat in enumerate(splits)}
bins = np.full(len(x), 0, dtype=int)
bins[x.isna()] = -1
for cat_str, bin_idx in cat_to_bin.items():
bins[x_str == cat_str] = bin_idx
if self.special_codes:
for code in self.special_codes:
bins[(x == code) | (x_str == str(code))] = -2
else:
if len(splits) == 0:
bins = np.zeros(len(x), dtype=int)
else:
bins = np.digitize(x.values, splits, right=False)
bins = np.where(x.isna(), -1, bins)
return bins