K-Veritas
Tutorial · scikit-learn

scikit-learn: classification

An SVM pipeline tuned with GridSearchCV on the breast-cancer dataset, then scored on a held-out split. Records the cross-validation best score and the test accuracy and F1.

Install

The framework, plus the K-Veritas CLI (see the Overview for install).

pip install scikit-learn

train.py

The complete script. The KVERITAS_ lines are the only additions to an ordinary training script; everything else is standard scikit-learn.

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"KVERITAS_INPUT src=seed:{SEED}", flush=True)

print("KVERITAS_PHASE name=data", flush=True)
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"KVERITAS_WORKLOAD dataset_size={len(X_train)} epochs=1 batch_size={len(X_train)}", flush=True)

print("KVERITAS_PHASE name=search", flush=True)
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"KVERITAS_METRIC name=cv_best_accuracy value={search.best_score_:.4f}", flush=True)
print(f"best params: {search.best_params_}", flush=True)

print("KVERITAS_PHASE name=evaluate", flush=True)
pred = search.predict(X_test)
acc = accuracy_score(y_test, pred)
f1 = f1_score(y_test, pred)
print(f"KVERITAS_METRIC name=test_accuracy value={acc:.4f}", flush=True)
print(f"KVERITAS_METRIC name=test_f1 value={f1:.4f}", flush=True)
print(f"KVERITAS_CLAIM metric=test_accuracy value={acc:.4f}", flush=True)
print(f"test_accuracy={acc:.4f}  test_f1={f1:.4f}", flush=True)

Run and seal

Wrap the script, seal a signed report, and verify it offline.

kveritas init
kveritas run -- python train.py
kveritas seal --output report.pdf
kveritas verify report.pdf

What it produced here

GridSearchCV over the SVM reached about 98.3% test accuracy and 0.986 F1.

All tutorials · The directives explained