Pag*_*Max 0 python tabular confusion-matrix scikit-learn
在我的 sklearn 逻辑回归模型中,我使用metrics.confusion_matrix命令获得了一个混淆矩阵。数组看起来像这样
array([[51, 0],
[26, 0]])
Run Code Online (Sandbox Code Playgroud)
忽略模型做得很糟糕的事实,我试图了解以漂亮的方式制表此矩阵的最佳方法是什么
我正在尝试使用tabulate 包,此代码部分适用于我
print tabulate(cm,headers=['Pred True', 'Pred False'])
Run Code Online (Sandbox Code Playgroud)
因为它给出了输出
Pred True Pred False
----------- ------------
51 0
26 0
Run Code Online (Sandbox Code Playgroud)
编辑
要插入行名称,我意识到插入元素而不是 zip 会有所帮助
cm_list=cm.tolist()
cm_list[0].insert(0,'Real True')
cm_list[1].insert(0,'Real False')
print tabulate(cm_list,headers=['Real/Pred','Pred True', 'Pred False'])
Run Code Online (Sandbox Code Playgroud)
因为它给
Real/Pred Pred True Pred False
----------- ----------- ------------
Real True 51 0
Real False 26 0
Run Code Online (Sandbox Code Playgroud)
但是,仍然想知道是否有更快或替代的美化混淆矩阵的方法。(我在网上找到了一些绘图示例,但我不需要那个)
谢谢,
您是否考虑过创建图形而不是表格?改编scikit-learn 示例中的一些代码,您可以获得一个看起来不错的图,它显示了您想要的内容。
import numpy as np
from matplotlib import pyplot as plt
def plot_confusion_matrix(cm, target_names, title='Confusion matrix', cmap=plt.cm.Blues):
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
plt.tight_layout()
width, height = cm.shape
for x in xrange(width):
for y in xrange(height):
plt.annotate(str(cm[x][y]), xy=(y, x),
horizontalalignment='center',
verticalalignment='center')
plt.ylabel('True label')
plt.xlabel('Predicted label')
cm = np.array([[13, 0, 0],[ 0, 10, 6],[ 0, 0, 9]])
plot_confusion_matrix(cm, ['A', 'B', 'C'])
Run Code Online (Sandbox Code Playgroud)