为了账号安全,请及时绑定邮箱和手机立即绑定

绘图:如何使用热图制作带注释的混淆矩阵?

绘图:如何使用热图制作带注释的混淆矩阵?

红糖糍粑 2022-09-13 20:04:08
我喜欢使用 Plotly 来可视化所有内容,我试图通过 Plotly 可视化一个混淆矩阵,这是我的代码:def plot_confusion_matrix(y_true, y_pred, class_names):    confusion_matrix = metrics.confusion_matrix(y_true, y_pred)    confusion_matrix = confusion_matrix.astype(int)    layout = {        "title": "Confusion Matrix",         "xaxis": {"title": "Predicted value"},         "yaxis": {"title": "Real value"}    }    fig = go.Figure(data=go.Heatmap(z=confusion_matrix,                                    x=class_names,                                    y=class_names,                                    hoverongaps=False),                    layout=layout)    fig.show()结果是如何在相应的单元格中显示数字,而不是像这样悬停
查看完整描述

3 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

您可以使用带注释的热图来获得以下内容:ff.create_annotated_heatmap()

//img1.sycdn.imooc.com//6320722f0001f4f107530469.jpg

完整代码:


import plotly.figure_factory as ff


z = [[0.1, 0.3, 0.5, 0.2],

     [1.0, 0.8, 0.6, 0.1],

     [0.1, 0.3, 0.6, 0.9],

     [0.6, 0.4, 0.2, 0.2]]


x = ['healthy', 'multiple diseases', 'rust', 'scab']

y =  ['healthy', 'multiple diseases', 'rust', 'scab']


# change each element of z to type string for annotations

z_text = [[str(y) for y in x] for x in z]


# set up figure 

fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')


# add title

fig.update_layout(title_text='<i><b>Confusion matrix</b></i>',

                  #xaxis = dict(title='x'),

                  #yaxis = dict(title='x')

                 )


# add custom xaxis title

fig.add_annotation(dict(font=dict(color="black",size=14),

                        x=0.5,

                        y=-0.15,

                        showarrow=False,

                        text="Predicted value",

                        xref="paper",

                        yref="paper"))


# add custom yaxis title

fig.add_annotation(dict(font=dict(color="black",size=14),

                        x=-0.35,

                        y=0.5,

                        showarrow=False,

                        text="Real value",

                        textangle=-90,

                        xref="paper",

                        yref="paper"))


# adjust margins to make room for yaxis title

fig.update_layout(margin=dict(t=50, l=200))


# add colorbar

fig['data'][0]['showscale'] = True

fig.show()



查看完整回答
反对 回复 2022-09-13
?
慕的地8271018

TA贡献1796条经验 获得超4个赞

我发现@vestland的策略是最有用的。


但是,与传统的混淆矩阵不同,正确的模型预测是沿着右上角线,而不是左上角。


这可以通过反转混淆矩阵的所有索引值来轻松修复,如下所示:


import plotly.figure_factory as ff


z = [[0.1, 0.3, 0.5, 0.2],

     [1.0, 0.8, 0.6, 0.1],

     [0.1, 0.3, 0.6, 0.9],

     [0.6, 0.4, 0.2, 0.2]]


# invert z idx values

z = z[::-1]


x = ['healthy', 'multiple diseases', 'rust', 'scab']

y =  x[::-1].copy() # invert idx values of x


# change each element of z to type string for annotations

z_text = [[str(y) for y in x] for x in z]


# set up figure 

fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')


# add title

fig.update_layout(title_text='<i><b>Confusion matrix</b></i>',

                  #xaxis = dict(title='x'),

                  #yaxis = dict(title='x')

                 )


# add custom xaxis title

fig.add_annotation(dict(font=dict(color="black",size=14),

                        x=0.5,

                        y=-0.15,

                        showarrow=False,

                        text="Predicted value",

                        xref="paper",

                        yref="paper"))


# add custom yaxis title

fig.add_annotation(dict(font=dict(color="black",size=14),

                        x=-0.35,

                        y=0.5,

                        showarrow=False,

                        text="Real value",

                        textangle=-90,

                        xref="paper",

                        yref="paper"))


# adjust margins to make room for yaxis title

fig.update_layout(margin=dict(t=50, l=200))


# add colorbar

fig['data'][0]['showscale'] = True

fig.show()


查看完整回答
反对 回复 2022-09-13
?
倚天杖

TA贡献1828条经验 获得超3个赞

正如@vestland所说,你可以用情节注释数字。热图可用作任何类型的绘图。这是一个用于从混淆矩阵(基本上只是一个带有数字的2-d向量)绘制热图的代码。


def plot_confusion_matrix(cm, labels, title):

# cm : confusion matrix list(list)

# labels : name of the data list(str)

# title : title for the heatmap

data = go.Heatmap(z=cm, y=labels, x=labels)

annotations = []

for i, row in enumerate(cm):

    for j, value in enumerate(row):

        annotations.append(

            {

                "x": labels[i],

                "y": labels[j],

                "font": {"color": "white"},

                "text": str(value),

                "xref": "x1",

                "yref": "y1",

                "showarrow": False

            }

        )

layout = {

    "title": title,

    "xaxis": {"title": "Predicted value"},

    "yaxis": {"title": "Real value"},

    "annotations": annotations

}

fig = go.Figure(data=data, layout=layout)

return fig


查看完整回答
反对 回复 2022-09-13
  • 3 回答
  • 0 关注
  • 310 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号