From ec59133844b0e3fab936aa366d0c60ad96315f83 Mon Sep 17 00:00:00 2001 From: aviralgarg05 Date: Fri, 20 Nov 2026 12:42:49 +0530 Subject: [PATCH 1/2] Reject forests with zero trees --- CHANGELOG.md | 1 + src/ensemble/base_forest.rs | 7 +++++++ src/ensemble/extra_trees_classifier.rs | 12 ++++++++++++ src/ensemble/extra_trees_regressor.rs | 13 +++++++++++++ src/ensemble/random_forest_classifier.rs | 12 ++++++++++++ src/ensemble/random_forest_regressor.rs | 13 +++++++++++++ 6 files changed, 58 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09a86be2..f8d0b3df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `ensemble`: `RandomForestClassifier` and `ExtraTreesClassifier` get a `predict_proba` method. ### Fixed +- `ensemble`: The four forest estimators return a parameter error when `n_trees` is zero instead of fitting an empty forest (#541). - `ensemble`: `ExtraTreesRegressor::predict_oob` returns an error when the forest was fitted without bootstrap. Before this change, it returned `NaN` for a float target and panicked for an integer target (#520). - `ensemble`: `RandomForestRegressor::predict_oob` and `ExtraTreesRegressor::predict_oob` return an error when a row is in-bag for all trees. Before this change, it returned `NaN` for a float target and panicked for an integer target. diff --git a/src/ensemble/base_forest.rs b/src/ensemble/base_forest.rs index eb89d2a1..3ba73dbd 100644 --- a/src/ensemble/base_forest.rs +++ b/src/ensemble/base_forest.rs @@ -117,6 +117,13 @@ impl< criterion: &C, parameters: BaseForestParameters, ) -> Result, Failed> { + if parameters.n_trees == 0 { + return Err(Failed::because( + FailedError::ParametersError, + "n_trees must be at least 1", + )); + } + let (n_rows, num_attributes) = x.shape(); if n_rows != y.shape() { diff --git a/src/ensemble/extra_trees_classifier.rs b/src/ensemble/extra_trees_classifier.rs index 4c4ff924..078b9677 100644 --- a/src/ensemble/extra_trees_classifier.rs +++ b/src/ensemble/extra_trees_classifier.rs @@ -728,6 +728,18 @@ mod tests { ); } + #[test] + fn fit_with_no_trees_returns_parameter_error() { + let (x, y) = iris(); + let result = ExtraTreesClassifier::fit( + &x, + &y, + ExtraTreesClassifierParameters::default().with_n_trees(0), + ); + + assert_eq!(result.err().unwrap().error(), FailedError::ParametersError); + } + #[cfg_attr( all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test diff --git a/src/ensemble/extra_trees_regressor.rs b/src/ensemble/extra_trees_regressor.rs index 666c6020..2f51d2d0 100644 --- a/src/ensemble/extra_trees_regressor.rs +++ b/src/ensemble/extra_trees_regressor.rs @@ -670,6 +670,19 @@ mod tests { } } + #[test] + fn fit_with_no_trees_returns_parameter_error() { + let x = DenseMatrix::from_2d_array(&[&[1.0], &[2.0]]).unwrap(); + let y = vec![1.0, 2.0]; + let result = ExtraTreesRegressor::fit( + &x, + &y, + ExtraTreesRegressorParameters::default().with_n_trees(0), + ); + + assert_eq!(result.err().unwrap().error(), FailedError::ParametersError); + } + mod sklearn_parity { use super::*; // sklearn parity tests. diff --git a/src/ensemble/random_forest_classifier.rs b/src/ensemble/random_forest_classifier.rs index f8175f98..7bb3ce43 100644 --- a/src/ensemble/random_forest_classifier.rs +++ b/src/ensemble/random_forest_classifier.rs @@ -936,6 +936,18 @@ mod tests { assert_eq!(result.err().unwrap().error(), FailedError::ParametersError); } + #[test] + fn fit_with_no_trees_returns_parameter_error() { + let (x, y) = iris(); + let result = RandomForestClassifier::fit( + &x, + &y, + RandomForestClassifierParameters::default().with_n_trees(0), + ); + + assert_eq!(result.err().unwrap().error(), FailedError::ParametersError); + } + #[cfg_attr( all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test diff --git a/src/ensemble/random_forest_regressor.rs b/src/ensemble/random_forest_regressor.rs index 7509732b..8723f1fb 100644 --- a/src/ensemble/random_forest_regressor.rs +++ b/src/ensemble/random_forest_regressor.rs @@ -974,6 +974,19 @@ mod tests { } } + #[test] + fn fit_with_no_trees_returns_parameter_error() { + let x = DenseMatrix::from_2d_array(&[&[1.0], &[2.0]]).unwrap(); + let y = vec![1.0, 2.0]; + let result = RandomForestRegressor::fit( + &x, + &y, + RandomForestRegressorParameters::default().with_n_trees(0), + ); + + assert_eq!(result.err().unwrap().error(), FailedError::ParametersError); + } + mod sklearn_parity { use super::*; // sklearn parity tests. From afd3369add6a2c9b4042cb1db0b530b6b7865896 Mon Sep 17 00:00:00 2001 From: aviralgarg05 Date: Sat, 21 Nov 2026 00:39:36 +0530 Subject: [PATCH 2/2] Address forest validation review feedback Signed-off-by: aviralgarg05 --- src/ensemble/extra_trees_classifier.rs | 8 ++++++-- src/ensemble/extra_trees_regressor.rs | 8 ++++++-- src/ensemble/random_forest_classifier.rs | 8 ++++++-- src/ensemble/random_forest_regressor.rs | 8 ++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/ensemble/extra_trees_classifier.rs b/src/ensemble/extra_trees_classifier.rs index 797f7d7b..4b639bf2 100644 --- a/src/ensemble/extra_trees_classifier.rs +++ b/src/ensemble/extra_trees_classifier.rs @@ -113,7 +113,7 @@ pub struct ExtraTreesClassifierParameters { /// The minimum number of samples required to split an internal node. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html) pub min_samples_split: usize, #[cfg_attr(feature = "serde", serde(default))] - /// The number of trees in the forest. + /// The number of trees in the forest. This value must be at least 1 when you call `fit`. pub n_trees: usize, #[cfg_attr(feature = "serde", serde(default))] /// Number of features to test at each split. See [`MaxFeatures`]. @@ -167,7 +167,7 @@ impl ExtraTreesClassifierParameters { self.min_samples_split = min_samples_split; self } - /// The number of trees in the forest. + /// Sets the number of trees in the forest. `fit` returns a parameter error if this value is 0. pub fn with_n_trees(mut self, n_trees: usize) -> Self { self.n_trees = n_trees; self @@ -728,6 +728,10 @@ mod tests { ); } + #[cfg_attr( + all(target_arch = "wasm32", not(target_os = "wasi")), + wasm_bindgen_test::wasm_bindgen_test + )] #[test] fn fit_with_no_trees_returns_parameter_error() { let (x, y) = iris(); diff --git a/src/ensemble/extra_trees_regressor.rs b/src/ensemble/extra_trees_regressor.rs index 2f51d2d0..222fdca9 100644 --- a/src/ensemble/extra_trees_regressor.rs +++ b/src/ensemble/extra_trees_regressor.rs @@ -87,7 +87,7 @@ pub struct ExtraTreesRegressorParameters { /// The minimum number of samples required to split an internal node. See [Decision Tree Regressor](../../tree/decision_tree_regressor/index.html) pub min_samples_split: usize, #[cfg_attr(feature = "serde", serde(default))] - /// The number of trees in the forest. + /// The number of trees in the forest. This value must be at least 1 when you call `fit`. pub n_trees: usize, #[cfg_attr(feature = "serde", serde(default))] /// Number of features to test at each split. See [`MaxFeatures`]. @@ -134,7 +134,7 @@ impl ExtraTreesRegressorParameters { self.min_samples_split = min_samples_split; self } - /// The number of trees in the forest. + /// Sets the number of trees in the forest. `fit` returns a parameter error if this value is 0. pub fn with_n_trees(mut self, n_trees: usize) -> Self { self.n_trees = n_trees; self @@ -670,6 +670,10 @@ mod tests { } } + #[cfg_attr( + all(target_arch = "wasm32", not(target_os = "wasi")), + wasm_bindgen_test::wasm_bindgen_test + )] #[test] fn fit_with_no_trees_returns_parameter_error() { let x = DenseMatrix::from_2d_array(&[&[1.0], &[2.0]]).unwrap(); diff --git a/src/ensemble/random_forest_classifier.rs b/src/ensemble/random_forest_classifier.rs index 1b76ced3..75109b74 100644 --- a/src/ensemble/random_forest_classifier.rs +++ b/src/ensemble/random_forest_classifier.rs @@ -89,7 +89,7 @@ pub struct RandomForestClassifierParameters { /// The minimum number of samples required to split an internal node. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html) pub min_samples_split: usize, #[cfg_attr(feature = "serde", serde(default))] - /// The number of trees in the forest. + /// The number of trees in the forest. This value must be at least 1 when you call `fit`. pub n_trees: u16, #[cfg_attr(feature = "serde", serde(default))] /// Number of features to test at each split. See [`MaxFeatures`]. @@ -137,7 +137,7 @@ impl RandomForestClassifierParameters { self.min_samples_split = min_samples_split; self } - /// The number of trees in the forest. + /// Sets the number of trees in the forest. `fit` returns a parameter error if this value is 0. pub fn with_n_trees(mut self, n_trees: u16) -> Self { self.n_trees = n_trees; self @@ -936,6 +936,10 @@ mod tests { assert_eq!(result.err().unwrap().error(), FailedError::ParametersError); } + #[cfg_attr( + all(target_arch = "wasm32", not(target_os = "wasi")), + wasm_bindgen_test::wasm_bindgen_test + )] #[test] fn fit_with_no_trees_returns_parameter_error() { let (x, y) = iris(); diff --git a/src/ensemble/random_forest_regressor.rs b/src/ensemble/random_forest_regressor.rs index 8723f1fb..78d3fd07 100644 --- a/src/ensemble/random_forest_regressor.rs +++ b/src/ensemble/random_forest_regressor.rs @@ -77,7 +77,7 @@ pub struct RandomForestRegressorParameters { /// The minimum number of samples required to split an internal node. See [Decision Tree Regressor](../../tree/decision_tree_regressor/index.html) pub min_samples_split: usize, #[cfg_attr(feature = "serde", serde(default))] - /// The number of trees in the forest. + /// The number of trees in the forest. This value must be at least 1 when you call `fit`. pub n_trees: usize, #[cfg_attr(feature = "serde", serde(default))] /// Number of features to test at each split. See [`MaxFeatures`]. @@ -118,7 +118,7 @@ impl RandomForestRegressorParameters { self.min_samples_split = min_samples_split; self } - /// The number of trees in the forest. + /// Sets the number of trees in the forest. `fit` returns a parameter error if this value is 0. pub fn with_n_trees(mut self, n_trees: usize) -> Self { self.n_trees = n_trees; self @@ -974,6 +974,10 @@ mod tests { } } + #[cfg_attr( + all(target_arch = "wasm32", not(target_os = "wasi")), + wasm_bindgen_test::wasm_bindgen_test + )] #[test] fn fit_with_no_trees_returns_parameter_error() { let x = DenseMatrix::from_2d_array(&[&[1.0], &[2.0]]).unwrap();