我正在使用两类数据集2
和4
,其中2
是肯定类,而4
是否定类(关于情感分析)。 / p>
我有来自模型的一组预测,以及一组实际值。我需要确定每个班级的精确度和召回率(正班和负班的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
但是我不确定如何计算。
如何使用上述数据调整当前的逻辑以检查每个班级的精确度和召回率得分?
答案 0 :(得分:1)
我想我知道该指出您的方向:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
这应该是您想要的。
请参阅Wikipedia链接:https://en.wikipedia.org/wiki/Confusion_matrix
并阅读如何使用它。