precision_recall_curve#

sklearn.metrics.precision_recall_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=False)[source]#

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

注意:通过一对多或一对一方式,不支持超出二元分类任务的扩展。

精确度是比率 tp / (tp + fp),其中 tp 是真阳性(true positives)的数量,fp 是假阳性(false positives)的数量。精确度的直观理解是分类器不将负样本错误地标记为正样本的能力。

召回率是比率 tp / (tp + fn),其中 tp 是真阳性(true positives)的数量,fn 是假阴性(false negatives)的数量。召回率的直观理解是分类器找到所有正样本的能力。

最后两个精确度和召回率值分别为 1. 和 0.,它们没有对应的阈值。这确保了图表从 y 轴开始。

第一个精确度和召回率值分别是 precision=class balance 和 recall=1.0,这对应于一个总是预测为正类的分类器。

用户指南中阅读更多内容。

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

真实的二元标签。如果标签不是 {-1, 1} 或 {0, 1},则应明确给出 pos_label。

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

目标分数,可以是正类的概率估计,也可以是未经过阈值处理的决策度量(由某些分类器上的 decision_function 返回)。对于 decision_function 分数,大于或等于零的值应表示正类。

pos_labelint, float, bool or str, default=None

正类的标签。当 pos_label=None 时,如果 y_true 在 {-1, 1} 或 {0, 1} 中,pos_label 设置为 1,否则将引发错误。

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

样本权重。

drop_intermediate布尔值,默认为 False

是否删除一些不会出现在绘制的精确度-召回率曲线上的次优阈值。这对于创建更轻量级的精确度-召回率曲线很有用。

在版本 1.3 中新增。

返回:
precision形状为 (n_thresholds + 1,) 的 ndarray

精确度值,其中元素 i 是分数 >= thresholds[i] 的预测的精确度,最后一个元素是 1。

recall形状为 (n_thresholds + 1,) 的 ndarray

递减的召回率值,其中元素 i 是分数 >= thresholds[i] 的预测的召回率,最后一个元素是 0。

thresholds形状为 (n_thresholds,) 的 ndarray

用于计算精确度和召回率的决策函数的递增阈值,其中 n_thresholds = len(np.unique(y_score))

另请参阅

PrecisionRecallDisplay.from_estimator

给定二元分类器绘制精确度召回率曲线。

PrecisionRecallDisplay.from_predictions

使用二元分类器的预测绘制精确度召回率曲线。

average_precision_score

根据预测分数计算平均精确度。

det_curve

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

roc_curve

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

confusion_matrix_at_thresholds

对于二分类,计算每个阈值的真阴性、假阳性、假阴性和真阳性计数。

示例

>>> import numpy as np
>>> from sklearn.metrics import precision_recall_curve
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> precision, recall, thresholds = precision_recall_curve(
...     y_true, y_scores)
>>> precision
array([0.5       , 0.66666667, 0.5       , 1.        , 1.        ])
>>> recall
array([1. , 1. , 0.5, 0.5, 0. ])
>>> thresholds
array([0.1 , 0.35, 0.4 , 0.8 ])