metric_at_thresholds#

sklearn.metrics.metric_at_thresholds(y_true, y_score, metric_func, *, sample_weight=None, metric_params=None)[source]#

计算二分类数据在每个阈值下的 metric_func

用于辅助在调整决策阈值时对指标值进行可视化。

更多信息请阅读用户指南

1.9 版本中新增。

参数:
y_true形状为 (n_samples,) 的 array-like

真实(正确)的目标标签。

y_score形状为 (n_samples,) 的类数组

连续预测分数,可以是正类的估计概率,也可以是决策函数的输出。

metric_func可调用对象

要使用的指标函数。它将被调用为 metric_func(y_true, y_pred, **metric_params),其中 y_pred 是经阈值处理后的预测结果,在内部计算为 y_pred = (y_score >= threshold)。输出应为单个数值,或每个元素大小相同的集合。

sample_weightshape 为 (n_samples,) 的 array-like, default=None

样本权重。如果不是 None,将传递给 metric_func

metric_paramsdict, default=None

传递给 metric_func 的参数。

返回:
metric_values形状为 (n_thresholds,) 或 (n_thresholds, *n_outputs) 的 ndarray

与每个阈值相关联的分数。如果 metric_func 返回一个集合(例如浮点数的元组),则输出将是一个形状为 (n_thresholds, *n_outputs) 的二维数组。

thresholds形状为 (n_thresholds,) 的 ndarray

用于计算分数的阈值。

另请参阅

confusion_matrix_at_thresholds

计算每个阈值的二分类混淆矩阵。

precision_recall_curve

计算不同概率阈值的精确度-召回率对。

det_curve

计算不同概率阈值的错误率。

roc_curve

计算接收者操作特征 (ROC) 曲线。

示例

>>> import numpy as np
>>> from sklearn.metrics import accuracy_score, metric_at_thresholds
>>> y_true = np.array([0, 0, 1, 1])
>>> y_score = np.array([0.1, 0.4, 0.35, 0.8])
>>> metric_values, thresholds = metric_at_thresholds(
...     y_true, y_score, accuracy_score)
>>> thresholds
array([0.8 , 0.4 , 0.35, 0.1 ])
>>> metric_values
array([0.75, 0.5 , 0.75, 0.5 ])