gpt4 book ai didi

python - 如何解释 sklearn fusion_matrix 函数中的 labels 参数?

转载 作者:行者123 更新时间:2023-11-30 09:44:37 24 4
gpt4 key购买 nike

假设我有以下两组类别和一个包含目标名称的变量:

spam = ["blue", "white", "blue", "yellow", "red"]
flagged = ["blue", "white", "yellow", "blue", "red"]
target_names = ["blue", "white", "yellow", "red"]

当我如下使用confusion_matrix函数时,结果如下:

from sklearn.metrics import confusion_matrix
confusion_matrix(spam, flagged, labels=target_names)

[[1 0 1 0]
[0 1 0 0]
[1 0 0 0]
[0 0 0 1]]

但是,当我向参数 labels 提供我只需要“blue”中的指标的信息时,我得到以下结果:

confusion_matrix(spam, flagged, labels=["blue"])

array([[1]])

只有一个数字,我无法计算准确度、精确度、召回率等。我在这里做错了什么?填充黄色、白色或蓝色将得到 0、1 和 1。

最佳答案

However, when i give the parameter labels the information that i only want the metrics from 'blue'

它不是这样工作的。

在像您这样的多类设置中,精度和召回率是根据整个混淆矩阵计算每个类的。

我已经在another answer中详细解释了原理和计算。 ;以下是它如何应用于您自己的混淆矩阵 cm 的情况:

import numpy as np

# your comfusion matrix:
cm =np.array([[1, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1]])

# true positives:
TP = np.diag(cm)
TP
# array([1, 1, 0, 1])

# false positives:
FP = np.sum(cm, axis=0) - TP
FP
# array([1, 0, 1, 0])

# false negatives
FN = np.sum(cm, axis=1) - TP
FN
# array([1, 0, 1, 0])

现在,根据精确率和召回率的定义,我们有:

precision = TP/(TP+FP)
recall = TP/(TP+FN)

对于您的示例,给出:

precision
# array([ 0.5, 1. , 0. , 1. ])

recall
# array([ 0.5, 1. , 0. , 1. ])

即对于“蓝色”类别,您可以获得 50% 的准确率和召回率。

这里的精度和召回率碰巧相同纯属巧合,因为 FP 和 FN 数组碰巧相同;尝试不同的预测来获得感觉......

关于python - 如何解释 sklearn fusion_matrix 函数中的 labels 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54129826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com