scikit-learn: classification¶
SVM pipeline tuned with GridSearchCV on the breast-cancer dataset, scored on a held-out split. Records best cross-validation score, test accuracy, F1.
| Framework | scikit-learn |
| Task | Tabular classification |
Install¶
Plus the K-Veritas CLI.
train.py¶
Only the KV lines are new.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, f1_score
SEED = 42
print(f"KV seed {SEED}")
print("KV phase data")
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=SEED, stratify=y)
print(f"KV workload dataset_size={len(X_train)} epochs=1 batch_size={len(X_train)}")
print("KV phase search")
pipe = make_pipeline(StandardScaler(), SVC())
grid = {"svc__C": [0.1, 1, 10], "svc__gamma": ["scale", 0.01, 0.001]}
search = GridSearchCV(pipe, grid, cv=5, scoring="accuracy", n_jobs=-1)
search.fit(X_train, y_train)
print(f"KV cv_best_accuracy={search.best_score_:.4f}")
print(f"best params: {search.best_params_}", flush=True)
print("KV phase evaluate")
pred = search.predict(X_test)
acc = accuracy_score(y_test, pred)
f1 = f1_score(y_test, pred)
print(f"KV test_accuracy={acc:.4f}")
print(f"KV test_f1={f1:.4f}")
print(f"KV claim test_accuracy={acc:.4f}")
print(f"test_accuracy={acc:.4f} test_f1={f1:.4f}", flush=True)
Run it¶
Result¶
About 98.3% test accuracy, 0.986 F1.