maxframe.learn.metrics.precision_score#

maxframe.learn.metrics.precision_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn', execute=False, session=None, run_kwargs=None)[源代码]#

计算精确率

精确率是 tp / (tp + fp) 的比率,其中 tp 是真正例的数量,fp 是假正例的数量。直观地讲,精确率是分类器不将负样本标记为正样本的能力。

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

更多内容请参见 用户指南

参数:
  • y_true (1d array-like, or label indicator array / sparse matrix) -- 真实(正确)的目标值。

  • y_pred (1d array-like, or label indicator array / sparse matrix) -- 由分类器返回的预测目标值。

  • labels (list, optional) -- 当 average != 'binary' 时要包含的标签集合,以及当 average is None 时这些标签的顺序。数据中存在的标签可以被排除,例如计算多类平均值时忽略多数负类;而数据中不存在的标签在宏平均中会导致 0 分量。对于多标签目标,标签是列索引。默认情况下,y_truey_pred 中的所有标签都按排序顺序使用。

  • pos_label (str or int, 1 by default) -- 如果 average='binary' 且数据是二分类的,则报告此类别。如果数据是多类或多标签的,则此参数将被忽略;设置 labels=[pos_label]average != 'binary' 将仅报告该标签的得分。

  • average (string, [None, 'binary' (default), 'micro', 'macro', 'samples', 'weighted']) -- 对于多类/多标签目标,此参数是必需的。如果为 None,则返回每个类别的得分。否则,此参数决定对数据执行的平均类型:'binary':仅报告由 pos_label 指定的类别的结果。这仅适用于目标(y_{true,pred})为二分类的情况。'micro':通过计算总的真正例、假负例和假正例来全局计算指标。'macro':为每个标签计算指标,并计算它们的未加权平均值。这不考虑标签不平衡。'weighted':为每个标签计算指标,并根据支持度(每个标签的真实实例数)计算它们的加权平均值。这会调整 'macro' 以考虑标签不平衡;可能导致 F-score 不在精确率和召回率之间。'samples':为每个实例计算指标,并计算它们的平均值(仅对与 accuracy_score() 不同的多标签分类有意义)。

  • sample_weight (array-like of shape (n_samples,), default=None) -- 样本权重。

  • zero_division ("warn", 0 or 1, default="warn") -- 设置当出现零除时返回的值。如果设置为 "warn",则其作用为 0,但也会引发警告。

返回:

precision -- 二分类中正类的精确率,或多类任务中各类别精确率的加权平均值。

返回类型:

float (if average is not None) or array of float, shape = [n_unique_labels]

示例

>>> from maxframe.learn.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.        ])

备注

true positive + false positive == 0 时,precision 返回 0 并引发 UndefinedMetricWarning。此行为可以通过 zero_division 进行修改。