-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_final.py
More file actions
271 lines (228 loc) · 10 KB
/
benchmark_final.py
File metadata and controls
271 lines (228 loc) · 10 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
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
"""
TurboCat vs CatBoost - FINAL Comprehensive Benchmark
All quality and speed metrics on multiple datasets
"""
import numpy as np
import time
import sys
sys.path.insert(0, 'build')
from sklearn.datasets import make_classification, load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import (
roc_auc_score, accuracy_score, precision_score,
recall_score, f1_score, log_loss
)
import warnings
warnings.filterwarnings('ignore')
import _turbocat as tc
from catboost import CatBoostClassifier
def create_datasets():
datasets = []
# 1. Breast Cancer (small)
X, y = load_breast_cancer(return_X_y=True)
datasets.append({
'X': X.astype(np.float32), 'y': y.astype(np.float32),
'name': 'Breast Cancer', 'samples': len(X), 'features': X.shape[1]
})
# 2. Small synthetic
X, y = make_classification(n_samples=5000, n_features=20, n_informative=15,
n_redundant=3, random_state=42)
datasets.append({
'X': X.astype(np.float32), 'y': y.astype(np.float32),
'name': 'Small (5K)', 'samples': 5000, 'features': 20
})
# 3. Medium
X, y = make_classification(n_samples=20000, n_features=30, n_informative=20,
n_redundant=5, random_state=42)
datasets.append({
'X': X.astype(np.float32), 'y': y.astype(np.float32),
'name': 'Medium (20K)', 'samples': 20000, 'features': 30
})
# 4. Large
X, y = make_classification(n_samples=50000, n_features=40, n_informative=25,
n_redundant=10, random_state=42)
datasets.append({
'X': X.astype(np.float32), 'y': y.astype(np.float32),
'name': 'Large (50K)', 'samples': 50000, 'features': 40
})
# 5. Imbalanced
X, y = make_classification(n_samples=30000, n_features=25, n_informative=15,
n_redundant=5, weights=[0.9, 0.1], random_state=42)
datasets.append({
'X': X.astype(np.float32), 'y': y.astype(np.float32),
'name': 'Imbalanced', 'samples': 30000, 'features': 25
})
# 6. High-dimensional
X, y = make_classification(n_samples=10000, n_features=100, n_informative=50,
n_redundant=20, random_state=42)
datasets.append({
'X': X.astype(np.float32), 'y': y.astype(np.float32),
'name': 'High-Dim', 'samples': 10000, 'features': 100
})
return datasets
def benchmark_tc(X_train, y_train, X_test, y_test, n_runs=3):
train_times, inf_times = [], []
for _ in range(n_runs):
model = tc.TurboCatClassifier(
n_estimators=500,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
mode="small", # Regular trees for best quality (symmetric slower to train)
verbosity=0
)
start = time.perf_counter()
model.fit(X_train, y_train)
train_times.append(time.perf_counter() - start)
_ = model.predict_proba(X_test[:100])
start = time.perf_counter()
proba = model.predict_proba(X_test)
inf_times.append(time.perf_counter() - start)
pred = model.predict(X_test)
proba = model.predict_proba(X_test)
return {
'auc': roc_auc_score(y_test, proba[:, 1]),
'accuracy': accuracy_score(y_test, pred),
'precision': precision_score(y_test, pred),
'recall': recall_score(y_test, pred),
'f1': f1_score(y_test, pred),
'logloss': log_loss(y_test, proba[:, 1]),
'train_time': np.median(train_times),
'inf_time': np.median(inf_times) * 1000
}
def benchmark_cb(X_train, y_train, X_test, y_test, n_runs=3):
train_times, inf_times = [], []
for _ in range(n_runs):
model = CatBoostClassifier(
n_estimators=500,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
verbose=False,
thread_count=-1
)
start = time.perf_counter()
model.fit(X_train, y_train)
train_times.append(time.perf_counter() - start)
_ = model.predict_proba(X_test[:100])
start = time.perf_counter()
proba = model.predict_proba(X_test)
inf_times.append(time.perf_counter() - start)
pred = model.predict(X_test)
proba = model.predict_proba(X_test)
return {
'auc': roc_auc_score(y_test, proba[:, 1]),
'accuracy': accuracy_score(y_test, pred),
'precision': precision_score(y_test, pred),
'recall': recall_score(y_test, pred),
'f1': f1_score(y_test, pred),
'logloss': log_loss(y_test, proba[:, 1]),
'train_time': np.median(train_times),
'inf_time': np.median(inf_times) * 1000
}
def main():
print("=" * 100)
print(" TURBOCAT vs CATBOOST - FINAL COMPREHENSIVE BENCHMARK")
print("=" * 100)
print("\nParameters: n_estimators=500, max_depth=6, learning_rate=0.1, subsample=0.8")
datasets = create_datasets()
all_results = []
total_wins = {'quality': {'TC': 0, 'CB': 0}, 'speed': {'TC': 0, 'CB': 0}}
for data in datasets:
print(f"\n{'='*100}")
print(f" {data['name']} ({data['samples']:,} samples, {data['features']} features)")
print("=" * 100)
X_train, X_test, y_train, y_test = train_test_split(
data['X'], data['y'], test_size=0.2, random_state=42, stratify=data['y']
)
print(" Benchmarking TurboCat...", end=" ", flush=True)
tc_res = benchmark_tc(X_train, y_train, X_test, y_test)
print(f"done ({tc_res['train_time']:.2f}s)")
print(" Benchmarking CatBoost...", end=" ", flush=True)
cb_res = benchmark_cb(X_train, y_train, X_test, y_test)
print(f"done ({cb_res['train_time']:.2f}s)")
# Quality metrics
print(f"\n {'QUALITY METRICS':-^70}")
print(f" {'Metric':<15} {'TurboCat':>12} {'CatBoost':>12} {'Diff':>10} {'Winner':>10}")
print(f" {'-'*60}")
quality_wins = {'TC': 0, 'CB': 0}
for metric in ['auc', 'accuracy', 'precision', 'recall', 'f1']:
tc_val = tc_res[metric]
cb_val = cb_res[metric]
diff = (tc_val - cb_val) * 100
winner = 'TC' if tc_val >= cb_val else 'CB'
quality_wins[winner] += 1
label = {'auc': 'ROC-AUC', 'accuracy': 'Accuracy', 'precision': 'Precision',
'recall': 'Recall', 'f1': 'F1'}[metric]
print(f" {label:<15} {tc_val:>12.4f} {cb_val:>12.4f} {diff:>+9.2f}% {winner:>10}")
# LogLoss (lower is better)
tc_ll, cb_ll = tc_res['logloss'], cb_res['logloss']
diff = (cb_ll - tc_ll) / cb_ll * 100
winner = 'TC' if tc_ll <= cb_ll else 'CB'
quality_wins[winner] += 1
print(f" {'LogLoss':<15} {tc_ll:>12.4f} {cb_ll:>12.4f} {diff:>+9.2f}% {winner:>10}")
# Speed metrics
print(f"\n {'SPEED METRICS':-^70}")
speed_wins = {'TC': 0, 'CB': 0}
# Train (lower is better)
tc_train, cb_train = tc_res['train_time'], cb_res['train_time']
ratio = cb_train / tc_train
winner = 'TC' if tc_train <= cb_train else 'CB'
speed_wins[winner] += 1
print(f" {'Train Time':<15} {tc_train:>11.3f}s {cb_train:>11.3f}s {ratio:>9.2f}x {winner:>10}")
# Inference (lower is better)
tc_inf, cb_inf = tc_res['inf_time'], cb_res['inf_time']
ratio = cb_inf / tc_inf
winner = 'TC' if tc_inf <= cb_inf else 'CB'
speed_wins[winner] += 1
print(f" {'Inference':<15} {tc_inf:>10.2f}ms {cb_inf:>10.2f}ms {ratio:>9.2f}x {winner:>10}")
# Throughput
tc_train_tput = data['samples'] * 0.8 / tc_res['train_time']
cb_train_tput = data['samples'] * 0.8 / cb_res['train_time']
tc_inf_tput = data['samples'] * 0.2 / (tc_res['inf_time'] / 1000)
cb_inf_tput = data['samples'] * 0.2 / (cb_res['inf_time'] / 1000)
print(f" {'Train samples/s':<15} {tc_train_tput:>11,.0f} {cb_train_tput:>11,.0f}")
print(f" {'Inf samples/s':<15} {tc_inf_tput:>11,.0f} {cb_inf_tput:>11,.0f}")
print(f"\n Summary: Quality TC={quality_wins['TC']}/6, Speed TC={speed_wins['TC']}/2")
total_wins['quality']['TC'] += quality_wins['TC']
total_wins['quality']['CB'] += quality_wins['CB']
total_wins['speed']['TC'] += speed_wins['TC']
total_wins['speed']['CB'] += speed_wins['CB']
all_results.append({
'name': data['name'],
'tc_auc': tc_res['auc'],
'cb_auc': cb_res['auc'],
'tc_train': tc_res['train_time'],
'cb_train': cb_res['train_time'],
'tc_inf': tc_res['inf_time'],
'cb_inf': cb_res['inf_time']
})
# Final Summary
print("\n" + "=" * 100)
print(" FINAL SUMMARY")
print("=" * 100)
print(f"\n{'Dataset':<20} {'TC AUC':>10} {'CB AUC':>10} {'AUC Diff':>10} {'Train':>10} {'Inference':>12}")
print("-" * 80)
for r in all_results:
auc_diff = (r['tc_auc'] - r['cb_auc']) * 100
train_ratio = r['cb_train'] / r['tc_train']
inf_ratio = r['cb_inf'] / r['tc_inf']
winner_sym = '✓' if r['tc_auc'] >= r['cb_auc'] else ' '
print(f"{r['name']:<20} {r['tc_auc']:>10.4f} {r['cb_auc']:>10.4f} {auc_diff:>+9.2f}% {train_ratio:>9.2f}x {inf_ratio:>11.2f}x {winner_sym}")
print("-" * 80)
print(f"\nTOTAL WINS ACROSS ALL DATASETS:")
print(f" Quality Metrics: TurboCat = {total_wins['quality']['TC']}, CatBoost = {total_wins['quality']['CB']}")
print(f" Speed Metrics: TurboCat = {total_wins['speed']['TC']}, CatBoost = {total_wins['speed']['CB']}")
tc_total = total_wins['quality']['TC'] + total_wins['speed']['TC']
cb_total = total_wins['quality']['CB'] + total_wins['speed']['CB']
print(f"\n OVERALL SCORE: TurboCat = {tc_total}, CatBoost = {cb_total}")
if tc_total > cb_total:
print("\n " + "🏆" * 10)
print(" 🏆 TURBOCAT WINS! 🏆")
print(" " + "🏆" * 10)
elif cb_total > tc_total:
print("\n CatBoost wins this round.")
else:
print("\n It's a tie!")
if __name__ == "__main__":
main()