use ferrolearn_core::error::FerroError;
use ferrolearn_core::introspection::{HasClasses, HasFeatureImportances};
use ferrolearn_core::pipeline::{FittedPipelineEstimator, PipelineEstimator};
use ferrolearn_core::traits::{Fit, Predict};
use ndarray::{Array1, Array2};
use num_traits::{Float, FromPrimitive, ToPrimitive};
use rand::SeedableRng;
use rand::rngs::StdRng;
use rand::seq::index::sample as rand_sample_indices;
use crate::decision_tree::{
self, Node, build_regression_tree_with_feature_subset, compute_feature_importances,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegressionLoss {
LeastSquares,
Lad,
Huber,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClassificationLoss {
LogLoss,
}
#[derive(Debug, Clone)]
pub struct GradientBoostingRegressor<F> {
pub n_estimators: usize,
pub learning_rate: f64,
pub max_depth: Option<usize>,
pub min_samples_split: usize,
pub min_samples_leaf: usize,
pub subsample: f64,
pub loss: RegressionLoss,
pub huber_alpha: f64,
pub random_state: Option<u64>,
_marker: std::marker::PhantomData<F>,
}
impl<F: Float> GradientBoostingRegressor<F> {
#[must_use]
pub fn new() -> Self {
Self {
n_estimators: 100,
learning_rate: 0.1,
max_depth: Some(3),
min_samples_split: 2,
min_samples_leaf: 1,
subsample: 1.0,
loss: RegressionLoss::LeastSquares,
huber_alpha: 0.9,
random_state: None,
_marker: std::marker::PhantomData,
}
}
#[must_use]
pub fn with_n_estimators(mut self, n: usize) -> Self {
self.n_estimators = n;
self
}
#[must_use]
pub fn with_learning_rate(mut self, lr: f64) -> Self {
self.learning_rate = lr;
self
}
#[must_use]
pub fn with_max_depth(mut self, d: Option<usize>) -> Self {
self.max_depth = d;
self
}
#[must_use]
pub fn with_min_samples_split(mut self, n: usize) -> Self {
self.min_samples_split = n;
self
}
#[must_use]
pub fn with_min_samples_leaf(mut self, n: usize) -> Self {
self.min_samples_leaf = n;
self
}
#[must_use]
pub fn with_subsample(mut self, ratio: f64) -> Self {
self.subsample = ratio;
self
}
#[must_use]
pub fn with_loss(mut self, loss: RegressionLoss) -> Self {
self.loss = loss;
self
}
#[must_use]
pub fn with_huber_alpha(mut self, alpha: f64) -> Self {
self.huber_alpha = alpha;
self
}
#[must_use]
pub fn with_random_state(mut self, seed: u64) -> Self {
self.random_state = Some(seed);
self
}
}
impl<F: Float> Default for GradientBoostingRegressor<F> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct FittedGradientBoostingRegressor<F> {
init: F,
learning_rate: F,
trees: Vec<Vec<Node<F>>>,
n_features: usize,
feature_importances: Array1<F>,
}
impl<F: Float + Send + Sync + 'static> Fit<Array2<F>, Array1<F>> for GradientBoostingRegressor<F> {
type Fitted = FittedGradientBoostingRegressor<F>;
type Error = FerroError;
fn fit(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<FittedGradientBoostingRegressor<F>, FerroError> {
let (n_samples, n_features) = x.dim();
if n_samples != y.len() {
return Err(FerroError::ShapeMismatch {
expected: vec![n_samples],
actual: vec![y.len()],
context: "y length must match number of samples in X".into(),
});
}
if n_samples == 0 {
return Err(FerroError::InsufficientSamples {
required: 1,
actual: 0,
context: "GradientBoostingRegressor requires at least one sample".into(),
});
}
if self.n_estimators == 0 {
return Err(FerroError::InvalidParameter {
name: "n_estimators".into(),
reason: "must be at least 1".into(),
});
}
if self.learning_rate <= 0.0 {
return Err(FerroError::InvalidParameter {
name: "learning_rate".into(),
reason: "must be positive".into(),
});
}
if self.subsample <= 0.0 || self.subsample > 1.0 {
return Err(FerroError::InvalidParameter {
name: "subsample".into(),
reason: "must be in (0, 1]".into(),
});
}
let lr = F::from(self.learning_rate).unwrap();
let params = decision_tree::TreeParams {
max_depth: self.max_depth,
min_samples_split: self.min_samples_split,
min_samples_leaf: self.min_samples_leaf,
};
let init = match self.loss {
RegressionLoss::LeastSquares => {
let sum: F = y.iter().copied().fold(F::zero(), |a, b| a + b);
sum / F::from(n_samples).unwrap()
}
RegressionLoss::Lad | RegressionLoss::Huber => median_f(y),
};
let mut f_vals = Array1::from_elem(n_samples, init);
let all_features: Vec<usize> = (0..n_features).collect();
let subsample_size = ((self.subsample * n_samples as f64).ceil() as usize)
.max(1)
.min(n_samples);
let mut rng = if let Some(seed) = self.random_state {
StdRng::seed_from_u64(seed)
} else {
use rand::RngCore;
StdRng::seed_from_u64(rand::rng().next_u64())
};
let mut trees = Vec::with_capacity(self.n_estimators);
for _ in 0..self.n_estimators {
let residuals = compute_regression_residuals(y, &f_vals, self.loss, self.huber_alpha);
let sample_indices = if subsample_size < n_samples {
rand_sample_indices(&mut rng, n_samples, subsample_size).into_vec()
} else {
(0..n_samples).collect()
};
let tree = build_regression_tree_with_feature_subset(
x,
&residuals,
&sample_indices,
&all_features,
¶ms,
);
for i in 0..n_samples {
let row = x.row(i);
let leaf_idx = decision_tree::traverse(&tree, &row);
if let Node::Leaf { value, .. } = tree[leaf_idx] {
f_vals[i] = f_vals[i] + lr * value;
}
}
trees.push(tree);
}
let mut total_importances = Array1::<F>::zeros(n_features);
for tree_nodes in &trees {
let tree_imp = compute_feature_importances(tree_nodes, n_features, n_samples);
total_importances = total_importances + tree_imp;
}
let imp_sum: F = total_importances
.iter()
.copied()
.fold(F::zero(), |a, b| a + b);
if imp_sum > F::zero() {
total_importances.mapv_inplace(|v| v / imp_sum);
}
Ok(FittedGradientBoostingRegressor {
init,
learning_rate: lr,
trees,
n_features,
feature_importances: total_importances,
})
}
}
impl<F: Float + Send + Sync + 'static> FittedGradientBoostingRegressor<F> {
#[must_use]
pub fn init(&self) -> F {
self.init
}
#[must_use]
pub fn learning_rate(&self) -> F {
self.learning_rate
}
#[must_use]
pub fn trees(&self) -> &[Vec<Node<F>>] {
&self.trees
}
#[must_use]
pub fn n_features(&self) -> usize {
self.n_features
}
pub fn score(&self, x: &Array2<F>, y: &Array1<F>) -> Result<F, FerroError> {
if x.nrows() != y.len() {
return Err(FerroError::ShapeMismatch {
expected: vec![x.nrows()],
actual: vec![y.len()],
context: "y length must match number of samples in X".into(),
});
}
let preds = self.predict(x)?;
Ok(crate::r2_score(&preds, y))
}
}
impl<F: Float + Send + Sync + 'static> Predict<Array2<F>> for FittedGradientBoostingRegressor<F> {
type Output = Array1<F>;
type Error = FerroError;
fn predict(&self, x: &Array2<F>) -> Result<Array1<F>, FerroError> {
if x.ncols() != self.n_features {
return Err(FerroError::ShapeMismatch {
expected: vec![self.n_features],
actual: vec![x.ncols()],
context: "number of features must match fitted model".into(),
});
}
let n_samples = x.nrows();
let mut predictions = Array1::from_elem(n_samples, self.init);
for i in 0..n_samples {
let row = x.row(i);
for tree_nodes in &self.trees {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
predictions[i] = predictions[i] + self.learning_rate * value;
}
}
}
Ok(predictions)
}
}
impl<F: Float + Send + Sync + 'static> HasFeatureImportances<F>
for FittedGradientBoostingRegressor<F>
{
fn feature_importances(&self) -> &Array1<F> {
&self.feature_importances
}
}
impl<F: Float + Send + Sync + 'static> PipelineEstimator<F> for GradientBoostingRegressor<F> {
fn fit_pipeline(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError> {
let fitted = self.fit(x, y)?;
Ok(Box::new(fitted))
}
}
impl<F: Float + Send + Sync + 'static> FittedPipelineEstimator<F>
for FittedGradientBoostingRegressor<F>
{
fn predict_pipeline(&self, x: &Array2<F>) -> Result<Array1<F>, FerroError> {
self.predict(x)
}
}
#[derive(Debug, Clone)]
pub struct GradientBoostingClassifier<F> {
pub n_estimators: usize,
pub learning_rate: f64,
pub max_depth: Option<usize>,
pub min_samples_split: usize,
pub min_samples_leaf: usize,
pub subsample: f64,
pub loss: ClassificationLoss,
pub random_state: Option<u64>,
_marker: std::marker::PhantomData<F>,
}
impl<F: Float> GradientBoostingClassifier<F> {
#[must_use]
pub fn new() -> Self {
Self {
n_estimators: 100,
learning_rate: 0.1,
max_depth: Some(3),
min_samples_split: 2,
min_samples_leaf: 1,
subsample: 1.0,
loss: ClassificationLoss::LogLoss,
random_state: None,
_marker: std::marker::PhantomData,
}
}
#[must_use]
pub fn with_n_estimators(mut self, n: usize) -> Self {
self.n_estimators = n;
self
}
#[must_use]
pub fn with_learning_rate(mut self, lr: f64) -> Self {
self.learning_rate = lr;
self
}
#[must_use]
pub fn with_max_depth(mut self, d: Option<usize>) -> Self {
self.max_depth = d;
self
}
#[must_use]
pub fn with_min_samples_split(mut self, n: usize) -> Self {
self.min_samples_split = n;
self
}
#[must_use]
pub fn with_min_samples_leaf(mut self, n: usize) -> Self {
self.min_samples_leaf = n;
self
}
#[must_use]
pub fn with_subsample(mut self, ratio: f64) -> Self {
self.subsample = ratio;
self
}
#[must_use]
pub fn with_random_state(mut self, seed: u64) -> Self {
self.random_state = Some(seed);
self
}
}
impl<F: Float> Default for GradientBoostingClassifier<F> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct FittedGradientBoostingClassifier<F> {
classes: Vec<usize>,
init: Vec<F>,
learning_rate: F,
trees: Vec<Vec<Vec<Node<F>>>>,
n_features: usize,
feature_importances: Array1<F>,
}
impl<F: Float + Send + Sync + 'static> Fit<Array2<F>, Array1<usize>>
for GradientBoostingClassifier<F>
{
type Fitted = FittedGradientBoostingClassifier<F>;
type Error = FerroError;
fn fit(
&self,
x: &Array2<F>,
y: &Array1<usize>,
) -> Result<FittedGradientBoostingClassifier<F>, FerroError> {
let (n_samples, n_features) = x.dim();
if n_samples != y.len() {
return Err(FerroError::ShapeMismatch {
expected: vec![n_samples],
actual: vec![y.len()],
context: "y length must match number of samples in X".into(),
});
}
if n_samples == 0 {
return Err(FerroError::InsufficientSamples {
required: 1,
actual: 0,
context: "GradientBoostingClassifier requires at least one sample".into(),
});
}
if self.n_estimators == 0 {
return Err(FerroError::InvalidParameter {
name: "n_estimators".into(),
reason: "must be at least 1".into(),
});
}
if self.learning_rate <= 0.0 {
return Err(FerroError::InvalidParameter {
name: "learning_rate".into(),
reason: "must be positive".into(),
});
}
if self.subsample <= 0.0 || self.subsample > 1.0 {
return Err(FerroError::InvalidParameter {
name: "subsample".into(),
reason: "must be in (0, 1]".into(),
});
}
let mut classes: Vec<usize> = y.iter().copied().collect();
classes.sort_unstable();
classes.dedup();
let n_classes = classes.len();
if n_classes < 2 {
return Err(FerroError::InvalidParameter {
name: "y".into(),
reason: "need at least 2 distinct classes".into(),
});
}
let y_mapped: Vec<usize> = y
.iter()
.map(|&c| classes.iter().position(|&cl| cl == c).unwrap())
.collect();
let lr = F::from(self.learning_rate).unwrap();
let params = decision_tree::TreeParams {
max_depth: self.max_depth,
min_samples_split: self.min_samples_split,
min_samples_leaf: self.min_samples_leaf,
};
let all_features: Vec<usize> = (0..n_features).collect();
let subsample_size = ((self.subsample * n_samples as f64).ceil() as usize)
.max(1)
.min(n_samples);
let mut rng = if let Some(seed) = self.random_state {
StdRng::seed_from_u64(seed)
} else {
use rand::RngCore;
StdRng::seed_from_u64(rand::rng().next_u64())
};
if n_classes == 2 {
self.fit_binary(
x,
&y_mapped,
n_samples,
n_features,
&classes,
lr,
¶ms,
&all_features,
subsample_size,
&mut rng,
)
} else {
self.fit_multiclass(
x,
&y_mapped,
n_samples,
n_features,
n_classes,
&classes,
lr,
¶ms,
&all_features,
subsample_size,
&mut rng,
)
}
}
}
impl<F: Float + Send + Sync + 'static> GradientBoostingClassifier<F> {
#[allow(clippy::too_many_arguments)]
fn fit_binary(
&self,
x: &Array2<F>,
y_mapped: &[usize],
n_samples: usize,
n_features: usize,
classes: &[usize],
lr: F,
params: &decision_tree::TreeParams,
all_features: &[usize],
subsample_size: usize,
rng: &mut StdRng,
) -> Result<FittedGradientBoostingClassifier<F>, FerroError> {
let pos_count = y_mapped.iter().filter(|&&c| c == 1).count();
let p = F::from(pos_count).unwrap() / F::from(n_samples).unwrap();
let eps = F::from(1e-15).unwrap();
let p_clipped = p.max(eps).min(F::one() - eps);
let init_val = (p_clipped / (F::one() - p_clipped)).ln();
let mut f_vals = Array1::from_elem(n_samples, init_val);
let mut trees_seq: Vec<Vec<Node<F>>> = Vec::with_capacity(self.n_estimators);
for _ in 0..self.n_estimators {
let probs: Vec<F> = f_vals.iter().map(|&fv| sigmoid(fv)).collect();
let mut residuals = Array1::zeros(n_samples);
for i in 0..n_samples {
let yi = F::from(y_mapped[i]).unwrap();
residuals[i] = yi - probs[i];
}
let sample_indices = if subsample_size < n_samples {
rand_sample_indices(rng, n_samples, subsample_size).into_vec()
} else {
(0..n_samples).collect()
};
let tree = build_regression_tree_with_feature_subset(
x,
&residuals,
&sample_indices,
all_features,
params,
);
for i in 0..n_samples {
let row = x.row(i);
let leaf_idx = decision_tree::traverse(&tree, &row);
if let Node::Leaf { value, .. } = tree[leaf_idx] {
f_vals[i] = f_vals[i] + lr * value;
}
}
trees_seq.push(tree);
}
let mut total_importances = Array1::<F>::zeros(n_features);
for tree_nodes in &trees_seq {
let tree_imp = compute_feature_importances(tree_nodes, n_features, n_samples);
total_importances = total_importances + tree_imp;
}
let imp_sum: F = total_importances
.iter()
.copied()
.fold(F::zero(), |a, b| a + b);
if imp_sum > F::zero() {
total_importances.mapv_inplace(|v| v / imp_sum);
}
Ok(FittedGradientBoostingClassifier {
classes: classes.to_vec(),
init: vec![init_val],
learning_rate: lr,
trees: vec![trees_seq],
n_features,
feature_importances: total_importances,
})
}
#[allow(clippy::too_many_arguments)]
fn fit_multiclass(
&self,
x: &Array2<F>,
y_mapped: &[usize],
n_samples: usize,
n_features: usize,
n_classes: usize,
classes: &[usize],
lr: F,
params: &decision_tree::TreeParams,
all_features: &[usize],
subsample_size: usize,
rng: &mut StdRng,
) -> Result<FittedGradientBoostingClassifier<F>, FerroError> {
let mut class_counts = vec![0usize; n_classes];
for &c in y_mapped {
class_counts[c] += 1;
}
let n_f = F::from(n_samples).unwrap();
let eps = F::from(1e-15).unwrap();
let init_vals: Vec<F> = class_counts
.iter()
.map(|&cnt| {
let p = (F::from(cnt).unwrap() / n_f).max(eps);
p.ln()
})
.collect();
let mut f_vals: Vec<Array1<F>> = init_vals
.iter()
.map(|&init| Array1::from_elem(n_samples, init))
.collect();
let mut trees_per_class: Vec<Vec<Vec<Node<F>>>> = (0..n_classes)
.map(|_| Vec::with_capacity(self.n_estimators))
.collect();
for _ in 0..self.n_estimators {
let probs = softmax_matrix(&f_vals, n_samples, n_classes);
let sample_indices = if subsample_size < n_samples {
rand_sample_indices(rng, n_samples, subsample_size).into_vec()
} else {
(0..n_samples).collect()
};
for k in 0..n_classes {
let mut residuals = Array1::zeros(n_samples);
for i in 0..n_samples {
let yi_k = if y_mapped[i] == k {
F::one()
} else {
F::zero()
};
residuals[i] = yi_k - probs[k][i];
}
let tree = build_regression_tree_with_feature_subset(
x,
&residuals,
&sample_indices,
all_features,
params,
);
for (i, fv) in f_vals[k].iter_mut().enumerate() {
let row = x.row(i);
let leaf_idx = decision_tree::traverse(&tree, &row);
if let Node::Leaf { value, .. } = tree[leaf_idx] {
*fv = *fv + lr * value;
}
}
trees_per_class[k].push(tree);
}
}
let mut total_importances = Array1::<F>::zeros(n_features);
for class_trees in &trees_per_class {
for tree_nodes in class_trees {
let tree_imp = compute_feature_importances(tree_nodes, n_features, n_samples);
total_importances = total_importances + tree_imp;
}
}
let imp_sum: F = total_importances
.iter()
.copied()
.fold(F::zero(), |a, b| a + b);
if imp_sum > F::zero() {
total_importances.mapv_inplace(|v| v / imp_sum);
}
Ok(FittedGradientBoostingClassifier {
classes: classes.to_vec(),
init: init_vals,
learning_rate: lr,
trees: trees_per_class,
n_features,
feature_importances: total_importances,
})
}
}
impl<F: Float + Send + Sync + 'static> FittedGradientBoostingClassifier<F> {
#[must_use]
pub fn init(&self) -> &[F] {
&self.init
}
#[must_use]
pub fn learning_rate(&self) -> F {
self.learning_rate
}
#[must_use]
pub fn trees(&self) -> &[Vec<Vec<Node<F>>>] {
&self.trees
}
#[must_use]
pub fn n_features(&self) -> usize {
self.n_features
}
pub fn score(&self, x: &Array2<F>, y: &Array1<usize>) -> Result<F, FerroError> {
if x.nrows() != y.len() {
return Err(FerroError::ShapeMismatch {
expected: vec![x.nrows()],
actual: vec![y.len()],
context: "y length must match number of samples in X".into(),
});
}
let preds = self.predict(x)?;
Ok(crate::mean_accuracy(&preds, y))
}
pub fn predict_proba(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError> {
if x.ncols() != self.n_features {
return Err(FerroError::ShapeMismatch {
expected: vec![self.n_features],
actual: vec![x.ncols()],
context: "number of features must match fitted model".into(),
});
}
let n_samples = x.nrows();
let n_classes = self.classes.len();
let mut proba = Array2::<F>::zeros((n_samples, n_classes));
if n_classes == 2 {
let init = self.init[0];
for i in 0..n_samples {
let row = x.row(i);
let mut f_val = init;
for tree_nodes in &self.trees[0] {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
f_val = f_val + self.learning_rate * value;
}
}
let p1 = sigmoid(f_val);
proba[[i, 0]] = F::one() - p1;
proba[[i, 1]] = p1;
}
} else {
for i in 0..n_samples {
let row = x.row(i);
let mut scores = vec![F::zero(); n_classes];
for k in 0..n_classes {
let mut f_val = self.init[k];
for tree_nodes in &self.trees[k] {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
f_val = f_val + self.learning_rate * value;
}
}
scores[k] = f_val;
}
let max_s = scores
.iter()
.copied()
.fold(F::neg_infinity(), |a, b| if b > a { b } else { a });
let mut sum_exp = F::zero();
for k in 0..n_classes {
let e = (scores[k] - max_s).exp();
proba[[i, k]] = e;
sum_exp = sum_exp + e;
}
if sum_exp > F::zero() {
for k in 0..n_classes {
proba[[i, k]] = proba[[i, k]] / sum_exp;
}
}
}
}
Ok(proba)
}
pub fn predict_log_proba(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError> {
let proba = self.predict_proba(x)?;
Ok(crate::log_proba(&proba))
}
pub fn decision_function(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError> {
if x.ncols() != self.n_features {
return Err(FerroError::ShapeMismatch {
expected: vec![self.n_features],
actual: vec![x.ncols()],
context: "number of features must match fitted model".into(),
});
}
let n_samples = x.nrows();
let n_classes = self.classes.len();
if n_classes == 2 {
let init = self.init[0];
let mut out = Array2::<F>::zeros((n_samples, 1));
for i in 0..n_samples {
let row = x.row(i);
let mut f_val = init;
for tree_nodes in &self.trees[0] {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
f_val = f_val + self.learning_rate * value;
}
}
out[[i, 0]] = f_val;
}
Ok(out)
} else {
let mut out = Array2::<F>::zeros((n_samples, n_classes));
for i in 0..n_samples {
let row = x.row(i);
for k in 0..n_classes {
let mut f_val = self.init[k];
for tree_nodes in &self.trees[k] {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
f_val = f_val + self.learning_rate * value;
}
}
out[[i, k]] = f_val;
}
}
Ok(out)
}
}
}
impl<F: Float + Send + Sync + 'static> Predict<Array2<F>> for FittedGradientBoostingClassifier<F> {
type Output = Array1<usize>;
type Error = FerroError;
fn predict(&self, x: &Array2<F>) -> Result<Array1<usize>, FerroError> {
if x.ncols() != self.n_features {
return Err(FerroError::ShapeMismatch {
expected: vec![self.n_features],
actual: vec![x.ncols()],
context: "number of features must match fitted model".into(),
});
}
let n_samples = x.nrows();
let n_classes = self.classes.len();
if n_classes == 2 {
let init = self.init[0];
let mut predictions = Array1::zeros(n_samples);
for i in 0..n_samples {
let row = x.row(i);
let mut f_val = init;
for tree_nodes in &self.trees[0] {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
f_val = f_val + self.learning_rate * value;
}
}
let prob = sigmoid(f_val);
let class_idx = if prob >= F::from(0.5).unwrap() { 1 } else { 0 };
predictions[i] = self.classes[class_idx];
}
Ok(predictions)
} else {
let mut predictions = Array1::zeros(n_samples);
for i in 0..n_samples {
let row = x.row(i);
let mut scores = Vec::with_capacity(n_classes);
for k in 0..n_classes {
let mut f_val = self.init[k];
for tree_nodes in &self.trees[k] {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
f_val = f_val + self.learning_rate * value;
}
}
scores.push(f_val);
}
let best_k = scores
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
.map_or(0, |(k, _)| k);
predictions[i] = self.classes[best_k];
}
Ok(predictions)
}
}
}
impl<F: Float + Send + Sync + 'static> HasFeatureImportances<F>
for FittedGradientBoostingClassifier<F>
{
fn feature_importances(&self) -> &Array1<F> {
&self.feature_importances
}
}
impl<F: Float + Send + Sync + 'static> HasClasses for FittedGradientBoostingClassifier<F> {
fn classes(&self) -> &[usize] {
&self.classes
}
fn n_classes(&self) -> usize {
self.classes.len()
}
}
impl<F: Float + ToPrimitive + FromPrimitive + Send + Sync + 'static> PipelineEstimator<F>
for GradientBoostingClassifier<F>
{
fn fit_pipeline(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError> {
let y_usize: Array1<usize> = y.mapv(|v| v.to_usize().unwrap_or(0));
let fitted = self.fit(x, &y_usize)?;
Ok(Box::new(FittedGbcPipelineAdapter(fitted)))
}
}
struct FittedGbcPipelineAdapter<F: Float + Send + Sync + 'static>(
FittedGradientBoostingClassifier<F>,
);
impl<F: Float + ToPrimitive + FromPrimitive + Send + Sync + 'static> FittedPipelineEstimator<F>
for FittedGbcPipelineAdapter<F>
{
fn predict_pipeline(&self, x: &Array2<F>) -> Result<Array1<F>, FerroError> {
let preds = self.0.predict(x)?;
Ok(preds.mapv(|v| F::from_usize(v).unwrap_or_else(F::nan)))
}
}
fn sigmoid<F: Float>(x: F) -> F {
F::one() / (F::one() + (-x).exp())
}
fn softmax_matrix<F: Float>(
f_vals: &[Array1<F>],
n_samples: usize,
n_classes: usize,
) -> Vec<Vec<F>> {
let mut probs: Vec<Vec<F>> = vec![vec![F::zero(); n_samples]; n_classes];
for i in 0..n_samples {
let max_val = (0..n_classes)
.map(|k| f_vals[k][i])
.fold(F::neg_infinity(), |a, b| if b > a { b } else { a });
let mut sum = F::zero();
let mut exps = vec![F::zero(); n_classes];
for k in 0..n_classes {
exps[k] = (f_vals[k][i] - max_val).exp();
sum = sum + exps[k];
}
let eps = F::from(1e-15).unwrap();
if sum < eps {
sum = eps;
}
for k in 0..n_classes {
probs[k][i] = exps[k] / sum;
}
}
probs
}
fn median_f<F: Float>(arr: &Array1<F>) -> F {
let mut sorted: Vec<F> = arr.iter().copied().collect();
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
let n = sorted.len();
if n == 0 {
return F::zero();
}
if n % 2 == 1 {
sorted[n / 2]
} else {
(sorted[n / 2 - 1] + sorted[n / 2]) / F::from(2.0).unwrap()
}
}
fn quantile_f<F: Float>(vals: &[F], alpha: f64) -> F {
if vals.is_empty() {
return F::zero();
}
let mut sorted: Vec<F> = vals.to_vec();
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
let idx = ((sorted.len() as f64 - 1.0) * alpha).round() as usize;
let idx = idx.min(sorted.len() - 1);
sorted[idx]
}
fn compute_regression_residuals<F: Float>(
y: &Array1<F>,
f_vals: &Array1<F>,
loss: RegressionLoss,
huber_alpha: f64,
) -> Array1<F> {
let n = y.len();
match loss {
RegressionLoss::LeastSquares => {
let mut residuals = Array1::zeros(n);
for i in 0..n {
residuals[i] = y[i] - f_vals[i];
}
residuals
}
RegressionLoss::Lad => {
let mut residuals = Array1::zeros(n);
for i in 0..n {
let diff = y[i] - f_vals[i];
residuals[i] = if diff > F::zero() {
F::one()
} else if diff < F::zero() {
-F::one()
} else {
F::zero()
};
}
residuals
}
RegressionLoss::Huber => {
let raw_residuals: Vec<F> = (0..n).map(|i| (y[i] - f_vals[i]).abs()).collect();
let delta = quantile_f(&raw_residuals, huber_alpha);
let mut residuals = Array1::zeros(n);
for i in 0..n {
let diff = y[i] - f_vals[i];
if diff.abs() <= delta {
residuals[i] = diff;
} else if diff > F::zero() {
residuals[i] = delta;
} else {
residuals[i] = -delta;
}
}
residuals
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
use ndarray::array;
#[test]
fn test_gbr_simple_least_squares() {
let x =
Array2::from_shape_vec((8, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(50)
.with_learning_rate(0.1)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 8);
for i in 0..4 {
assert!(preds[i] < 3.0, "Expected ~1.0, got {}", preds[i]);
}
for i in 4..8 {
assert!(preds[i] > 3.0, "Expected ~5.0, got {}", preds[i]);
}
}
#[test]
fn test_gbr_lad_loss() {
let x =
Array2::from_shape_vec((8, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(50)
.with_loss(RegressionLoss::Lad)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 8);
for i in 0..4 {
assert!(preds[i] < 3.5, "LAD expected <3.5, got {}", preds[i]);
}
for i in 4..8 {
assert!(preds[i] > 2.5, "LAD expected >2.5, got {}", preds[i]);
}
}
#[test]
fn test_gbr_huber_loss() {
let x =
Array2::from_shape_vec((8, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(50)
.with_loss(RegressionLoss::Huber)
.with_huber_alpha(0.9)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 8);
}
#[test]
fn test_gbr_reproducibility() {
let x =
Array2::from_shape_vec((8, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(20)
.with_random_state(123);
let fitted1 = model.fit(&x, &y).unwrap();
let fitted2 = model.fit(&x, &y).unwrap();
let preds1 = fitted1.predict(&x).unwrap();
let preds2 = fitted2.predict(&x).unwrap();
for (p1, p2) in preds1.iter().zip(preds2.iter()) {
assert_relative_eq!(*p1, *p2, epsilon = 1e-10);
}
}
#[test]
fn test_gbr_feature_importances() {
let x = Array2::from_shape_vec(
(10, 3),
vec![
1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 3.0, 0.0, 0.0, 4.0, 0.0, 0.0, 5.0, 0.0, 0.0, 6.0,
0.0, 0.0, 7.0, 0.0, 0.0, 8.0, 0.0, 0.0, 9.0, 0.0, 0.0, 10.0, 0.0, 0.0,
],
)
.unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0, 5.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(20)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let importances = fitted.feature_importances();
assert_eq!(importances.len(), 3);
assert!(importances[0] > importances[1]);
assert!(importances[0] > importances[2]);
}
#[test]
fn test_gbr_shape_mismatch_fit() {
let x = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
let y = array![1.0, 2.0];
let model = GradientBoostingRegressor::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_gbr_shape_mismatch_predict() {
let x =
Array2::from_shape_vec((4, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
let y = array![1.0, 2.0, 3.0, 4.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(5)
.with_random_state(0);
let fitted = model.fit(&x, &y).unwrap();
let x_bad = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
assert!(fitted.predict(&x_bad).is_err());
}
#[test]
fn test_gbr_empty_data() {
let x = Array2::<f64>::zeros((0, 2));
let y = Array1::<f64>::zeros(0);
let model = GradientBoostingRegressor::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_gbr_zero_estimators() {
let x = Array2::from_shape_vec((4, 1), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
let y = array![1.0, 2.0, 3.0, 4.0];
let model = GradientBoostingRegressor::<f64>::new().with_n_estimators(0);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_gbr_invalid_learning_rate() {
let x = Array2::from_shape_vec((4, 1), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
let y = array![1.0, 2.0, 3.0, 4.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(5)
.with_learning_rate(0.0);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_gbr_invalid_subsample() {
let x = Array2::from_shape_vec((4, 1), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
let y = array![1.0, 2.0, 3.0, 4.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(5)
.with_subsample(0.0);
assert!(model.fit(&x, &y).is_err());
let model2 = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(5)
.with_subsample(1.5);
assert!(model2.fit(&x, &y).is_err());
}
#[test]
fn test_gbr_subsample() {
let x =
Array2::from_shape_vec((8, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(50)
.with_subsample(0.5)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 8);
}
#[test]
fn test_gbr_pipeline_integration() {
let x = Array2::from_shape_vec((4, 1), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
let y = array![1.0, 2.0, 3.0, 4.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(10)
.with_random_state(42);
let fitted = model.fit_pipeline(&x, &y).unwrap();
let preds = fitted.predict_pipeline(&x).unwrap();
assert_eq!(preds.len(), 4);
}
#[test]
fn test_gbr_f32_support() {
let x = Array2::from_shape_vec((4, 1), vec![1.0f32, 2.0, 3.0, 4.0]).unwrap();
let y = Array1::from_vec(vec![1.0f32, 2.0, 3.0, 4.0]);
let model = GradientBoostingRegressor::<f32>::new()
.with_n_estimators(10)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 4);
}
#[test]
fn test_gbr_max_depth() {
let x =
Array2::from_shape_vec((8, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0];
let model = GradientBoostingRegressor::<f64>::new()
.with_n_estimators(20)
.with_max_depth(Some(1))
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 8);
}
#[test]
fn test_gbr_default_trait() {
let model = GradientBoostingRegressor::<f64>::default();
assert_eq!(model.n_estimators, 100);
assert!((model.learning_rate - 0.1).abs() < 1e-10);
}
#[test]
fn test_gbc_binary_simple() {
let x = Array2::from_shape_vec(
(8, 2),
vec![
1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0, 4.0, 5.0, 6.0, 6.0, 7.0, 7.0, 8.0, 8.0, 9.0,
],
)
.unwrap();
let y = array![0, 0, 0, 0, 1, 1, 1, 1];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(50)
.with_learning_rate(0.1)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 8);
for i in 0..4 {
assert_eq!(preds[i], 0, "Expected 0 at index {}, got {}", i, preds[i]);
}
for i in 4..8 {
assert_eq!(preds[i], 1, "Expected 1 at index {}, got {}", i, preds[i]);
}
}
#[test]
fn test_gbc_multiclass() {
let x = Array2::from_shape_vec((9, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
.unwrap();
let y = array![0, 0, 0, 1, 1, 1, 2, 2, 2];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(50)
.with_learning_rate(0.1)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 9);
let correct = preds.iter().zip(y.iter()).filter(|(p, t)| p == t).count();
assert!(
correct >= 6,
"Expected at least 6/9 correct, got {correct}/9"
);
}
#[test]
fn test_gbc_has_classes() {
let x = Array2::from_shape_vec((6, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
let y = array![0, 1, 2, 0, 1, 2];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(5)
.with_random_state(0);
let fitted = model.fit(&x, &y).unwrap();
assert_eq!(fitted.classes(), &[0, 1, 2]);
assert_eq!(fitted.n_classes(), 3);
}
#[test]
fn test_gbc_reproducibility() {
let x = Array2::from_shape_vec(
(8, 2),
vec![
1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0, 4.0, 5.0, 6.0, 6.0, 7.0, 7.0, 8.0, 8.0, 9.0,
],
)
.unwrap();
let y = array![0, 0, 0, 0, 1, 1, 1, 1];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(10)
.with_random_state(42);
let fitted1 = model.fit(&x, &y).unwrap();
let fitted2 = model.fit(&x, &y).unwrap();
let preds1 = fitted1.predict(&x).unwrap();
let preds2 = fitted2.predict(&x).unwrap();
assert_eq!(preds1, preds2);
}
#[test]
fn test_gbc_feature_importances() {
let x = Array2::from_shape_vec(
(10, 3),
vec![
1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 3.0, 0.0, 0.0, 4.0, 0.0, 0.0, 5.0, 0.0, 0.0, 6.0,
0.0, 0.0, 7.0, 0.0, 0.0, 8.0, 0.0, 0.0, 9.0, 0.0, 0.0, 10.0, 0.0, 0.0,
],
)
.unwrap();
let y = array![0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(20)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let importances = fitted.feature_importances();
assert_eq!(importances.len(), 3);
assert!(importances[0] > importances[1]);
assert!(importances[0] > importances[2]);
}
#[test]
fn test_gbc_shape_mismatch_fit() {
let x = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
let y = array![0, 1];
let model = GradientBoostingClassifier::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_gbc_shape_mismatch_predict() {
let x =
Array2::from_shape_vec((4, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
let y = array![0, 0, 1, 1];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(5)
.with_random_state(0);
let fitted = model.fit(&x, &y).unwrap();
let x_bad = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
assert!(fitted.predict(&x_bad).is_err());
}
#[test]
fn test_gbc_empty_data() {
let x = Array2::<f64>::zeros((0, 2));
let y = Array1::<usize>::zeros(0);
let model = GradientBoostingClassifier::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_gbc_single_class() {
let x = Array2::from_shape_vec((3, 1), vec![1.0, 2.0, 3.0]).unwrap();
let y = array![0, 0, 0];
let model = GradientBoostingClassifier::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_gbc_zero_estimators() {
let x = Array2::from_shape_vec((4, 1), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
let y = array![0, 0, 1, 1];
let model = GradientBoostingClassifier::<f64>::new().with_n_estimators(0);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_gbc_pipeline_integration() {
let x = Array2::from_shape_vec((6, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
let y = Array1::from_vec(vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0]);
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(10)
.with_random_state(42);
let fitted = model.fit_pipeline(&x, &y).unwrap();
let preds = fitted.predict_pipeline(&x).unwrap();
assert_eq!(preds.len(), 6);
}
#[test]
fn test_gbc_f32_support() {
let x = Array2::from_shape_vec((6, 1), vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
let y = array![0, 0, 0, 1, 1, 1];
let model = GradientBoostingClassifier::<f32>::new()
.with_n_estimators(10)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 6);
}
#[test]
fn test_gbc_subsample() {
let x = Array2::from_shape_vec(
(8, 2),
vec![
1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0, 4.0, 5.0, 6.0, 6.0, 7.0, 7.0, 8.0, 8.0, 9.0,
],
)
.unwrap();
let y = array![0, 0, 0, 0, 1, 1, 1, 1];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(20)
.with_subsample(0.5)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 8);
}
#[test]
fn test_gbc_default_trait() {
let model = GradientBoostingClassifier::<f64>::default();
assert_eq!(model.n_estimators, 100);
assert!((model.learning_rate - 0.1).abs() < 1e-10);
}
#[test]
fn test_gbc_non_contiguous_labels() {
let x = Array2::from_shape_vec((6, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
let y = array![10, 10, 10, 20, 20, 20];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(20)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 6);
for &p in &preds {
assert!(p == 10 || p == 20);
}
}
#[test]
fn test_sigmoid() {
assert_relative_eq!(sigmoid(0.0f64), 0.5, epsilon = 1e-10);
assert!(sigmoid(10.0f64) > 0.999);
assert!(sigmoid(-10.0f64) < 0.001);
}
#[test]
fn test_median_f_odd() {
let arr = array![3.0, 1.0, 2.0];
assert_relative_eq!(median_f(&arr), 2.0, epsilon = 1e-10);
}
#[test]
fn test_median_f_even() {
let arr = array![4.0, 1.0, 3.0, 2.0];
assert_relative_eq!(median_f(&arr), 2.5, epsilon = 1e-10);
}
#[test]
fn test_median_f_empty() {
let arr = Array1::<f64>::zeros(0);
assert_relative_eq!(median_f(&arr), 0.0, epsilon = 1e-10);
}
#[test]
fn test_quantile_f() {
let vals = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let q90 = quantile_f(&vals, 0.9);
assert!((4.0..=5.0).contains(&q90));
}
#[test]
fn test_regression_residuals_least_squares() {
let y = array![1.0, 2.0, 3.0];
let f = array![0.5, 2.5, 2.0];
let r = compute_regression_residuals(&y, &f, RegressionLoss::LeastSquares, 0.9);
assert_relative_eq!(r[0], 0.5, epsilon = 1e-10);
assert_relative_eq!(r[1], -0.5, epsilon = 1e-10);
assert_relative_eq!(r[2], 1.0, epsilon = 1e-10);
}
#[test]
fn test_regression_residuals_lad() {
let y = array![1.0, 2.0, 3.0];
let f = array![0.5, 2.5, 3.0];
let r = compute_regression_residuals(&y, &f, RegressionLoss::Lad, 0.9);
assert_relative_eq!(r[0], 1.0, epsilon = 1e-10);
assert_relative_eq!(r[1], -1.0, epsilon = 1e-10);
assert_relative_eq!(r[2], 0.0, epsilon = 1e-10);
}
#[test]
fn test_regression_residuals_huber() {
let y = array![1.0, 2.0, 10.0, 3.0, 4.0];
let f = array![1.5, 2.5, 2.0, 3.5, 4.5];
let r = compute_regression_residuals(&y, &f, RegressionLoss::Huber, 0.9);
assert_relative_eq!(r[0], -0.5, epsilon = 1e-10);
assert_relative_eq!(r[1], -0.5, epsilon = 1e-10);
assert_relative_eq!(r[2], 8.0, epsilon = 1e-10);
assert_relative_eq!(r[3], -0.5, epsilon = 1e-10);
assert_relative_eq!(r[4], -0.5, epsilon = 1e-10);
let r2 = compute_regression_residuals(&y, &f, RegressionLoss::Huber, 0.1);
assert_relative_eq!(r2[0], -0.5, epsilon = 1e-10);
assert_relative_eq!(r2[2], 0.5, epsilon = 1e-10);
}
#[test]
fn test_gbc_multiclass_4_classes() {
let x = Array2::from_shape_vec(
(12, 1),
vec![
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
],
)
.unwrap();
let y = array![0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(50)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 12);
assert_eq!(fitted.n_classes(), 4);
}
#[test]
fn test_gbc_invalid_learning_rate() {
let x = Array2::from_shape_vec((4, 1), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
let y = array![0, 0, 1, 1];
let model = GradientBoostingClassifier::<f64>::new()
.with_n_estimators(5)
.with_learning_rate(-0.1);
assert!(model.fit(&x, &y).is_err());
}
}