-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarks.py
More file actions
206 lines (175 loc) · 7.5 KB
/
benchmarks.py
File metadata and controls
206 lines (175 loc) · 7.5 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
Benchmark script for Snake classifier.
Trains Snake on classic sklearn datasets + Spaceship Titanic at n_layers=5, 15, 50
and reports accuracy in a markdown table ready to paste into the README.
Requirements (benchmark only — Snake itself has zero deps):
pip install pandas scikit-learn
For Spaceship Titanic, the script downloads the dataset from GitHub automatically.
The CSV is stored in /tmp and never committed to the repo.
"""
import csv
import os
import random
import sys
import time
import urllib.request
import pandas as pd
from sklearn.datasets import load_iris, load_wine, load_breast_cancer, load_digits
from sklearn.model_selection import train_test_split
sys.path.insert(0, ".")
from algorithmeai import Snake
SEED = 42
BUCKET = 250
NOISE = 0.25
LAYERS = [5, 15, 50]
SPACESHIP_URL = "https://raw.githubusercontent.com/AmirFARES/Kaggle-Spaceship-Titanic/main/data/train.csv"
SPACESHIP_PATH = "/tmp/spaceship_titanic_train.csv"
DATASETS = [
("Iris", load_iris, {"type": "Multi", "text_feats": 0}),
("Wine", load_wine, {"type": "Multi", "text_feats": 0}),
("Breast Cancer", load_breast_cancer, {"type": "Binary", "text_feats": 0}),
("Digits", load_digits, {"type": "Multi", "text_feats": 0}),
]
def load_as_dicts(loader):
"""Load a sklearn dataset as list[dict] with 'target' as first key."""
ds = loader()
feature_names = [f.replace(" ", "_").replace("(", "").replace(")", "") for f in ds.feature_names]
records = []
for i in range(len(ds.data)):
row = {"target": str(ds.target[i])}
for j, name in enumerate(feature_names):
row[name] = ds.data[i][j]
records.append(row)
return records, len(ds.data), len(feature_names), len(set(ds.target))
def load_spaceship_titanic():
"""Download (if needed) and load Spaceship Titanic as list[dict]."""
if not os.path.exists(SPACESHIP_PATH):
print(f" Downloading Spaceship Titanic to {SPACESHIP_PATH} ...")
urllib.request.urlretrieve(SPACESHIP_URL, SPACESHIP_PATH)
rows = []
with open(SPACESHIP_PATH) as f:
reader = csv.DictReader(f)
for r in reader:
cleaned = {"Transported": r["Transported"]}
for k in ["HomePlanet", "CryoSleep", "Destination", "VIP"]:
cleaned[k] = r[k] if r[k] else "unknown"
cabin = r.get("Cabin", "")
if cabin and "/" in cabin:
parts = cabin.split("/")
cleaned["CabinDeck"] = parts[0]
cleaned["CabinSide"] = parts[2] if len(parts) > 2 else "unknown"
else:
cleaned["CabinDeck"] = "unknown"
cleaned["CabinSide"] = "unknown"
for k in ["Age", "RoomService", "FoodCourt", "ShoppingMall", "Spa", "VRDeck"]:
try:
cleaned[k] = float(r[k]) if r[k] else 0.0
except ValueError:
cleaned[k] = 0.0
rows.append(cleaned)
return rows, len(rows), 12, 2
def accuracy(model, test_data, target_key="target"):
correct = 0
for row in test_data:
features = {k: v for k, v in row.items() if k != target_key}
pred = model.get_prediction(features)
if str(pred) == str(row[target_key]):
correct += 1
return correct / len(test_data) if test_data else 0
def accuracy_binary(model, test_data, target_key="Transported"):
"""Accuracy for binary True/False targets stored as int 0/1 in the model."""
correct = 0
for row in test_data:
features = {k: v for k, v in row.items() if k != target_key}
pred = model.get_prediction(features)
actual_str = row[target_key]
actual = 1 if actual_str in ("True", "TRUE", "true") else 0
if pred == actual:
correct += 1
return correct / len(test_data) if test_data else 0
def run_benchmarks():
all_results = {n: [] for n in LAYERS}
# sklearn datasets
for ds_name, loader, meta in DATASETS:
records, n_samples, n_features, n_classes = load_as_dicts(loader)
train_data, test_data = train_test_split(records, test_size=0.2, random_state=SEED)
for n_layers in LAYERS:
t0 = time.time()
model = Snake(train_data, n_layers=n_layers, bucket=BUCKET, noise=NOISE, vocal=True)
train_time = time.time() - t0
train_acc = accuracy(model, train_data)
test_acc = accuracy(model, test_data)
t0 = time.time()
for row in test_data[:50]:
features = {k: v for k, v in row.items() if k != "target"}
model.get_prediction(features)
n_infer = min(50, len(test_data))
avg_infer = (time.time() - t0) / n_infer * 1000
all_results[n_layers].append({
"dataset": ds_name,
"type": meta["type"],
"samples": n_samples,
"features": n_features,
"text_feats": meta["text_feats"],
"classes": n_classes,
"train_acc": train_acc,
"test_acc": test_acc,
"train_time": train_time,
"avg_infer_ms": avg_infer,
})
print(f" {ds_name} | n_layers={n_layers} | train={train_acc:.1%} test={test_acc:.1%} | {train_time:.1f}s")
# Spaceship Titanic
print("\n --- Spaceship Titanic ---")
records, n_samples, n_features, n_classes = load_spaceship_titanic()
random.seed(SEED)
random.shuffle(records)
split = int(len(records) * 0.8)
train_data = records[:split]
test_data = records[split:]
for n_layers in LAYERS:
t0 = time.time()
model = Snake(train_data, n_layers=n_layers, bucket=BUCKET, noise=NOISE, vocal=True)
train_time = time.time() - t0
train_acc = accuracy_binary(model, train_data)
test_acc = accuracy_binary(model, test_data)
t0 = time.time()
for row in test_data[:50]:
features = {k: v for k, v in row.items() if k != "Transported"}
model.get_prediction(features)
n_infer = min(50, len(test_data))
avg_infer = (time.time() - t0) / n_infer * 1000
all_results[n_layers].append({
"dataset": "Spaceship Titanic",
"type": "Binary",
"samples": n_samples,
"features": n_features,
"text_feats": 6,
"classes": n_classes,
"train_acc": train_acc,
"test_acc": test_acc,
"train_time": train_time,
"avg_infer_ms": avg_infer,
})
print(f" Spaceship Titanic | n_layers={n_layers} | train={train_acc:.1%} test={test_acc:.1%} | {train_time:.1f}s")
# Print markdown tables
print("\n\n## Benchmark Results\n")
for n_layers in LAYERS:
print(f"### `n_layers={n_layers}`\n")
print("| Dataset | Type | Samples | Features | Classes | Train Acc | Test Acc | Train Time | Inference |")
print("|---------|------|---------|----------|---------|-----------|----------|------------|-----------|")
for r in all_results[n_layers]:
feat_str = f"{r['features']}"
print(
f"| {r['dataset']:<20} "
f"| {r['type']:<6} "
f"| {r['samples']:<7} "
f"| {feat_str:<8} "
f"| {r['classes']:<7} "
f"| {r['train_acc']:.1%} "
f"| {r['test_acc']:.1%} "
f"| {r['train_time']:.1f}s "
f"| {r['avg_infer_ms']:.1f}ms |"
)
print()
if __name__ == "__main__":
run_benchmarks()