两个文本文件混淆矩阵的计算

2024-04-19 10:37:18 发布

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

我想计算两个文本文件的混淆矩阵。有人知道python或shell脚本中的库或工具可以做到这一点吗?在

例如,我有两个文件

文件A:

1
1
2
2

文件B:

^{pr2}$

我可以得到一个混乱矩阵:

   1   2
--------
1| 0   2
2| 0   2

更新:我想指出,原来的帖子包括行和列标签


Tags: 文件工具脚本矩阵标签shell帖子行和列
1条回答
网友
1楼 · 发布于 2024-04-19 10:37:18

这可能有点过头了,但scikit learn会很容易做到:

from sklearn.metrics import confusion_matrix

# Read the data
with open('file1', 'r') as infile:
    true_values = [int(i) for i in infile]
with open('file2', 'r') as infile:
    predictions = [int(i) for i in infile]

# Make confusion matrix
confusion = confusion_matrix(true_values, predictions)

print(confusion)

带输出

^{pr2}$

http://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html

更新: 要使用标签打印,您可以使用pandas或类似的方法转换为数据帧:

def print_confusion(confusion):
    print('   ' + '  '.join([str(n) for n in range(confusion.shape[1])]))
    for rownum in range(confusion.shape[0]):
        print(str(rownum) + '  ' + '  '.join([str(n) for n in confusion[rownum]]))

哪个指纹

   0  1
0  0  2
1  0  2

相关问题 更多 >