precision_score#
- sklearn.metrics.precision_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn')[源代码]#
计算精确率(precision)。
精确率是
tp / (tp + fp)的比率,其中tp是真阳性(true positives)的数量,fp是假阳性(false positives)的数量。精确率直观地衡量了分类器不将负样本错误地标记为正样本的能力。最佳值为 1,最差值为 0。
对超出术语:
binary目标的支持通过将多类别(multiclass)和多标签(multilabel)数据视为二元问题集合来实现,每个标签一个二元问题。对于二元(binary)情况,设置 average='binary'将返回pos_label的精确率。如果average不是'binary',则pos_label被忽略,计算两个类别的精确率,然后取平均值或返回两个值(当average=None时)。类似地,对于多类别(multiclass)和多标签(multilabel)目标,所有labels的精确率将根据average参数返回或取平均值。使用labels指定要计算精确率的标签集合。在用户指南中阅读更多信息。
- 参数:
- y_true1d array-like,或标签指示器数组/稀疏矩阵
真实(正确)的目标值。
- y_pred1d array-like,或标签指示器数组/稀疏矩阵
分类器返回的估计目标。
- labelsarray-like, 默认=None
当
average != 'binary'时要包含的标签集合,如果average is None,则为它们的顺序。数据中存在的标签可以被排除,例如在多类别分类中排除“负类”。数据中不存在的标签可以被包含,并将“分配”0个样本。对于多标签目标,标签是列索引。默认情况下,y_true和y_pred中的所有标签都按排序顺序使用。0.17 版中的更改:
labels参数针对多类别问题进行了改进。- pos_labelint, float, bool 或 str, 默认=1
当
average='binary'且数据为二元时要报告的类别,否则此参数将被忽略。对于多类别或多标签目标,设置labels=[pos_label]且average != 'binary'以仅报告一个标签的指标。- average{'micro', 'macro', 'samples', 'weighted', 'binary'} 或 None, 默认='binary'
此参数对于多类别/多标签目标是必需的。如果为
None,则返回每个类别的指标。否则,这决定了对数据执行的平均类型。'binary':仅报告
pos_label指定的类的结果。这仅适用于目标 (y_{true,pred}) 是二元的情况。'micro':通过计算真阳性、假阴性和假阳性的总数来全局计算指标。
'macro':计算每个标签的指标,并找到它们的未加权平均值。这不考虑标签不平衡。
'weighted':计算每个标签的指标,并根据支持度(每个标签的真实实例数)加权取平均值。这改变了“macro”以考虑标签不平衡;它可能导致 F-score 不在精确率和召回率之间。
'samples':计算每个实例的指标,并找到它们的平均值(仅对多标签分类有意义,这与
accuracy_score不同)。
- sample_weight形状为 (n_samples,) 的 array-like, 默认=None
样本权重。
- zero_division{"warn", 0.0, 1.0, np.nan}, 默认="warn"
设置当出现零除时返回的值。
注意
如果设置为“warn”,则其作用类似于 0,但会引发警告。
如果设置为
np.nan,这些值将从平均值中排除。
1.3 版新增: 增加了
np.nan选项。
- 返回:
- precisionfloat (如果 average 不是 None) 或 形状为 (n_unique_labels,) 的 float 数组
二元分类中正类别的精确率,或多类别任务中每个类别精确率的加权平均值。
另请参阅
precision_recall_fscore_support计算每个类别的精确率、召回率、F-measure 和支持度。
recall_score计算
tp / (tp + fn)的比率,其中tp是真阳性的数量,fn是假阴性的数量。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. ])
图库示例#