评估指标

hscredit.core.models 提供面向风控任务的模型评估指标。

class hscredit.core.models.BaseMetric(name='custom_metric', greater_is_better=True)[源代码]

基类:ABC

评估指标基类。

所有自定义评估指标都应该继承此类,并实现__call__方法。

参数:
  • name (str) -- 指标名称,默认为"custom_metric"

  • greater_is_better (bool) -- 是否越大越好,默认为True

to_xgboost()[源代码]

转换为XGBoost格式的评估指标。

返回:

XGBoost可用的评估指标

返回类型:

Callable

to_lightgbm()[源代码]

转换为LightGBM格式的评估指标。

返回:

LightGBM可用的评估指标

返回类型:

Callable

to_catboost()[源代码]

转换为CatBoost格式的评估指标。

返回:

CatBoost可用的评估指标

返回类型:

Callable

class hscredit.core.models.KSMetric(name='ks')[源代码]

基类:BaseMetric

KS (Kolmogorov-Smirnov) 指标.

衡量模型区分好坏客户的能力,KS值越大表示模型区分能力越强。

KS = max(|累积好客户比例 - 累积坏客户比例|)

参数:

name (str) -- 指标名称,默认为"ks"

参考样例

>>> import numpy as np
>>> from hscredit.core.models.losses import KSMetric
>>>
>>> ks_metric = KSMetric()
>>> y_true = np.array([0, 0, 1, 1, 0, 1, 0, 1])
>>> y_pred = np.array([0.1, 0.2, 0.8, 0.9, 0.3, 0.7, 0.4, 0.6])
>>> ks_value = ks_metric(y_true, y_pred)
>>> print(f"KS: {ks_value:.4f}")
>>> # 在LightGBM中使用
>>> import lightgbm as lgb
>>> train_data = lgb.Dataset(X_train, label=y_train)
>>> bst = lgb.train(
...     params={'objective': 'binary'},
...     train_set=train_data,
...     feval=ks_metric.to_lightgbm(),
...     num_boost_round=100
... )

引用

KS(Kolmogorov–Smirnov)统计量:https://en.wikipedia.org/wiki/Kolmogorov–Smirnov_test ; 其在信用评分中的应用见 Siddiqi, N. (2006). Credit Risk Scorecards. Wiley。

class hscredit.core.models.GiniMetric(name='gini')[源代码]

基类:BaseMetric

Gini系数指标.

Gini = 2 * AUC - 1

衡量模型区分能力,Gini越大越好。

参数:

name (str) -- 指标名称,默认为"gini"

参考样例

>>> from hscredit.core.models.losses import GiniMetric
>>> gini_metric = GiniMetric()
>>> gini_value = gini_metric(y_true, y_pred)
class hscredit.core.models.PSIMetric(expected=None, n_bins=10, name='psi')[源代码]

基类:BaseMetric

PSI (Population Stability Index) 指标.

衡量样本分布的稳定性,常用于模型监控。

PSI = sum((实际占比 - 期望占比) * ln(实际占比/期望占比))

参数:
  • expected (ndarray | None) -- 期望分布的预测分数(基准数据),默认为None。如果为None,需要在__call__时提供

  • n_bins (int) -- 分箱数量,默认为10

  • name (str) -- 指标名称,默认为"psi"

参考样例

>>> from hscredit.core.models.losses import PSIMetric
>>>
>>> # 使用训练集作为基准
>>> psi_metric = PSIMetric(expected=y_train_pred, n_bins=10)
>>>
>>> # 计算测试集的PSI
>>> psi_value = psi_metric(y_test, y_test_pred)
>>> print(f"PSI: {psi_value:.4f}")

注意

PSI解释: - PSI < 0.1: 分布稳定 - 0.1 <= PSI < 0.25: 分布有轻微变化 - PSI >= 0.25: 分布变化显著,需要关注