Plotly:如何确定混淆矩阵的方向?

Plotly: How to determine direction of confusion matrix?

我正在使用 Plotly 绘制以下混淆矩阵:

cf_matrix= confusion_matrix(y_true, y_pred)
print(cf_matrix)

[[1595  545 2240]
 [ 788  722 2870]
 [ 181  118 4081]]

import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(cf_matrix)
fig.show()

但由于某种原因,情节“从左下角开始”的方向错误,如下所示:

是否有任何变通方法可以使其成为 Matrix 的通常方向并同时显示 X 和 Y 轴?

答案:

fig.update_layout(yaxis = dict(categoryorder = 'category descending'))

您示例中的一些细节:

如果您提供的数据实际上是您的数据集,那么您还没有指定任何名称。例如,您可以使用 names = list('ABC'),然后执行:

fig = ff.create_annotated_heatmap(z, x = names, y = names)

为了改变热图的顺序,你可以这样做:

fig.update_layout(yaxis = dict(categoryorder = 'category descending'))

剧情:

如果这完全不是您希望的显示方式,您可以用同样的方式更改xaxis。此外,categoryorder 的其他选项是:

['trace', 'category ascending', 'category descending',
'array', 'total ascending', 'total descending', 'min
ascending', 'min descending', 'max ascending', 'max
descending', 'sum ascending', 'sum descending', 'mean
ascending', 'mean descending', 'median ascending', 'median
descending']

完整代码:

import plotly.figure_factory as ff

z = [[1595, 545, 2240],
 [ 788,   722,  2870],
 [ 181,   118,  4081]]

names = list('ABC')

fig = ff.create_annotated_heatmap(z, x = names, y = names)
fig.update_layout(yaxis = dict(categoryorder = 'category descending'))
fig.show()