Spaces:
Sleeping
Sleeping
File size: 9,873 Bytes
7ac4226 | 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
confusion_matrix,
classification_report
)
import gradio as gr
# -------- Data --------
iris = load_iris()
X_full = pd.DataFrame(iris.data, columns=iris.feature_names)
y_full = pd.Series(iris.target, name="target")
class_names = iris.target_names
FEATURE_CHOICES = list(X_full.columns)
# -------- Plot helpers --------
def plot_confusion_matrix(cm, class_names):
fig, ax = plt.subplots(figsize=(5, 4), dpi=120)
im = ax.imshow(cm, interpolation="nearest")
ax.figure.colorbar(im, ax=ax)
ax.set(
xticks=np.arange(cm.shape[1]),
yticks=np.arange(cm.shape[0]),
xticklabels=class_names,
yticklabels=class_names,
ylabel="True label",
xlabel="Predicted label",
title="Confusion Matrix",
)
# Show counts on cells
thresh = cm.max() / 2.0
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(
j, i, format(cm[i, j], "d"),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black"
)
fig.tight_layout()
return fig
def plot_feature_importances(model, feature_names):
importances = model.feature_importances_
order = np.argsort(importances)[::-1]
fig, ax = plt.subplots(figsize=(6, 4), dpi=120)
ax.bar(range(len(importances)), importances[order])
ax.set_xticks(range(len(importances)))
ax.set_xticklabels([feature_names[i] for i in order], rotation=30, ha="right")
ax.set_ylabel("Importance")
ax.set_title("Feature Importances")
fig.tight_layout()
return fig
def plot_decision_regions_2d(model, X, y, feature_x_name, feature_y_name, class_names):
# X: dataframe with exactly two columns (selected features)
# Create a mesh
x_min, x_max = X.iloc[:, 0].min() - 0.5, X.iloc[:, 0].max() + 0.5
y_min, y_max = X.iloc[:, 1].min() - 0.5, X.iloc[:, 1].max() + 0.5
xx, yy = np.meshgrid(
np.linspace(x_min, x_max, 300),
np.linspace(y_min, y_max, 300)
)
grid = np.c_[xx.ravel(), yy.ravel()]
Z = model.predict(grid).reshape(xx.shape)
fig, ax = plt.subplots(figsize=(6, 5), dpi=120)
ax.contourf(xx, yy, Z, alpha=0.2)
# Scatter original points
for idx, cname in enumerate(class_names):
mask = (y == idx)
ax.scatter(
X.loc[mask, feature_x_name],
X.loc[mask, feature_y_name],
label=cname, s=24
)
ax.set_xlabel(feature_x_name)
ax.set_ylabel(feature_y_name)
ax.set_title("Decision Regions (2 features)")
ax.legend(loc="upper right", fontsize=8)
fig.tight_layout()
return fig
# -------- Core training + evaluation --------
def run_decision_tree(
test_size,
random_state,
criterion,
splitter,
unlimited_depth,
max_depth,
min_samples_split,
min_samples_leaf,
max_features,
class_weight_mode,
feature_x_name,
feature_y_name
):
# Map UI values to sklearn-friendly options
md = None if unlimited_depth else int(max_depth)
if max_features == "None":
mf = None
elif max_features == "auto/sqrt":
mf = "sqrt"
elif max_features == "log2":
mf = "log2"
else:
mf = None
cw = None if class_weight_mode == "None" else "balanced"
# Train / Test split
X_train, X_test, y_train, y_test = train_test_split(
X_full, y_full,
test_size=float(test_size),
random_state=int(random_state),
stratify=y_full
)
# Model
clf = DecisionTreeClassifier(
criterion=criterion,
splitter=splitter,
max_depth=md,
min_samples_split=int(min_samples_split),
min_samples_leaf=int(min_samples_leaf),
max_features=mf,
class_weight=cw,
random_state=int(random_state)
)
clf.fit(X_train, y_train)
# Predictions & metrics
y_pred = clf.predict(X_test)
acc = accuracy_score(y_test, y_pred)
prec = precision_score(y_test, y_pred, average="macro", zero_division=0)
rec = recall_score(y_test, y_pred, average="macro", zero_division=0)
f1 = f1_score(y_test, y_pred, average="macro", zero_division=0)
report = classification_report(y_test, y_pred, target_names=class_names, zero_division=0)
# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
cm_fig = plot_confusion_matrix(cm, class_names)
# Feature importances
fi_fig = plot_feature_importances(clf, X_full.columns)
# Decision boundary for 2 chosen features
# Refit a new tree **on those two features only** to plot clear regions (same hyperparams)
if feature_x_name == feature_y_name:
# If same feature accidentally chosen, pick a safe default distinct pair
feature_x_name, feature_y_name = FEATURE_CHOICES[0], FEATURE_CHOICES[1]
two_feat_cols = [feature_x_name, feature_y_name]
X2_train = X_train[two_feat_cols]
X2_test = X_test[two_feat_cols]
clf2 = DecisionTreeClassifier(
criterion=criterion,
splitter=splitter,
max_depth=md,
min_samples_split=int(min_samples_split),
min_samples_leaf=int(min_samples_leaf),
max_features=None, # force 2D features for plotting
class_weight=cw,
random_state=int(random_state)
)
clf2.fit(X2_train, y_train)
boundary_fig = plot_decision_regions_2d(
clf2,
pd.concat([X2_train, X2_test], axis=0),
pd.concat([y_train, y_test], axis=0).values,
feature_x_name, feature_y_name, class_names
)
# Make a small metrics dataframe for display
metrics_df = pd.DataFrame(
[{"accuracy": acc, "precision_macro": prec, "recall_macro": rec, "f1_macro": f1}]
)
# Classification report as preformatted text
report_text = f"```\n{report}\n```"
return (
metrics_df,
report_text,
cm_fig,
fi_fig,
boundary_fig
)
# -------- Gradio UI --------
with gr.Blocks(title="Iris Decision Tree Explorer") as demo:
gr.Markdown(
"""
# 🌸 Iris Decision Tree Explorer
Train a Decision Tree on the classic Iris dataset. Tweak hyperparameters on the left, then inspect metrics and plots on the right.
"""
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Hyperparameters")
criterion = gr.Dropdown(
label="criterion",
choices=["gini", "entropy", "log_loss"],
value="gini"
)
splitter = gr.Dropdown(
label="splitter",
choices=["best", "random"],
value="best"
)
unlimited_depth = gr.Checkbox(
label="Unlimited depth (ignore max_depth)",
value=True
)
max_depth = gr.Slider(
label="max_depth (used only if Unlimited depth = False)",
minimum=1, maximum=30, step=1, value=5
)
min_samples_split = gr.Slider(
label="min_samples_split",
minimum=2, maximum=20, step=1, value=2
)
min_samples_leaf = gr.Slider(
label="min_samples_leaf",
minimum=1, maximum=20, step=1, value=1
)
max_features = gr.Dropdown(
label="max_features",
choices=["None", "auto/sqrt", "log2"],
value="None"
)
class_weight_mode = gr.Dropdown(
label="class_weight",
choices=["None", "balanced"],
value="None"
)
gr.Markdown("### Data & Reproducibility")
test_size = gr.Slider(
label="test_size",
minimum=0.1, maximum=0.5, step=0.05, value=0.2
)
random_state = gr.Number(
label="random_state",
value=42, precision=0
)
gr.Markdown("### Decision Region (2D) Features")
feature_x_name = gr.Dropdown(
label="X-axis feature",
choices=FEATURE_CHOICES,
value=FEATURE_CHOICES[0]
)
feature_y_name = gr.Dropdown(
label="Y-axis feature",
choices=FEATURE_CHOICES,
value=FEATURE_CHOICES[1]
)
run_btn = gr.Button("Train & Evaluate", variant="primary")
with gr.Column(scale=2):
gr.Markdown("### Results")
metrics_df = gr.Dataframe(
label="Metrics (test set)",
interactive=False,
headers=["accuracy", "precision_macro", "recall_macro", "f1_macro"]
)
report_md = gr.Markdown(label="Classification Report")
with gr.Row():
cm_plot = gr.Plot(label="Confusion Matrix")
fi_plot = gr.Plot(label="Feature Importances")
boundary_plot = gr.Plot(label="Decision Regions (2 features)")
run_btn.click(
fn=run_decision_tree,
inputs=[
test_size, random_state,
criterion, splitter, unlimited_depth, max_depth,
min_samples_split, min_samples_leaf, max_features, class_weight_mode,
feature_x_name, feature_y_name
],
outputs=[metrics_df, report_md, cm_plot, fi_plot, boundary_plot]
)
if __name__ == "__main__":
# You can set share=True if you want a public link when running locally
demo.launch()
|