注意
前往末尾 下载完整示例代码,或通过 JupyterLite 或 Binder 在浏览器中运行此示例。
成本敏感学习中决策阈值的后调整#
分类器训练完成后,predict 方法的输出是根据 decision_function 或 predict_proba 输出进行阈值处理后得到的类别标签预测。对于二元分类器,默认阈值定义为后验概率估计值 0.5 或决策分数 0.0。
然而,这种默认策略很可能并非当前任务的最佳选择。在此,我们使用“Statlog”德国信用数据集 [1] 来演示一个用例。该数据集的任务是预测一个人是“好”信用还是“坏”信用。此外,还提供了一个成本矩阵,其中指定了错误分类的成本。具体来说,将“坏”信用错误分类为“好”的平均成本是将其“好”信用错误分类为“坏”的五倍。
我们使用 TunedThresholdClassifierCV
来选择决策函数的截止点,以最小化所提供的业务成本。
在示例的第二部分,我们通过考虑信用卡交易中的欺诈检测问题进一步扩展了此方法:在这种情况下,业务指标取决于每笔独立交易的金额。
参考文献
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
收益和成本恒定的成本敏感学习#
在第一节中,我们演示了在混淆矩阵的每个条目关联的收益和成本恒定的成本敏感学习设置中,如何使用 TunedThresholdClassifierCV
。我们使用 [2] 中提出的问题,并使用“Statlog”德国信用数据集 [1]。
“Statlog”德国信用数据集#
我们从 OpenML 获取德国信用数据集。
import sklearn
from sklearn.datasets import fetch_openml
sklearn.set_config(transform_output="pandas")
german_credit = fetch_openml(data_id=31, as_frame=True, parser="pandas")
X, y = german_credit.data, german_credit.target
我们检查 X
中可用的特征类型。
X.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 20 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 checking_status 1000 non-null category
1 duration 1000 non-null int64
2 credit_history 1000 non-null category
3 purpose 1000 non-null category
4 credit_amount 1000 non-null int64
5 savings_status 1000 non-null category
6 employment 1000 non-null category
7 installment_commitment 1000 non-null int64
8 personal_status 1000 non-null category
9 other_parties 1000 non-null category
10 residence_since 1000 non-null int64
11 property_magnitude 1000 non-null category
12 age 1000 non-null int64
13 other_payment_plans 1000 non-null category
14 housing 1000 non-null category
15 existing_credits 1000 non-null int64
16 job 1000 non-null category
17 num_dependents 1000 non-null int64
18 own_telephone 1000 non-null category
19 foreign_worker 1000 non-null category
dtypes: category(13), int64(7)
memory usage: 69.9 KB
许多特征是类别型的,通常是字符串编码的。在开发预测模型时,我们需要对这些类别进行编码。让我们检查目标。
y.value_counts()
class
good 700
bad 300
Name: count, dtype: int64
另一个观察是数据集不平衡。在评估我们的预测模型时,我们需要小心,并使用适用于这种设置的指标族。
此外,我们观察到目标是字符串编码的。一些指标(例如精确率和召回率)需要提供感兴趣的标签,也称为“正标签”。在这里,我们定义我们的目标是预测样本是否是“坏”信用。
pos_label, neg_label = "bad", "good"
为了进行分析,我们使用单个分层拆分来拆分数据集。
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
我们准备设计我们的预测模型和相关的评估策略。
评估指标#
在本节中,我们定义了一组稍后将使用的指标。为了查看调整截止点的影响,我们使用接收者操作特征 (ROC) 曲线和精确率-召回率曲线来评估预测模型。因此,这些图上报告的值是 ROC 曲线的真阳性率 (TPR)(也称为召回率或敏感度)和假阳性率 (FPR)(也称为特异度),以及精确率-召回率曲线的精确率和召回率。
从这四个指标来看,scikit-learn 没有提供 FPR 的评分器。因此,我们需要定义一个小的自定义函数来计算它。
from sklearn.metrics import confusion_matrix
def fpr_score(y, y_pred, neg_label, pos_label):
cm = confusion_matrix(y, y_pred, labels=[neg_label, pos_label])
tn, fp, _, _ = cm.ravel()
tnr = tn / (tn + fp)
return 1 - tnr
如前所述,“正标签”并非定义为值“1”,并且使用此非标准值调用某些指标会引发错误。我们需要向指标提供“正标签”的指示。
因此,我们需要使用 make_scorer
定义一个 scikit-learn 评分器,其中传递信息。我们将所有自定义评分器存储在一个字典中。要使用它们,我们需要传递拟合模型、数据以及我们要评估预测模型的目标。
from sklearn.metrics import make_scorer, precision_score, recall_score
tpr_score = recall_score # TPR and recall are the same metric
scoring = {
"precision": make_scorer(precision_score, pos_label=pos_label),
"recall": make_scorer(recall_score, pos_label=pos_label),
"fpr": make_scorer(fpr_score, neg_label=neg_label, pos_label=pos_label),
"tpr": make_scorer(tpr_score, pos_label=pos_label),
}
此外,原始研究 [1] 定义了一个自定义业务指标。我们称“业务指标”为任何旨在量化预测(正确或错误)如何影响在特定应用场景中部署给定机器学习模型的业务价值的指标函数。对于我们的信用预测任务,作者提供了一个自定义成本矩阵,其中编码了将“坏”信用分类为“好”的平均成本是反向操作的 5 倍:对于金融机构来说,不向一个不会违约的潜在客户(从而错过了一个本可以偿还贷款并支付利息的好客户)发放信用,比向一个将违约的客户发放信用成本更低。
我们定义一个 Python 函数,该函数对混淆矩阵进行加权并返回总成本。
import numpy as np
def credit_gain_score(y, y_pred, neg_label, pos_label):
cm = confusion_matrix(y, y_pred, labels=[neg_label, pos_label])
# The rows of the confusion matrix hold the counts of observed classes
# while the columns hold counts of predicted classes. Recall that here we
# consider "bad" as the positive class (second row and column).
# Scikit-learn model selection tools expect that we follow a convention
# that "higher" means "better", hence the following gain matrix assigns
# negative gains (costs) to the two kinds of prediction errors:
# - a gain of -1 for each false positive ("good" credit labeled as "bad"),
# - a gain of -5 for each false negative ("bad" credit labeled as "good"),
# The true positives and true negatives are assigned null gains in this
# metric.
#
# Note that theoretically, given that our model is calibrated and our data
# set representative and large enough, we do not need to tune the
# threshold, but can safely set it to the cost ration 1/5, as stated by Eq.
# (2) in Elkan paper [2]_.
gain_matrix = np.array(
[
[0, -1], # -1 gain for false positives
[-5, 0], # -5 gain for false negatives
]
)
return np.sum(cm * gain_matrix)
scoring["credit_gain"] = make_scorer(
credit_gain_score, neg_label=neg_label, pos_label=pos_label
)
香草预测模型#
我们使用 HistGradientBoostingClassifier
作为一种预测模型,它原生支持处理类别特征和缺失值。
from sklearn.ensemble import HistGradientBoostingClassifier
model = HistGradientBoostingClassifier(
categorical_features="from_dtype", random_state=0
).fit(X_train, y_train)
model
我们使用 ROC 曲线和精确率-召回率曲线来评估预测模型的性能。
import matplotlib.pyplot as plt
from sklearn.metrics import PrecisionRecallDisplay, RocCurveDisplay
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(14, 6))
PrecisionRecallDisplay.from_estimator(
model, X_test, y_test, pos_label=pos_label, ax=axs[0], name="GBDT"
)
axs[0].plot(
scoring["recall"](model, X_test, y_test),
scoring["precision"](model, X_test, y_test),
marker="o",
markersize=10,
color="tab:blue",
label="Default cut-off point at a probability of 0.5",
)
axs[0].set_title("Precision-Recall curve")
axs[0].legend()
RocCurveDisplay.from_estimator(
model,
X_test,
y_test,
pos_label=pos_label,
ax=axs[1],
name="GBDT",
plot_chance_level=True,
)
axs[1].plot(
scoring["fpr"](model, X_test, y_test),
scoring["tpr"](model, X_test, y_test),
marker="o",
markersize=10,
color="tab:blue",
label="Default cut-off point at a probability of 0.5",
)
axs[1].set_title("ROC curve")
axs[1].legend()
_ = fig.suptitle("Evaluation of the vanilla GBDT model")

我们回顾一下,这些曲线提供了预测模型在不同截止点下的统计性能洞察。对于精确率-召回率曲线,报告的指标是精确率和召回率;对于 ROC 曲线,报告的指标是 TPR(与召回率相同)和 FPR。
这里,不同的截止点对应于 0 到 1 之间不同级别的后验概率估计。默认情况下,model.predict
使用概率估计值 0.5 作为截止点。曲线上蓝色点报告了此截止点的指标:它对应于使用 model.predict
时模型的统计性能。
然而,我们回想一下,最初的目标是根据业务指标最小化成本(或最大化收益)。我们可以计算业务指标的值
print(f"Business defined metric: {scoring['credit_gain'](model, X_test, y_test)}")
Business defined metric: -232
在这个阶段,我们不知道是否有其他截止点能带来更大的收益。要找到最佳截止点,我们需要使用业务指标计算所有可能的截止点的成本-收益,并选择最佳的。这种策略手动实现起来可能相当繁琐,但 TunedThresholdClassifierCV
类可以帮助我们。它自动计算所有可能的截止点的成本-收益,并针对 scoring
进行优化。
调整截止点#
我们使用 TunedThresholdClassifierCV
来调整截止点。我们需要提供要优化的业务指标以及正标签。在内部,最佳截止点是通过交叉验证选择的,以最大化业务指标。默认使用 5 折分层交叉验证。
from sklearn.model_selection import TunedThresholdClassifierCV
tuned_model = TunedThresholdClassifierCV(
estimator=model,
scoring=scoring["credit_gain"],
store_cv_results=True, # necessary to inspect all results
)
tuned_model.fit(X_train, y_train)
print(f"{tuned_model.best_threshold_=:0.2f}")
tuned_model.best_threshold_=0.02
我们绘制了香草模型和调整后模型的 ROC 曲线和精确率-召回率曲线。我们还绘制了每个模型将使用的截止点。因为我们稍后会重用相同的代码,所以我们定义了一个生成这些图的函数。
def plot_roc_pr_curves(vanilla_model, tuned_model, *, title):
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(21, 6))
linestyles = ("dashed", "dotted")
markerstyles = ("o", ">")
colors = ("tab:blue", "tab:orange")
names = ("Vanilla GBDT", "Tuned GBDT")
for idx, (est, linestyle, marker, color, name) in enumerate(
zip((vanilla_model, tuned_model), linestyles, markerstyles, colors, names)
):
decision_threshold = getattr(est, "best_threshold_", 0.5)
PrecisionRecallDisplay.from_estimator(
est,
X_test,
y_test,
pos_label=pos_label,
linestyle=linestyle,
color=color,
ax=axs[0],
name=name,
)
axs[0].plot(
scoring["recall"](est, X_test, y_test),
scoring["precision"](est, X_test, y_test),
marker,
markersize=10,
color=color,
label=f"Cut-off point at probability of {decision_threshold:.2f}",
)
RocCurveDisplay.from_estimator(
est,
X_test,
y_test,
pos_label=pos_label,
curve_kwargs=dict(linestyle=linestyle, color=color),
ax=axs[1],
name=name,
plot_chance_level=idx == 1,
)
axs[1].plot(
scoring["fpr"](est, X_test, y_test),
scoring["tpr"](est, X_test, y_test),
marker,
markersize=10,
color=color,
label=f"Cut-off point at probability of {decision_threshold:.2f}",
)
axs[0].set_title("Precision-Recall curve")
axs[0].legend()
axs[1].set_title("ROC curve")
axs[1].legend()
axs[2].plot(
tuned_model.cv_results_["thresholds"],
tuned_model.cv_results_["scores"],
color="tab:orange",
)
axs[2].plot(
tuned_model.best_threshold_,
tuned_model.best_score_,
"o",
markersize=10,
color="tab:orange",
label="Optimal cut-off point for the business metric",
)
axs[2].legend()
axs[2].set_xlabel("Decision threshold (probability)")
axs[2].set_ylabel("Objective score (using cost-matrix)")
axs[2].set_title("Objective score as a function of the decision threshold")
fig.suptitle(title)
title = "Comparison of the cut-off point for the vanilla and tuned GBDT model"
plot_roc_pr_curves(model, tuned_model, title=title)

第一个 ملاحظ 是,两个分类器具有完全相同的 ROC 和精确率-召回率曲线。这是预料之中的,因为默认情况下,分类器是在相同的训练数据上拟合的。在后面一节中,我们将更详细地讨论有关模型重新拟合和交叉验证的可用选项。
第二个 ملاحظ 是香草模型和调整后模型的截止点不同。为了理解为什么调整后模型选择了这个截止点,我们可以查看右侧的图,它绘制了与我们的业务指标完全相同的目标分数。我们看到最佳阈值对应于目标分数的最大值。这个最大值是在远低于 0.5 的决策阈值处达到的:调整后的模型以显著降低精确率为代价,获得了更高的召回率:调整后的模型更倾向于将“坏”类别标签预测给更大比例的个体。
我们现在可以检查选择这个截止点是否能在测试集上获得更好的分数
print(f"Business defined metric: {scoring['credit_gain'](tuned_model, X_test, y_test)}")
Business defined metric: -134
我们观察到,调整决策阈值几乎将我们的业务收益提高了 2 倍。
模型重新拟合和交叉验证的考虑#
在上述实验中,我们使用了 TunedThresholdClassifierCV
的默认设置。特别是,截止点是使用 5 折分层交叉验证进行调整的。此外,一旦选择截止点,底层预测模型就会在整个训练数据上重新拟合。
这两种策略可以通过提供 refit
和 cv
参数来改变。例如,可以提供一个已拟合的 estimator
并设置 cv="prefit"
,在这种情况下,截止点是在拟合时提供的整个数据集上找到的。此外,通过设置 refit=False
,底层分类器不会被重新拟合。在这里,我们可以尝试进行这样的实验。
model.fit(X_train, y_train)
tuned_model.set_params(cv="prefit", refit=False).fit(X_train, y_train)
print(f"{tuned_model.best_threshold_=:0.2f}")
tuned_model.best_threshold_=0.28
然后,我们使用与之前相同的方法评估我们的模型
title = "Tuned GBDT model without refitting and using the entire dataset"
plot_roc_pr_curves(model, tuned_model, title=title)

我们观察到最佳截止点与之前实验中找到的不同。如果查看右侧图,我们观察到业务收益在很大范围的决策阈值内,接近最佳的 0 收益出现了一个大平台。这种行为是过拟合的症状。由于我们禁用了交叉验证,我们在训练模型的数据集上调整了截止点,这就是观察到过拟合的原因。
因此,应谨慎使用此选项。需要确保在拟合时提供给 TunedThresholdClassifierCV
的数据与用于训练底层分类器的数据不同。这有时会发生,当目的只是在全新的验证集上调整预测模型,而无需昂贵的完整重新拟合时。
当交叉验证成本过高时,一个潜在的替代方案是通过将 [0, 1]
范围内的浮点数提供给 cv
参数来使用单个训练-测试拆分。它将数据拆分为训练集和测试集。让我们探索这个选项
tuned_model.set_params(cv=0.75).fit(X_train, y_train)
title = "Tuned GBDT model without refitting and using the entire dataset"
plot_roc_pr_curves(model, tuned_model, title=title)

复制
带有均值
带有标准差
LogisticRegression
?LogisticRegression 文档
credit_card = fetch_openml(data_id=1597, as_frame=True, parser="pandas")
credit_card.frame.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 284807 entries, 0 to 284806
Data columns (total 30 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 V1 284807 non-null float64
1 V2 284807 non-null float64
2 V3 284807 non-null float64
3 V4 284807 non-null float64
4 V5 284807 non-null float64
5 V6 284807 non-null float64
6 V7 284807 non-null float64
7 V8 284807 non-null float64
8 V9 284807 non-null float64
9 V10 284807 non-null float64
10 V11 284807 non-null float64
11 V12 284807 non-null float64
12 V13 284807 non-null float64
13 V14 284807 non-null float64
14 V15 284807 non-null float64
15 V16 284807 non-null float64
16 V17 284807 non-null float64
17 V18 284807 non-null float64
18 V19 284807 non-null float64
19 V20 284807 non-null float64
20 V21 284807 non-null float64
21 V22 284807 non-null float64
22 V23 284807 non-null float64
23 V24 284807 non-null float64
24 V25 284807 non-null float64
25 V26 284807 non-null float64
26 V27 284807 non-null float64
27 V28 284807 non-null float64
28 Amount 284807 non-null float64
29 Class 284807 non-null category
dtypes: category(1), float64(29)
memory usage: 63.3 MB
惩罚项
columns_to_drop = ["Class"]
data = credit_card.frame.drop(columns=columns_to_drop)
target = credit_card.frame["Class"].astype(int)
'l2'
target.value_counts(normalize=True)
Class
0 0.998273
1 0.001727
Name: proportion, dtype: float64
对偶
target.value_counts()
Class
0 284315
1 492
Name: count, dtype: int64
C
fraud = target == 1
amount_fraud = data["Amount"][fraud]
_, ax = plt.subplots()
ax.hist(amount_fraud, bins=30)
ax.set_title("Amount of fraud transaction")
_ = ax.set_xlabel("Amount (€)")

np.float64(100.0)
拟合截距
def business_metric(y_true, y_pred, amount):
mask_true_positive = (y_true == 1) & (y_pred == 1)
mask_true_negative = (y_true == 0) & (y_pred == 0)
mask_false_positive = (y_true == 0) & (y_pred == 1)
mask_false_negative = (y_true == 1) & (y_pred == 0)
fraudulent_refuse = mask_true_positive.sum() * 50
fraudulent_accept = -amount[mask_false_negative].sum()
legitimate_refuse = mask_false_positive.sum() * -5
legitimate_accept = (amount[mask_true_negative] * 0.02).sum()
return fraudulent_refuse + fraudulent_accept + legitimate_refuse + legitimate_accept
截距缩放
sklearn.set_config(enable_metadata_routing=True)
business_scorer = make_scorer(business_metric).set_score_request(amount=True)
求解器
amount = credit_card.frame["Amount"].to_numpy()
from sklearn.model_selection import train_test_split
data_train, data_test, target_train, target_test, amount_train, amount_test = (
train_test_split(
data, target, amount, stratify=target, test_size=0.5, random_state=42
)
)
'lbfgs'
from sklearn.dummy import DummyClassifier
always_accept_policy = DummyClassifier(strategy="constant", constant=0)
always_accept_policy.fit(data_train, target_train)
benefit = business_scorer(
always_accept_policy, data_test, target_test, amount=amount_test
)
print(f"Benefit of the 'always accept' policy: {benefit:,.2f}€")
Benefit of the 'always accept' policy: 221,445.07€
多类别
always_reject_policy = DummyClassifier(strategy="constant", constant=1)
always_reject_policy.fit(data_train, target_train)
benefit = business_scorer(
always_reject_policy, data_test, target_test, amount=amount_test
)
print(f"Benefit of the 'always reject' policy: {benefit:,.2f}€")
Benefit of the 'always reject' policy: -698,490.00€
'deprecated'
L1 比例
业务指标显示,我们采用默认决策阈值的预测模型在利润方面已优于基线,并且使用它来接受或拒绝交易而非接受所有交易已经是有益的。
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
logistic_regression = make_pipeline(StandardScaler(), LogisticRegression())
param_grid = {"logisticregression__C": np.logspace(-6, 6, 13)}
model = GridSearchCV(logistic_regression, param_grid, scoring="neg_log_loss").fit(
data_train, target_train
)
model
调整决策阈值#
现在的问题是:我们的模型是否最适合我们想要做出的决策类型?到目前为止,我们还没有优化决策阈值。我们使用 TunedThresholdClassifierCV
来根据我们的业务评分器优化决策。为了避免嵌套交叉验证,我们将使用上次网格搜索中找到的最佳估计器。
tuned_model = TunedThresholdClassifierCV(
estimator=model.best_estimator_,
scoring=business_scorer,
thresholds=100,
n_jobs=2,
)
由于我们的业务评分器需要每笔交易的金额,我们需要在 fit
方法中传递此信息。TunedThresholdClassifierCV
负责将此元数据自动分派到底层评分器。
tuned_model.fit(data_train, target_train, amount=amount_train)
手动设置决策阈值而非调整它#
在前面的示例中,我们使用 TunedThresholdClassifierCV
来寻找最佳决策阈值。然而,在某些情况下,我们可能对当前问题有一些先验知识,并且可能乐于手动设置决策阈值。
FixedThresholdClassifier
类允许我们手动设置决策阈值。在预测时,它的行为与之前调整后的模型相同,但在拟合过程中不执行任何搜索。请注意,这里我们使用 FrozenEstimator
来封装预测模型,以避免任何重新拟合。
在这里,我们将重用上一节中找到的决策阈值来创建一个新模型,并检查它是否给出相同的结果。
from sklearn.frozen import FrozenEstimator
from sklearn.model_selection import FixedThresholdClassifier
model_fixed_threshold = FixedThresholdClassifier(
estimator=FrozenEstimator(model), threshold=tuned_model.best_threshold_
)
business_score = business_scorer(
model_fixed_threshold, data_test, target_test, amount=amount_test
)
print(f"Benefit of logistic regression with a tuned threshold: {business_score:,.2f}€")
Benefit of logistic regression with a tuned threshold: 249,433.39€
我们观察到我们获得了完全相同的结果,但拟合过程要快得多,因为我们没有执行任何超参数搜索。
最后,(平均) 业务指标本身的估计可能不可靠,特别是当少数类中的数据点数量非常少时。通过在历史数据上对业务指标进行交叉验证(离线评估)估计的任何业务影响,理想情况下应通过在线数据(在线评估)上的 A/B 测试来确认。然而请注意,A/B 测试模型本身超出了 scikit-learn 库的范围。
# At the end, we disable the configuration flag for metadata routing::
sklearn.set_config(enable_metadata_routing=False)
脚本总运行时间: (0 分 24.389 秒)
相关示例