-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathplot_helpers.py
More file actions
47 lines (40 loc) · 1.58 KB
/
plot_helpers.py
File metadata and controls
47 lines (40 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(y_true, y_pred, labels=None, cmap="Blues", annotate=True, normalize=False, model_name="Model"):
"""
Plots a confusion matrix with optional annotations and color customization.
Parameters:
y_true (array-like): True labels
y_pred (array-like): Predicted labels
labels (list): List of class labels for axes
cmap (str): Color map for the plot (default: 'Blues')
annotate (bool): Whether to show cell values
normalize (bool): Normalize values to show percentages
model_name (str): Name of the model for the plot title
Returns:
fig (matplotlib.figure.Figure): Confusion matrix figure for Streamlit display
"""
# Compute confusion matrix
cm = confusion_matrix(y_true, y_pred, labels=labels)
if normalize:
cm = cm.astype("float") / cm.sum(axis=1, keepdims=True)
# Set up figure
fig, ax = plt.subplots(figsize=(6, 5))
sns.heatmap(
cm,
annot=annotate,
fmt=".2f" if normalize else "d",
cmap=cmap,
xticklabels=labels if labels is not None else sorted(set(y_true)),
yticklabels=labels if labels is not None else sorted(set(y_true)),
cbar=True,
ax=ax,
linewidths=0.5,
linecolor="gray"
)
ax.set_xlabel("Predicted Labels", fontsize=11)
ax.set_ylabel("True Labels", fontsize=11)
ax.set_title(f"Confusion Matrix - {model_name}", fontsize=13)
plt.tight_layout()
return fig