precision_score#

sklearn.metrics.precision_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn')[source]#

计算精确度。

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

最佳值为 1,最差值为 0。

对于超出 二元 目标的支持,通过将 多类多标签 数据视为二元问题的集合(每个标签一个问题)来实现。对于 二元 情况,设置 average='binary' 将返回 pos_label 的精确度。如果 average 不是 'binary',则忽略 pos_label,并计算两个类别的精确度,然后进行平均或返回两者(当 average=None 时)。类似地,对于 多类多标签 目标,所有 labels 的精确度将根据 average 参数返回或平均。使用 labels 指定要计算精确度的标签集。

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

参数:
y_true1d array-like, or label indicator array / sparse matrix

真实(正确)目标值。稀疏矩阵仅在目标为 多标签 类型时受支持。

y_pred1d array-like, or label indicator array / sparse matrix

分类器返回的估计目标。稀疏矩阵仅在目标为 多标签 类型时受支持。

labels类似数组对象, default=None

average != 'binary' 时要包括的标签集,如果 average is None,则为它们的顺序。可以排除数据中存在的标签,例如在多类分类中排除“负类”。可以包括数据中不存在的标签,这些标签将被“分配”0个样本。对于多标签目标,标签是列索引。默认情况下,按排序顺序使用 y_truey_pred 中的所有标签。

版本 0.17 中的变化: 针对多类问题改进了参数 labels

pos_labelint, float, bool or str, default=1

如果 average='binary' 且数据为二元,则报告的类别,否则此参数将被忽略。对于多类或多标签目标,设置 labels=[pos_label]average != 'binary' 以仅报告一个标签的指标。

average{‘micro’, ‘macro’, ‘samples’, ‘weighted’, ‘binary’} or None, default=’binary’

此参数对于多类/多标签目标是必需的。如果为 None,则返回每个类别的指标。否则,这决定了对数据执行的平均类型

'binary':

仅报告由 pos_label 指定的类别的结果。这仅适用于目标 (y_{true,pred}) 是二元的情况。

'micro':

通过计算总的真正例、假负例和假正例来全局计算指标。

'macro':

计算每个标签的指标,并找到它们的未加权平均值。这不考虑标签不平衡。

'weighted':

计算每个标签的指标,并找到按支持度(每个标签的真实实例数)加权的平均值。这会修改“宏平均”以考虑标签不平衡;它可能导致 F 分数不在精确率和召回率之间。

'samples':

计算每个实例的指标,并找到它们的平均值(仅对多标签分类有意义,这与 accuracy_score 不同)。

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

样本权重。

zero_division{“warn”, 0.0, 1.0, np.nan}, default=”warn”

当出现零除法时设置要返回的值。

注意事项

  • 如果设置为“warn”,则其作用类似于 0,但也会发出警告。

  • 如果设置为 np.nan,则此类值将被排除在平均值之外。

版本 1.3 中的新增: 添加了 np.nan 选项。

返回:
precisionfloat (如果 average 不是 None) 或 float 数组,形状为 (n_unique_labels,)

二元分类中正类别的精确度,或多类任务中每个类别精确度的加权平均值。

另请参阅

precision_recall_fscore_support

计算每个类别的精确度、召回率、F-measure 和支持度。

recall_score

计算比率 tp / (tp + fn),其中 tp 是真阳性(true positives)的数量,fn 是假阴性(false negatives)的数量。

PrecisionRecallDisplay.from_estimator

给定估计器和一些数据,绘制精确度-召回率曲线。

PrecisionRecallDisplay.from_predictions

给定二元类别预测,绘制精确度-召回率曲线。

multilabel_confusion_matrix

计算每个类别或样本的混淆矩阵。

注意事项

true positive + false positive == 0 时,精确度返回 0 并引发 UndefinedMetricWarning。可以使用 zero_division 修改此行为。

示例

>>> import numpy as np
>>> from sklearn.metrics import precision_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> precision_score(y_true, y_pred, average='macro')
0.22
>>> precision_score(y_true, y_pred, average='micro')
0.33
>>> precision_score(y_true, y_pred, average='weighted')
0.22
>>> precision_score(y_true, y_pred, average=None)
array([0.66, 0.        , 0.        ])
>>> y_pred = [0, 0, 0, 0, 0, 0]
>>> precision_score(y_true, y_pred, average=None)
array([0.33, 0.        , 0.        ])
>>> precision_score(y_true, y_pred, average=None, zero_division=1)
array([0.33, 1.        , 1.        ])
>>> precision_score(y_true, y_pred, average=None, zero_division=np.nan)
array([0.33,        nan,        nan])
>>> # multilabel classification
>>> y_true = [[0, 0, 0], [1, 1, 1], [0, 1, 1]]
>>> y_pred = [[0, 0, 0], [1, 1, 1], [1, 1, 0]]
>>> precision_score(y_true, y_pred, average=None)
array([0.5, 1. , 1. ])