如何从打印结果中计算字母?

2024-04-19 01:14:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着计算代码,从一个热门的MNIST字母数据中找出准确度分数。我想计算MNIST数据中每个标签的准确度,因为我对精度、召回率和f1分数使用相同的方法。y_true是数据帧[88800, 26]。首先,我定义了真正的积极,真正的消极和其他。我的代码是:

for i in y_true:
x=y_true[i]
y=y_pred[i]
    for j in range(len(x)):
       if (x.values[j] == 1) and (y.values[j] == 1):
           print("True Positive", y_pred.columns[i-1])
       elif (x.values[j] == 0) and (y.values[j] == 0):
           print("True Negative", y_pred.columns[i-1])
       elif (x.values[j] == 0) and (y.values[j] == 1):
           print("False Positive", y_pred.columns[i-1])
       else:
           print("False Negative", y_pred.columns[i-1])

输出为:

True Positive 1
True Positive 1
True Negative 1
...
True Negative 26

直到1和26都是标签的行。但是,我意识到,我无法从打印结果中计算每个标签有多少真阳性、真阴性、假阳性和假阴性。我不知道怎么数。能从打印结果中数一数吗?你知道吗


Tags: columnsand数据代码true标签分数values
1条回答
网友
1楼 · 发布于 2024-04-19 01:14:35

您可以在代码中使用Counter

from collections import Counter

   c = Counter()


   for j in range(len(x)):
       if (x.values[j] == 1) and (y.values[j] == 1):
           print("True Positive", y_pred.columns[i-1])
           c.update([f'"True Positive" {y_pred.columns[i-1]}'])
       elif (x.values[j] == 0) and (y.values[j] == 0):
           print("True Negative", y_pred.columns[i-1])
           c.update([f'"True Negative" {y_pred.columns[i-1]}'])
       elif (x.values[j] == 0) and (y.values[j] == 1):
           print("False Positive", y_pred.columns[i-1])
           c.update([f'"False Positive" {y_pred.columns[i-1]}'])
       else:
           print("False Negative", y_pred.columns[i-1])
           c.update([f'"False Negative" {y_pred.columns[i-1]}'])

在这之后,c将是您想要的输出。你知道吗

要打印输出,请使用以下命令:

for k,v in dict(c).items():
    print(k,':',v)

相关问题 更多 >