Skip to content

LightGBM: covertype

LightGBM with early stopping on a Covertype subsample (forest cover, seven classes).

Framework LightGBM
Task Tabular classification

Install

pip install lightgbm scikit-learn

Plus the K-Veritas CLI.

train.py

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

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

print("KV phase train")
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"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

40,000-row subsample: about 87.3% test accuracy.