我正在处理两个类数据集,2和4,其中2是正类,4是负面类(关于情感分析)。
我从我的模型中得到了一组预测,以及一组实际值。我需要确定每个班级的精确性和召回率(正负两级的P和R分数)。
代码如下:
preds = [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 2]
actuals = [2, 4, 2, 4, 2, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2]
true_pos = 0
true_neg = 0
false_pos = 0
false_neg = 0
for pred, act in zip(preds, actuals):
# 2 is positive, 4 is negative
if(pred == 2 & act == 2):
true_pos += 1
elif(pred == 4 & act == 4):
true_neg += 1
elif(pred == 2 & act == 4):
false_pos += 1
elif(pred == 4 & act == 2):
false_neg += 1
print("True Positive: ", true_pos)
print("True Negative: ", true_neg)
print("False Positive: ", false_neg)
print("False Negative: ", false_neg)产生的结果:
True Positive: 1
True Negative: 14
False Positive: 0
False Negative: 0然而,我真的被困在应该如何按类计算这些指标上了。This SO post指示如何在总体上(而不是按类)执行此操作。
理想情况下,我的输出如下:
Class 2 P Score: x
Class 2 R Score: x
Class 4 P Score: x
Class 4 R Score: x但我不知道如何计算。
我如何调整我目前的逻辑,以检查精度和召回分数的每一个班级与上述数据?
发布于 2019-10-28 16:04:58
我想我知道你该朝哪个方向走:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
这应该是你要找的。
请参阅维基百科链接:https://en.wikipedia.org/wiki/Confusion_matrix
读一读如何使用这个。
https://stackoverflow.com/questions/58593527
复制相似问题