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 rayon::prelude::*;
use serde::{Deserialize, Serialize};
use crate::decision_tree::{
self, ClassificationCriterion, Node, build_classification_tree_per_split_features,
build_regression_tree_per_split_features, compute_feature_importances,
};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum MaxFeatures {
Sqrt,
Log2,
All,
Fixed(usize),
Fraction(f64),
}
fn resolve_max_features(strategy: MaxFeatures, n_features: usize) -> usize {
let result = match strategy {
MaxFeatures::Sqrt => (n_features as f64).sqrt().ceil() as usize,
MaxFeatures::Log2 => (n_features as f64).log2().ceil().max(1.0) as usize,
MaxFeatures::All => n_features,
MaxFeatures::Fixed(n) => n.min(n_features),
MaxFeatures::Fraction(f) => ((n_features as f64) * f).ceil() as usize,
};
result.max(1).min(n_features)
}
fn make_tree_params(
max_depth: Option<usize>,
min_samples_split: usize,
min_samples_leaf: usize,
) -> decision_tree::TreeParams {
decision_tree::TreeParams {
max_depth,
min_samples_split,
min_samples_leaf,
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RandomForestClassifier<F> {
pub n_estimators: usize,
pub max_depth: Option<usize>,
pub max_features: MaxFeatures,
pub min_samples_split: usize,
pub min_samples_leaf: usize,
pub random_state: Option<u64>,
pub criterion: ClassificationCriterion,
_marker: std::marker::PhantomData<F>,
}
impl<F: Float> RandomForestClassifier<F> {
#[must_use]
pub fn new() -> Self {
Self {
n_estimators: 100,
max_depth: None,
max_features: MaxFeatures::Sqrt,
min_samples_split: 2,
min_samples_leaf: 1,
random_state: None,
criterion: ClassificationCriterion::Gini,
_marker: std::marker::PhantomData,
}
}
#[must_use]
pub fn with_n_estimators(mut self, n_estimators: usize) -> Self {
self.n_estimators = n_estimators;
self
}
#[must_use]
pub fn with_max_depth(mut self, max_depth: Option<usize>) -> Self {
self.max_depth = max_depth;
self
}
#[must_use]
pub fn with_max_features(mut self, max_features: MaxFeatures) -> Self {
self.max_features = max_features;
self
}
#[must_use]
pub fn with_min_samples_split(mut self, min_samples_split: usize) -> Self {
self.min_samples_split = min_samples_split;
self
}
#[must_use]
pub fn with_min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
self.min_samples_leaf = min_samples_leaf;
self
}
#[must_use]
pub fn with_random_state(mut self, seed: u64) -> Self {
self.random_state = Some(seed);
self
}
#[must_use]
pub fn with_criterion(mut self, criterion: ClassificationCriterion) -> Self {
self.criterion = criterion;
self
}
}
impl<F: Float> Default for RandomForestClassifier<F> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct FittedRandomForestClassifier<F> {
trees: Vec<Vec<Node<F>>>,
classes: Vec<usize>,
n_features: usize,
feature_importances: Array1<F>,
}
impl<F: Float + Send + Sync + 'static> Fit<Array2<F>, Array1<usize>> for RandomForestClassifier<F> {
type Fitted = FittedRandomForestClassifier<F>;
type Error = FerroError;
fn fit(
&self,
x: &Array2<F>,
y: &Array1<usize>,
) -> Result<FittedRandomForestClassifier<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: "RandomForestClassifier 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(),
});
}
let mut classes: Vec<usize> = y.iter().copied().collect();
classes.sort_unstable();
classes.dedup();
let n_classes = classes.len();
let y_mapped: Vec<usize> = y
.iter()
.map(|&c| classes.iter().position(|&cl| cl == c).unwrap())
.collect();
let max_features_n = resolve_max_features(self.max_features, n_features);
let params = make_tree_params(
self.max_depth,
self.min_samples_split,
self.min_samples_leaf,
);
let criterion = self.criterion;
let tree_seeds: Vec<u64> = if let Some(seed) = self.random_state {
let mut master_rng = StdRng::seed_from_u64(seed);
(0..self.n_estimators)
.map(|_| {
use rand::RngCore;
master_rng.next_u64()
})
.collect()
} else {
(0..self.n_estimators)
.map(|_| {
use rand::RngCore;
rand::rng().next_u64()
})
.collect()
};
let trees: Vec<Vec<Node<F>>> = tree_seeds
.par_iter()
.map(|&seed| {
let mut bootstrap_rng = StdRng::seed_from_u64(seed);
let bootstrap_indices: Vec<usize> = (0..n_samples)
.map(|_| {
use rand::RngCore;
(bootstrap_rng.next_u64() as usize) % n_samples
})
.collect();
use rand::RngCore;
let split_seed = bootstrap_rng.next_u64();
build_classification_tree_per_split_features(
x,
&y_mapped,
n_classes,
&bootstrap_indices,
max_features_n,
¶ms,
criterion,
split_seed,
)
})
.collect();
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(FittedRandomForestClassifier {
trees,
classes,
n_features,
feature_importances: total_importances,
})
}
}
impl<F: Float + Send + Sync + 'static> FittedRandomForestClassifier<F> {
#[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<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 n_trees_f = F::from(self.trees.len()).unwrap();
let mut proba = Array2::<F>::zeros((n_samples, n_classes));
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);
match &tree_nodes[leaf_idx] {
Node::Leaf {
class_distribution: Some(dist),
..
} => {
for (j, &p) in dist.iter().enumerate().take(n_classes) {
proba[[i, j]] = proba[[i, j]] + p;
}
}
Node::Leaf { value, .. } => {
let class_idx = value.to_f64().map_or(0, |f| f.round() as usize);
if class_idx < n_classes {
proba[[i, class_idx]] = proba[[i, class_idx]] + F::one();
}
}
_ => {}
}
}
for j in 0..n_classes {
proba[[i, j]] = proba[[i, j]] / n_trees_f;
}
}
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))
}
}
impl<F: Float + Send + Sync + 'static> Predict<Array2<F>> for FittedRandomForestClassifier<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();
let mut predictions = Array1::zeros(n_samples);
for i in 0..n_samples {
let row = x.row(i);
let mut votes = vec![0usize; n_classes];
for tree_nodes in &self.trees {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
let class_idx = value.to_f64().map_or(0, |f| f.round() as usize);
if class_idx < n_classes {
votes[class_idx] += 1;
}
}
}
let winner = votes
.iter()
.enumerate()
.max_by_key(|&(_, &count)| count)
.map_or(0, |(idx, _)| idx);
predictions[i] = self.classes[winner];
}
Ok(predictions)
}
}
impl<F: Float + Send + Sync + 'static> HasFeatureImportances<F>
for FittedRandomForestClassifier<F>
{
fn feature_importances(&self) -> &Array1<F> {
&self.feature_importances
}
}
impl<F: Float + Send + Sync + 'static> HasClasses for FittedRandomForestClassifier<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 RandomForestClassifier<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(FittedForestClassifierPipelineAdapter(fitted)))
}
}
struct FittedForestClassifierPipelineAdapter<F: Float + Send + Sync + 'static>(
FittedRandomForestClassifier<F>,
);
impl<F: Float + ToPrimitive + FromPrimitive + Send + Sync + 'static> FittedPipelineEstimator<F>
for FittedForestClassifierPipelineAdapter<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)))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RandomForestRegressor<F> {
pub n_estimators: usize,
pub max_depth: Option<usize>,
pub max_features: MaxFeatures,
pub min_samples_split: usize,
pub min_samples_leaf: usize,
pub random_state: Option<u64>,
_marker: std::marker::PhantomData<F>,
}
impl<F: Float> RandomForestRegressor<F> {
#[must_use]
pub fn new() -> Self {
Self {
n_estimators: 100,
max_depth: None,
max_features: MaxFeatures::All,
min_samples_split: 2,
min_samples_leaf: 1,
random_state: None,
_marker: std::marker::PhantomData,
}
}
#[must_use]
pub fn with_n_estimators(mut self, n_estimators: usize) -> Self {
self.n_estimators = n_estimators;
self
}
#[must_use]
pub fn with_max_depth(mut self, max_depth: Option<usize>) -> Self {
self.max_depth = max_depth;
self
}
#[must_use]
pub fn with_max_features(mut self, max_features: MaxFeatures) -> Self {
self.max_features = max_features;
self
}
#[must_use]
pub fn with_min_samples_split(mut self, min_samples_split: usize) -> Self {
self.min_samples_split = min_samples_split;
self
}
#[must_use]
pub fn with_min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
self.min_samples_leaf = min_samples_leaf;
self
}
#[must_use]
pub fn with_random_state(mut self, seed: u64) -> Self {
self.random_state = Some(seed);
self
}
}
impl<F: Float> Default for RandomForestRegressor<F> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct FittedRandomForestRegressor<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 RandomForestRegressor<F> {
type Fitted = FittedRandomForestRegressor<F>;
type Error = FerroError;
fn fit(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<FittedRandomForestRegressor<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: "RandomForestRegressor 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(),
});
}
let max_features_n = resolve_max_features(self.max_features, n_features);
let params = make_tree_params(
self.max_depth,
self.min_samples_split,
self.min_samples_leaf,
);
let tree_seeds: Vec<u64> = if let Some(seed) = self.random_state {
let mut master_rng = StdRng::seed_from_u64(seed);
(0..self.n_estimators)
.map(|_| {
use rand::RngCore;
master_rng.next_u64()
})
.collect()
} else {
(0..self.n_estimators)
.map(|_| {
use rand::RngCore;
rand::rng().next_u64()
})
.collect()
};
let trees: Vec<Vec<Node<F>>> = tree_seeds
.par_iter()
.map(|&seed| {
let mut bootstrap_rng = StdRng::seed_from_u64(seed);
let bootstrap_indices: Vec<usize> = (0..n_samples)
.map(|_| {
use rand::RngCore;
(bootstrap_rng.next_u64() as usize) % n_samples
})
.collect();
use rand::RngCore;
let split_seed = bootstrap_rng.next_u64();
build_regression_tree_per_split_features(
x,
y,
&bootstrap_indices,
max_features_n,
¶ms,
split_seed,
)
})
.collect();
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(FittedRandomForestRegressor {
trees,
n_features,
feature_importances: total_importances,
})
}
}
impl<F: Float + Send + Sync + 'static> FittedRandomForestRegressor<F> {
#[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 FittedRandomForestRegressor<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 n_trees_f = F::from(self.trees.len()).unwrap();
let mut predictions = Array1::zeros(n_samples);
for i in 0..n_samples {
let row = x.row(i);
let mut sum = F::zero();
for tree_nodes in &self.trees {
let leaf_idx = decision_tree::traverse(tree_nodes, &row);
if let Node::Leaf { value, .. } = tree_nodes[leaf_idx] {
sum = sum + value;
}
}
predictions[i] = sum / n_trees_f;
}
Ok(predictions)
}
}
impl<F: Float + Send + Sync + 'static> HasFeatureImportances<F> for FittedRandomForestRegressor<F> {
fn feature_importances(&self) -> &Array1<F> {
&self.feature_importances
}
}
impl<F: Float + Send + Sync + 'static> PipelineEstimator<F> for RandomForestRegressor<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 FittedRandomForestRegressor<F>
{
fn predict_pipeline(&self, x: &Array2<F>) -> Result<Array1<F>, FerroError> {
self.predict(x)
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
use ndarray::array;
#[test]
fn test_forest_classifier_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 = RandomForestClassifier::<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(), 8);
for i in 0..4 {
assert_eq!(preds[i], 0);
}
for i in 4..8 {
assert_eq!(preds[i], 1);
}
}
#[test]
fn test_forest_classifier_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 = RandomForestClassifier::<f64>::new()
.with_n_estimators(10)
.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();
assert_eq!(preds1, preds2);
}
#[test]
fn test_forest_classifier_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 = RandomForestClassifier::<f64>::new()
.with_n_estimators(20)
.with_max_features(MaxFeatures::All)
.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_forest_classifier_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 = RandomForestClassifier::<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_forest_classifier_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 = RandomForestClassifier::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_forest_classifier_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 = RandomForestClassifier::<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_forest_classifier_empty_data() {
let x = Array2::<f64>::zeros((0, 2));
let y = Array1::<usize>::zeros(0);
let model = RandomForestClassifier::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_forest_classifier_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 = RandomForestClassifier::<f64>::new().with_n_estimators(0);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_forest_classifier_single_tree() {
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, 0, 0, 1, 1, 1];
let model = RandomForestClassifier::<f64>::new()
.with_n_estimators(1)
.with_max_features(MaxFeatures::All)
.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_forest_classifier_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 = RandomForestClassifier::<f64>::new()
.with_n_estimators(5)
.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_forest_classifier_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![0, 0, 0, 0, 1, 1, 1, 1];
let model = RandomForestClassifier::<f64>::new()
.with_n_estimators(10)
.with_max_depth(Some(1))
.with_max_features(MaxFeatures::All)
.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_forest_regressor_simple() {
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 = RandomForestRegressor::<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(), 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_forest_regressor_reproducibility() {
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![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let model = RandomForestRegressor::<f64>::new()
.with_n_estimators(10)
.with_random_state(99);
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_forest_regressor_feature_importances() {
let x = Array2::from_shape_vec(
(8, 2),
vec![
1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0, 0.0, 5.0, 0.0, 6.0, 0.0, 7.0, 0.0, 8.0, 0.0,
],
)
.unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0];
let model = RandomForestRegressor::<f64>::new()
.with_n_estimators(20)
.with_max_features(MaxFeatures::All)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let importances = fitted.feature_importances();
assert_eq!(importances.len(), 2);
assert!(importances[0] > importances[1]);
}
#[test]
fn test_forest_regressor_shape_mismatch_fit() {
let x = Array2::from_shape_vec((3, 1), vec![1.0, 2.0, 3.0]).unwrap();
let y = array![1.0, 2.0];
let model = RandomForestRegressor::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_forest_regressor_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 = RandomForestRegressor::<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_forest_regressor_empty_data() {
let x = Array2::<f64>::zeros((0, 2));
let y = Array1::<f64>::zeros(0);
let model = RandomForestRegressor::<f64>::new().with_n_estimators(5);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_forest_regressor_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 = RandomForestRegressor::<f64>::new().with_n_estimators(0);
assert!(model.fit(&x, &y).is_err());
}
#[test]
fn test_forest_regressor_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 = RandomForestRegressor::<f64>::new()
.with_n_estimators(5)
.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_forest_regressor_max_features_strategies() {
let x = Array2::from_shape_vec(
(8, 4),
vec![
1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 6.0, 4.0, 5.0, 6.0, 7.0,
5.0, 6.0, 7.0, 8.0, 6.0, 7.0, 8.0, 9.0, 7.0, 8.0, 9.0, 10.0, 8.0, 9.0, 10.0, 11.0,
],
)
.unwrap();
let y = array![1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0];
for strategy in &[
MaxFeatures::Sqrt,
MaxFeatures::Log2,
MaxFeatures::All,
MaxFeatures::Fixed(2),
MaxFeatures::Fraction(0.5),
] {
let model = RandomForestRegressor::<f64>::new()
.with_n_estimators(5)
.with_max_features(*strategy)
.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_resolve_max_features_sqrt() {
assert_eq!(resolve_max_features(MaxFeatures::Sqrt, 9), 3);
assert_eq!(resolve_max_features(MaxFeatures::Sqrt, 10), 4);
assert_eq!(resolve_max_features(MaxFeatures::Sqrt, 1), 1);
}
#[test]
fn test_resolve_max_features_log2() {
assert_eq!(resolve_max_features(MaxFeatures::Log2, 8), 3);
assert_eq!(resolve_max_features(MaxFeatures::Log2, 1), 1);
}
#[test]
fn test_resolve_max_features_all() {
assert_eq!(resolve_max_features(MaxFeatures::All, 10), 10);
assert_eq!(resolve_max_features(MaxFeatures::All, 1), 1);
}
#[test]
fn test_resolve_max_features_fixed() {
assert_eq!(resolve_max_features(MaxFeatures::Fixed(3), 10), 3);
assert_eq!(resolve_max_features(MaxFeatures::Fixed(20), 10), 10);
}
#[test]
fn test_resolve_max_features_fraction() {
assert_eq!(resolve_max_features(MaxFeatures::Fraction(0.5), 10), 5);
assert_eq!(resolve_max_features(MaxFeatures::Fraction(0.1), 10), 1);
}
#[test]
fn test_forest_classifier_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 = RandomForestClassifier::<f32>::new()
.with_n_estimators(5)
.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_forest_regressor_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 = RandomForestRegressor::<f32>::new()
.with_n_estimators(5)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 4);
}
}