-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_benchmark.py
More file actions
210 lines (168 loc) · 7.54 KB
/
final_benchmark.py
File metadata and controls
210 lines (168 loc) · 7.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
"""Final benchmark: TurboCat vs CatBoost - Speed and Quality"""
import numpy as np
import time
from sklearn.metrics import roc_auc_score, accuracy_score, f1_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer, load_digits, make_classification
import warnings
warnings.filterwarnings('ignore')
import sys
sys.path.insert(0, 'build')
from turbocat import TurboCatClassifier
from catboost import CatBoostClassifier
def run_benchmark(X, y, name, n_trees=100, max_depth=6):
"""Run benchmark on a single dataset"""
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
results = {'name': name, 'n_samples': len(X), 'n_features': X.shape[1]}
# TurboCat
clf_tc = TurboCatClassifier(
n_estimators=n_trees,
max_depth=max_depth,
learning_rate=0.1,
use_goss=True,
verbosity=0,
n_jobs=-1
)
start = time.perf_counter()
clf_tc.fit(X_train, y_train.astype(np.float32))
results['tc_train'] = time.perf_counter() - start
start = time.perf_counter()
tc_proba = clf_tc.predict_proba(X_test)
results['tc_infer'] = (time.perf_counter() - start) * 1000 # ms
if tc_proba.ndim == 2:
tc_pred = (tc_proba[:, 1] > 0.5).astype(int)
tc_proba_1 = tc_proba[:, 1]
else:
tc_pred = (tc_proba > 0.5).astype(int)
tc_proba_1 = tc_proba
results['tc_acc'] = accuracy_score(y_test, tc_pred)
results['tc_f1'] = f1_score(y_test, tc_pred, average='weighted')
try:
results['tc_auc'] = roc_auc_score(y_test, tc_proba_1)
except:
results['tc_auc'] = 0
# CatBoost
clf_cb = CatBoostClassifier(
n_estimators=n_trees,
max_depth=max_depth,
learning_rate=0.1,
verbose=0
)
start = time.perf_counter()
clf_cb.fit(X_train, y_train.astype(int))
results['cb_train'] = time.perf_counter() - start
start = time.perf_counter()
cb_proba = clf_cb.predict_proba(X_test)
results['cb_infer'] = (time.perf_counter() - start) * 1000 # ms
cb_pred = clf_cb.predict(X_test)
results['cb_acc'] = accuracy_score(y_test, cb_pred)
results['cb_f1'] = f1_score(y_test, cb_pred, average='weighted')
try:
results['cb_auc'] = roc_auc_score(y_test, cb_proba[:, 1])
except:
results['cb_auc'] = 0
return results
def main():
print("=" * 100)
print("ФИНАЛЬНЫЙ БЕНЧМАРК: TurboCat vs CatBoost")
print("=" * 100)
datasets = []
# 1. Breast Cancer (small, binary)
data = load_breast_cancer()
datasets.append(("Breast Cancer", data.data.astype(np.float32), data.target))
# 2. Synthetic small
X, y = make_classification(n_samples=2000, n_features=20, n_informative=10,
n_redundant=5, random_state=42)
datasets.append(("Synthetic 2K", X.astype(np.float32), y))
# 3. Synthetic medium
X, y = make_classification(n_samples=10000, n_features=30, n_informative=15,
n_redundant=10, random_state=42)
datasets.append(("Synthetic 10K", X.astype(np.float32), y))
# 4. Synthetic large
X, y = make_classification(n_samples=50000, n_features=50, n_informative=25,
n_redundant=15, random_state=42)
datasets.append(("Synthetic 50K", X.astype(np.float32), y))
# 5. Imbalanced 95/5
X, y = make_classification(n_samples=5000, n_features=20, n_informative=10,
weights=[0.95, 0.05], random_state=42)
datasets.append(("Imbalanced 95/5", X.astype(np.float32), y))
# 6. Imbalanced 99/1
X, y = make_classification(n_samples=5000, n_features=20, n_informative=10,
weights=[0.99, 0.01], random_state=42)
datasets.append(("Imbalanced 99/1", X.astype(np.float32), y))
# 7. High-dimensional
X, y = make_classification(n_samples=3000, n_features=100, n_informative=50,
n_redundant=30, random_state=42)
datasets.append(("High-Dim 100f", X.astype(np.float32), y))
all_results = []
for name, X, y in datasets:
results = run_benchmark(X, y, name)
all_results.append(results)
# Print Speed Table
print("\n" + "=" * 100)
print("СКОРОСТЬ (Train / Inference)")
print("=" * 100)
print(f"{'Датасет':<20} {'Samples':<10} {'TC Train':<12} {'CB Train':<12} {'Speedup':<10} {'TC Infer':<12} {'CB Infer':<12} {'Speedup':<10}")
print("-" * 100)
total_tc_train = 0
total_cb_train = 0
total_tc_infer = 0
total_cb_infer = 0
for r in all_results:
train_speedup = r['cb_train'] / r['tc_train']
infer_speedup = r['cb_infer'] / r['tc_infer']
total_tc_train += r['tc_train']
total_cb_train += r['cb_train']
total_tc_infer += r['tc_infer']
total_cb_infer += r['cb_infer']
print(f"{r['name']:<20} {r['n_samples']:<10} {r['tc_train']:<12.4f} {r['cb_train']:<12.4f} {train_speedup:<10.2f}x {r['tc_infer']:<12.2f} {r['cb_infer']:<12.2f} {infer_speedup:<10.2f}x")
print("-" * 100)
overall_train = total_cb_train / total_tc_train
overall_infer = total_cb_infer / total_tc_infer
print(f"{'ИТОГО':<20} {'':<10} {total_tc_train:<12.4f} {total_cb_train:<12.4f} {overall_train:<10.2f}x {total_tc_infer:<12.2f} {total_cb_infer:<12.2f} {overall_infer:<10.2f}x")
# Print Quality Table
print("\n" + "=" * 100)
print("КАЧЕСТВО (Accuracy / F1 / ROC-AUC)")
print("=" * 100)
print(f"{'Датасет':<20} {'TC Acc':<10} {'CB Acc':<10} {'TC F1':<10} {'CB F1':<10} {'TC AUC':<10} {'CB AUC':<10} {'Winner':<15}")
print("-" * 100)
tc_wins = 0
cb_wins = 0
ties = 0
for r in all_results:
# Determine winner based on AUC
if r['tc_auc'] > r['cb_auc'] + 0.001:
winner = "TurboCat"
tc_wins += 1
elif r['cb_auc'] > r['tc_auc'] + 0.001:
winner = "CatBoost"
cb_wins += 1
else:
winner = "Tie"
ties += 1
print(f"{r['name']:<20} {r['tc_acc']:<10.4f} {r['cb_acc']:<10.4f} {r['tc_f1']:<10.4f} {r['cb_f1']:<10.4f} {r['tc_auc']:<10.4f} {r['cb_auc']:<10.4f} {winner:<15}")
print("-" * 100)
avg_tc_acc = np.mean([r['tc_acc'] for r in all_results])
avg_cb_acc = np.mean([r['cb_acc'] for r in all_results])
avg_tc_f1 = np.mean([r['tc_f1'] for r in all_results])
avg_cb_f1 = np.mean([r['cb_f1'] for r in all_results])
avg_tc_auc = np.mean([r['tc_auc'] for r in all_results])
avg_cb_auc = np.mean([r['cb_auc'] for r in all_results])
print(f"{'СРЕДНЕЕ':<20} {avg_tc_acc:<10.4f} {avg_cb_acc:<10.4f} {avg_tc_f1:<10.4f} {avg_cb_f1:<10.4f} {avg_tc_auc:<10.4f} {avg_cb_auc:<10.4f}")
# Summary
print("\n" + "=" * 100)
print("ИТОГОВЫЙ ВЕРДИКТ")
print("=" * 100)
print(f"\n📊 СКОРОСТЬ:")
print(f" • Training: TurboCat в {overall_train:.2f}x {'БЫСТРЕЕ' if overall_train > 1 else 'медленнее'}")
print(f" • Inference: TurboCat в {overall_infer:.2f}x {'БЫСТРЕЕ' if overall_infer > 1 else 'медленнее'}")
print(f"\n🎯 КАЧЕСТВО:")
print(f" • TurboCat побед: {tc_wins}")
print(f" • CatBoost побед: {cb_wins}")
print(f" • Ничьих: {ties}")
print(f" • Средний AUC: TurboCat {avg_tc_auc:.4f} vs CatBoost {avg_cb_auc:.4f} (разница: {(avg_tc_auc - avg_cb_auc)*100:+.2f}%)")
if __name__ == "__main__":
main()