LightGBM: covertype¶
LightGBM with early stopping on a subsample of the real Covertype forest-cover dataset. Shows the same tabular pattern on a larger, multiclass problem.
| Framework | LightGBM |
| Task | Tabular classification |
Install¶
Plus the K-Veritas CLI.
train.py¶
Only the KVERITAS_ lines are new.
import numpy as np
from sklearn.datasets import fetch_covtype
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import lightgbm as lgb
SEED = 42
print(f"KVERITAS_INPUT src=seed:{SEED}", flush=True)
print("KVERITAS_PHASE name=data", flush=True)
X, y = fetch_covtype(return_X_y=True)
y = y - 1
rng = np.random.default_rng(SEED)
idx = rng.choice(len(X), size=40000, replace=False)
X, y = X[idx], y[idx]
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 = lgb.LGBMClassifier(n_estimators=300, learning_rate=0.1, num_leaves=63,
subsample=0.9, colsample_bytree=0.9, random_state=SEED, verbose=-1)
model.fit(X_tr, y_tr, eval_set=[(X_val, y_val)],
callbacks=[lgb.early_stopping(20, 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 it¶
kveritas init
kveritas run -- python train.py
kveritas seal -o report.pdf
kveritas verify report.pdf
Result¶
Early stopping on a 40,000-row Covertype subsample reached about 87.3% test accuracy across the seven forest-cover types.