【Python学习】 - sklearn学习 - 评估指标precision_score的参数说明

函数声明:

precision_score(y_true, y_pred, labels=None, pos_label=1, average='binary', sample_weight=None)

其中较为常用的参数解释如下:

y_true:真实标签

y_pred:预测标签

average:评价值的平均值的计算方式。可以接收[None, 'binary' (default), 'micro', 'macro', 'samples', 'weighted']对于多类/多标签目标需要此参数。下面进行详细说明:

如果是None,则返回每个类的分数。否则,这决定了对数据进行平均的类型用下面几种统计方法的哪一种:

 

先说对于'micro' :

这两种写法是等价的

print(precision_score(y_test, y_pred,average='micro'))
print(np.sum(y_test == y_pred) / len(y_test))

 

以下内容中,P表示二分类时精确率的计算结果  部分内容参考

' macro '  : 相当于类间不带权重。不考虑类别数量,不适用于类别不均衡的数据集,其计算方式为: 各类别的P求和/类别数量

' weighted ' : 相当于类间带权重。各类别的P × 该类别的样本数量(实际值而非预测值)/ 样本总数量

举个例子:

如实际样本中,0类有98个样本,1类有2个样本,3类有100个样本,共有3类,样本总数为20。

预测结果中,0类全部预测为3类,全部错误;1类全部预测正确;3类全部预测为0类,全部预测错误。

则P_macro = 0 + 1 + 0 / 3 = 0.33333333

P_weighted = 0×98 + 1×2 + 0×100 / 200 = 2/200 = 0.01

 

再举个例子:

如共有100个样本,0类98个,1类2个;

预测结果为全0

则P_macro = 0.98 + 0 / 2 = 0.49

P_weighted = 98×0.98 + 0×2 / 100 = 2/200 = 0.9604

得出结论:

对于类别不均衡的分类模型,采用macro方式会有较大的偏差,采用weighted方式则可较好反映模型的优劣,因为若类别数量较小则存在蒙对或蒙错的概率,其结果不能真实反映模型优劣,需要较大的样本数量才可计算较为准确的评价值,通过将样本数量作为权重,可理解为评价值的置信度,数量越多,其评价值越可信。
 

官方说明:
    
average : string, [None, 'binary' (default), 'micro', 'macro', 'samples', \
                       'weighted']
        This parameter is required for multiclass/multilabel targets.
        If ``None``, the scores for each class are returned. Otherwise, this
        determines the type of averaging performed on the data:

        ``'binary'``:
            Only report results for the class specified by ``pos_label``.
            This is applicable only if targets (``y_{true,pred}``) are binary.
        ``'micro'``:
            Calculate metrics globally by counting the total true positives,
            false negatives and false positives.
        ``'macro'``:
            Calculate metrics for each label, and find their unweighted
            mean.  This does not take label imbalance into account.
        ``'weighted'``:
            Calculate metrics for each label, and find their average weighted
            by support (the number of true instances for each label). This
            alters 'macro' to account for label imbalance; it can result in an
            F-score that is not between precision and recall.
        ``'samples'``:
            Calculate metrics for each instance, and find their average (only
            meaningful for multilabel classification where this differs from
            :func:`accuracy_score`).

再给个链接给予参考:https://www.cnblogs.com/harvey888/p/6964741.html

  • 12
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用Python安装from sklearn.metrics import average_precision_score,可以使用以下命令:pip install sklearn.metrics。 ### 回答2: 要安装`sklearn`库中的`average_precision_score`函数,需要先安装`scikit-learn`库。 安装步骤如下: 1. 打开命令行终端。 2. 输入`pip install -U scikit-learn`并按下回车键,等待安装过程完成。 3. 如果已经安装过`scikit-learn`库,可以跳过步骤2。 安装完成后,可以在Python脚本中使用`from sklearn.metrics import average_precision_score`语句来导入`average_precision_score`函数。 ### 回答3: 要安装 `sklearn.metrics` 中的 `average_precision_score` 方法,需要先确保已经安装了 `scikit-learn`(通常称为 `sklearn`)库。可以使用以下命令安装 `scikit-learn` 库: ``` pip install scikit-learn ``` 安装完毕后,您可以通过在 Python 代码中导入 `from sklearn.metrics import average_precision_score` 来使用 `average_precision_score` 方法。请确保您已经在代码中引入了所需的库: ```python from sklearn.metrics import average_precision_score ``` 然后,您就可以在代码中使用 `average_precision_score` 方法来计算平均精确度得分了。例如: ```python y_true = [1, 0, 1, 1, 0] y_scores = [0.2, 0.8, 0.5, 0.9, 0.3] average_precision = average_precision_score(y_true, y_scores) print("平均精确度得分:", average_precision) ``` 这将输出: ``` 平均精确度得分: 0.8333333333333333 ``` 以上是安装和使用 `sklearn.metrics` 中的 `average_precision_score` 的简要说明。通过按照上述步骤进行操作,您将能够在您的项目中使用该方法进行平均精确度评估
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值