-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-benchmark-all.py
More file actions
138 lines (110 loc) · 4.37 KB
/
example-benchmark-all.py
File metadata and controls
138 lines (110 loc) · 4.37 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
"""
Benchmark all supported algorithms on classification (Iris) and regression
(Diabetes) datasets using AlgorithmBenchmark, then plot ranking charts and
save results to CSV files.
"""
import warnings
warnings.filterwarnings("ignore")
import matplotlib
matplotlib.use("Agg") # non-interactive backend; must be set before pyplot import
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
from pathlib import Path
from sklearn.datasets import load_iris, load_diabetes
from sklearn.model_selection import train_test_split
from optuml import AlgorithmBenchmark
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
N_TRIALS = 30
RANDOM_STATE = 42
OUTPUT_DIR = Path("benchmark_results")
OUTPUT_DIR.mkdir(exist_ok=True)
# ---------------------------------------------------------------------------
# Datasets
# ---------------------------------------------------------------------------
X_clf, y_clf = load_iris(return_X_y=True)
X_clf_train, X_clf_test, y_clf_train, y_clf_test = train_test_split(
X_clf, y_clf, test_size=0.2, random_state=RANDOM_STATE
)
X_reg, y_reg = load_diabetes(return_X_y=True)
X_reg_train, X_reg_test, y_reg_train, y_reg_test = train_test_split(
X_reg, y_reg, test_size=0.2, random_state=RANDOM_STATE
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def plot_ranking(summary_df, title, metric_label, output_path, color):
"""Horizontal bar chart of algorithm scores, best at the top."""
df = summary_df.dropna(subset=["best_score"]).sort_values("best_score")
fig, ax = plt.subplots(figsize=(9, max(4, len(df) * 0.45)))
bars = ax.barh(df["algorithm"], df["best_score"], color=color, edgecolor="white")
# Annotate each bar with the score value
for bar, score in zip(bars, df["best_score"]):
ax.text(
bar.get_width() + (df["best_score"].max() * 0.005),
bar.get_y() + bar.get_height() / 2,
f"{score:.4f}",
va="center",
fontsize=8,
)
ax.set_xlabel(metric_label)
ax.set_title(title)
ax.xaxis.set_major_formatter(mticker.FormatStrFormatter("%.3f"))
ax.margins(x=0.12)
plt.tight_layout()
fig.savefig(output_path, dpi=150)
plt.close(fig)
print(f" Plot saved → {output_path}")
def run_benchmark(task, X_train, y_train, label, metric_label, color):
print(f"\n{'='*60}")
print(f" {label}")
print(f"{'='*60}")
bench = AlgorithmBenchmark(
task=task,
n_trials=N_TRIALS,
random_state=RANDOM_STATE,
verbose=False,
)
bench.fit(X_train, y_train)
summary = bench.summary()
print(summary.to_string(index=False))
csv_path = OUTPUT_DIR / f"benchmark_{task}.csv"
summary.to_csv(csv_path, index=False)
print(f"\n Results saved → {csv_path}")
plot_path = OUTPUT_DIR / f"benchmark_{task}.png"
plot_ranking(summary, label, metric_label, plot_path, color)
print(f"\n Best algorithm : {bench.best_algorithm_}")
print(f" Best CV score : {bench.best_score_:.4f}")
return bench, summary
# ---------------------------------------------------------------------------
# Run benchmarks
# ---------------------------------------------------------------------------
clf_bench, clf_summary = run_benchmark(
task="classification",
X_train=X_clf_train,
y_train=y_clf_train,
label="Classification benchmark — Iris dataset (CV accuracy)",
metric_label="CV Accuracy",
color="steelblue",
)
reg_bench, reg_summary = run_benchmark(
task="regression",
X_train=X_reg_train,
y_train=y_reg_train,
label="Regression benchmark — Diabetes dataset (CV R²)",
metric_label="CV R²",
color="darkorange",
)
# ---------------------------------------------------------------------------
# Combined summary
# ---------------------------------------------------------------------------
print(f"\n{'='*60}")
print(" Summary")
print(f"{'='*60}")
print(f"Classification winner : {clf_bench.best_algorithm_} "
f"(accuracy={clf_bench.best_score_:.4f})")
print(f"Regression winner : {reg_bench.best_algorithm_} "
f"(R²={reg_bench.best_score_:.4f})")
print(f"\nAll outputs written to: {OUTPUT_DIR.resolve()}")