-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
186 lines (152 loc) · 6.54 KB
/
Copy pathapp.py
File metadata and controls
186 lines (152 loc) · 6.54 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
import matplotlib.pyplot as plt
from io import BytesIO
# ------------------------ Page config ------------------------
st.set_page_config(page_title="Confusion Matrix Generator", layout="centered")
# ------------------------ Centered Title ------------------------
st.markdown(
"<h1 style='text-align: center;'>Confusion Matrix Generator</h1>",
unsafe_allow_html=True
)
st.markdown(
"<p style='text-align: center;'>Generate beautiful confusion matrices for your ML models</p>",
unsafe_allow_html=True
)
st.info("You can either **upload a CSV file** containing true and predicted labels, or **enter the labels manually** in the fields below.")
st.markdown("---")
# ------------------------ CSV Upload ------------------------
uploaded_file = st.file_uploader(
"Upload CSV File",
type=["csv"],
help="CSV must contain true and predicted labels (true/pred columns)."
)
# ------------------------ Manual input ------------------------
col1, col2 = st.columns(2)
with col1:
true_labels_input = st.text_input(
"True Labels (comma separated)",
placeholder="Example: cat, dog, cat, cat, dog"
)
with col2:
pred_labels_input = st.text_input(
"Predicted Labels (comma separated)",
placeholder="Example: cat, cat, dog, cat, dog"
)
# ------------------------ Color map selection ------------------------
cmap = st.selectbox(
"Choose color map for the confusion matrix",
options=[
"viridis", "plasma", "inferno", "magma",
"cividis", "Blues", "Greens", "coolwarm"
]
)
# ------------------------ Helper functions ------------------------
def parse_labels(text):
"""Parse manual input labels (numbers or strings)."""
try:
return [x.strip() for x in text.split(",")]
except:
return None
def detect_columns(df):
true_cols = ["true", "t", "actual", "label"]
pred_cols = ["pred", "predicted", "p"]
true_col = next((c for c in df.columns if c.lower() in true_cols), None)
pred_col = next((c for c in df.columns if c.lower() in pred_cols), None)
return true_col, pred_col
# ------------------------ Custom button style ------------------------
st.markdown("""
<style>
.custom-button {
background-color: #8CA9FF;
color: black;
font-weight: bold;
}
.custom-button:hover {
background-color: #AAC4F5;
color: black;
}
</style>
""", unsafe_allow_html=True)
# ------------------------ Generate Button ------------------------
generate_clicked = st.button("Generate Confusion Matrix", key="generate", help="Click to generate matrix", args=None)
# ------------------------ Load labels & generate ------------------------
if generate_clicked:
true_labels = None
pred_labels = None
# ------------------------ CSV input ------------------------
if uploaded_file is not None:
try:
df = pd.read_csv(uploaded_file)
true_col, pred_col = detect_columns(df)
if true_col is None or pred_col is None:
st.error("Could not detect true/pred columns in CSV.")
else:
true_labels = df[true_col].tolist()
pred_labels = df[pred_col].tolist()
st.success("CSV loaded successfully ✅")
except:
st.error("Error reading CSV file.")
# ------------------------ Manual input fallback ------------------------
if true_labels_input and pred_labels_input:
true_labels = parse_labels(true_labels_input)
pred_labels = parse_labels(pred_labels_input)
if true_labels is None or pred_labels is None:
st.error("Invalid manual input. Use numbers or strings separated by commas.")
# ------------------------ Validation ------------------------
if true_labels is not None and pred_labels is not None:
if len(true_labels) != len(pred_labels):
st.error("True labels and predicted labels must have the same length.")
else:
# ------------------------ Confusion Matrix ------------------------
classes = sorted(list(set(true_labels) | set(pred_labels))) # works for strings and numbers
cm = confusion_matrix(true_labels, pred_labels, labels=classes)
fig, ax = plt.subplots(figsize=(6,5))
im = ax.imshow(cm, cmap=cmap)
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(
j, i, cm[i, j],
ha="center",
va="center",
color="white" if cm[i, j] > cm.max()/2 else "black"
)
ax.set_xlabel("Predicted Label")
ax.set_ylabel("True Label")
ax.set_xticks(range(len(classes)))
ax.set_yticks(range(len(classes)))
ax.set_xticklabels(classes)
ax.set_yticklabels(classes)
ax.set_title("Confusion Matrix")
plt.colorbar(im, ax=ax)
st.pyplot(fig)
# ------------------------ Metrics ------------------------
metrics_df = pd.DataFrame({
"Metric": ["Accuracy", "Precision", "Recall", "F1-score"],
"Value": [
f"{accuracy_score(true_labels, pred_labels):.4f}",
f"{precision_score(true_labels, pred_labels, average='weighted', zero_division=0):.4f}",
f"{recall_score(true_labels, pred_labels, average='weighted', zero_division=0):.4f}",
f"{f1_score(true_labels, pred_labels, average='weighted', zero_division=0):.4f}"
]
})
st.subheader("Metrics")
st.table(metrics_df)
# ------------------------ Download Button ------------------------
buf = BytesIO()
fig.savefig(buf, format="png", bbox_inches="tight")
buf.seek(0)
st.download_button(
label="📥 Download Confusion Matrix",
data=buf,
file_name="confusion_matrix.png",
mime="image/png"
)
# ------------------------ Footer ------------------------
st.markdown("---")
st.markdown(
"<p style='text-align: center;'>Made with ❤️ by Paresh Nayak</p>",
unsafe_allow_html=True
)