scikit-learn: regression
Gradient-boosted regression on California housing. Records RMSE and R2 on the held-out split and commits R2 as the claim.
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.
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.metrics import mean_squared_error, r2_score
SEED = 42
print(f"KVERITAS_INPUT src=seed:{SEED}", flush=True)
print("KVERITAS_PHASE name=data", flush=True)
X, y = fetch_california_housing(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=SEED)
print(f"KVERITAS_WORKLOAD dataset_size={len(X_train)} epochs=1 batch_size={len(X_train)}", flush=True)
print("KVERITAS_PHASE name=train", flush=True)
model = HistGradientBoostingRegressor(max_iter=300, learning_rate=0.08, random_state=SEED)
model.fit(X_train, y_train)
print("KVERITAS_PHASE name=evaluate", flush=True)
pred = model.predict(X_test)
rmse = float(np.sqrt(mean_squared_error(y_test, pred)))
r2 = r2_score(y_test, pred)
print(f"KVERITAS_METRIC name=test_rmse value={rmse:.4f}", flush=True)
print(f"KVERITAS_METRIC name=test_r2 value={r2:.4f}", flush=True)
print(f"KVERITAS_CLAIM metric=test_r2 value={r2:.4f}", flush=True)
print(f"test_rmse={rmse:.4f} test_r2={r2:.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
Reached R2 of about 0.848 (RMSE about 0.45) on the held-out split.