Untitled

 avatar
unknown
plain_text
8 months ago
1.1 kB
3
Indexable
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from sklearn.metrics import confusion_matrix


# Compute confusion matrix
cm = confusion_matrix(flag_out, pred_out)
labels = ['Class 0', 'Class 1']

# Create hovertext
hovertext = [[f"Names: {X_Y[(X_Y['Flag'] == i) & (X_Y['y_pred_binary'] == j)]['Portfolio Name'].values}"
            #   f"Additional Info: {df[(df['Actual'] == i) & (df['Predicted'] == j)]['Additional Info'].values}"
              for j in range(len(labels))] for i in range(len(labels))]

# Create a heatmap with hovertext
fig = go.Figure(data=go.Heatmap(
                   z=cm,
                   x=labels,
                   y=labels,
                   hoverongaps=False,
                   colorscale='Blues',
                   text=hovertext,
                   hoverinfo='text'))

# Update layout
fig.update_layout(
    title='Confusion Matrix with Hover Info',
    xaxis=dict(title='Predicted Labels'),
    yaxis=dict(title='True Labels'),
    yaxis_autorange='reversed'
)

fig.show()
Editor is loading...
Leave a Comment