Plotly: How to make an annotated confusion matrix using a heatmap?

Written by - Aionlinecourse2345 times views

To create an annotated confusion matrix using a heatmap in Plotly, you will need to use the go.Heatmap trace type. Here is an example of how you can do this:

import plotly.express as px
import plotly.graph_objects as go

# Define the confusion matrix and labels
confusion_matrix = [[10, 2, 3], [4, 5, 6], [7, 8, 9]]
labels = ['Class 1', 'Class 2', 'Class 3']

# Create the heatmap trace
heatmap = go.Heatmap(z=confusion_matrix, x=labels, y=labels, colorscale='Viridis')

# Create the figure and add the heatmap trace
fig = go.Figure(data=[heatmap])

# Add annotations for each cell in the confusion matrix
for i in range(len(confusion_matrix)):
    for j in range(len(confusion_matrix[i])):
        fig.add_annotation(
            text=confusion_matrix[i][j],
            x=labels[j],
            y=labels[i],
            xref='x',
            yref='y'
        )

# Show the figure
fig.show()

This will create a heatmap of the confusion matrix, with each cell in the matrix annotated with its value. You can customize the appearance of the heatmap and the annotations by using the various options available in the go.Heatmap and fig.add_annotation functions.

Recommended Projects

Deep Learning Interview Guide

Topic modeling using K-means clustering to group customer reviews

Have you ever thought about the ways one can analyze a review to extract all the misleading or useful information?...

Natural Language Processing
Deep Learning Interview Guide

Medical Image Segmentation With UNET

Have you ever thought about how doctors are so precise in diagnosing any conditions based on medical images? Quite simply,...

Computer Vision
Deep Learning Interview Guide

Build A Book Recommender System With TF-IDF And Clustering(Python)

Have you ever thought about the reasons behind the segregation and recommendation of books with similarities? This project is aimed...

Machine LearningDeep LearningNatural Language Processing
Deep Learning Interview Guide

Automatic Eye Cataract Detection Using YOLOv8

Cataracts are a leading cause of vision impairment worldwide, affecting millions of people every year. Early detection and timely intervention...

Computer Vision
Deep Learning Interview Guide

Crop Disease Detection Using YOLOv8

In this project, we are utilizing AI for a noble objective, which is crop disease detection. Well, you're here if...

Computer Vision
Deep Learning Interview Guide

Vegetable classification with Parallel CNN model

The Vegetable Classification project shows how CNNs can sort vegetables efficiently. As industries like agriculture and food retail grow, automating...

Machine LearningDeep Learning
Deep Learning Interview Guide

Banana Leaf Disease Detection using Vision Transformer model

Banana cultivation is a significant agricultural activity in many tropical and subtropical regions, providing a vital source of income and...

Deep LearningComputer Vision