K-Veritas
Tutorial · XGBoost

XGBoost: digits

XGBoost with early stopping on the digits dataset, using a validation split to pick the best iteration. Records the chosen iteration and the test accuracy.

Install

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

pip install xgboost scikit-learn

train.py

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

from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import xgboost as xgb

SEED = 42
print(f"KVERITAS_INPUT src=seed:{SEED}", flush=True)

print("KVERITAS_PHASE name=data", flush=True)
X, y = load_digits(return_X_y=True)
X_tr, X_tmp, y_tr, y_tmp = train_test_split(X, y, test_size=0.3, random_state=SEED, stratify=y)
X_val, X_test, y_val, y_test = train_test_split(X_tmp, y_tmp, test_size=0.5, random_state=SEED, stratify=y_tmp)
print(f"KVERITAS_WORKLOAD dataset_size={len(X_tr)} epochs=300 batch_size={len(X_tr)}", flush=True)

print("KVERITAS_PHASE name=train", flush=True)
model = xgb.XGBClassifier(
    n_estimators=300, max_depth=4, learning_rate=0.1,
    subsample=0.9, colsample_bytree=0.9, eval_metric="mlogloss",
    early_stopping_rounds=20, random_state=SEED,
)
model.fit(X_tr, y_tr, eval_set=[(X_val, y_val)], verbose=False)
print(f"KVERITAS_METRIC name=best_iteration value={model.best_iteration}", flush=True)

print("KVERITAS_PHASE name=evaluate", flush=True)
acc = accuracy_score(y_test, model.predict(X_test))
print(f"KVERITAS_METRIC name=test_accuracy value={acc:.4f}", flush=True)
print(f"KVERITAS_CLAIM metric=test_accuracy value={acc:.4f}", flush=True)
print(f"test_accuracy={acc:.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

Early stopping picked the best iteration and reached about 95.6% test accuracy.

All tutorials · The directives explained