use hessboost::metric::EvalMetric;
use hessboost::prelude::*;
mod common;
use common::lcg;
fn main() -> Result<()> {
let n_rows = 2000;
let n_cols = 4;
let mut x = Vec::with_capacity(n_rows * n_cols);
let mut y = Vec::with_capacity(n_rows);
let mut next = lcg(0x1234_5678);
for _ in 0..n_rows {
let x0 = next();
let x1 = next();
let x2 = next();
let x3 = next();
x.extend_from_slice(&[x0, x1, x2, x3]);
y.push(2.0 * x0 - 3.0 * x1 * x1 + 0.5 * x2);
}
let dtrain = DMatrix::from_dense(&x, n_rows, n_cols)?.with_labels(&y)?;
let params = TrainingParams::builder()
.objective(Objective::SquaredError(RegLoss::default()))
.max_depth(4)
.eta(0.1)
.subsample(0.9)
.colsample_bytree(1.0)
.lambda(1.0)
.build()?;
let model = train(¶ms, &dtrain, 200)?;
let preds = model.predict(&dtrain, Iterations::Best)?;
let rmse = EvalMetric::Rmse
.build(1)?
.eval(preds.as_slice(), dtrain.labels().unwrap(), None);
println!("trained {} trees", model.num_trees());
println!("training RMSE: {rmse:.5}");
let importance = model.feature_importance(ImportanceType::Weight);
let mut imp: Vec<_> = importance.into_iter().collect();
imp.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
println!("feature importance (split count):");
for (feat, score) in imp {
println!(" feature {feat}: {score}");
}
Ok(())
}