Skip to content

XGBoost: digits

XGBoost with early stopping on the digits dataset. Records the best round and test accuracy.

Framework XGBoost
Task Tabular classification

Install

pip install xgboost scikit-learn

Plus the K-Veritas CLI.

train.py

Only the KV lines are new.

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"KV seed {SEED}")

print("KV phase data")
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"KV workload dataset_size={len(X_tr)} epochs=300 batch_size={len(X_tr)}")

print("KV phase train")
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"KV best_iteration={model.best_iteration}")

print("KV phase evaluate")
acc = accuracy_score(y_test, model.predict(X_test))
print(f"KV test_accuracy={acc:.4f}")
print(f"KV claim test_accuracy={acc:.4f}")
print(f"test_accuracy={acc:.4f}", flush=True)

Run it

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

Result

About 95.6% test accuracy.