diff --git a/CHANGELOG.md b/CHANGELOG.md index 20a6f4ec..867f7f01 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. - `linalg`: `variance` and `std_dev` use a numerically stable formula. Before this change, columns far from zero (for example Unix timestamps) had a variance that was wrong, `0` or negative. This affected `StandardScaler`, `GaussianNB`, `RidgeRegression`, `Lasso` and `ElasticNet`. diff --git a/src/ensemble/base_forest.rs b/src/ensemble/base_forest.rs index 19c59739..c12e8516 100644 --- a/src/ensemble/base_forest.rs +++ b/src/ensemble/base_forest.rs @@ -137,6 +137,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 6f0bd20e..3cb91ed4 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`]. @@ -166,7 +166,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 @@ -735,6 +735,22 @@ 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(); + 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 e54a1897..827a7828 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`]. @@ -133,7 +133,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 @@ -723,6 +723,23 @@ 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(); + 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 6f9bff8c..2e91f7f2 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 @@ -960,6 +960,22 @@ 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(); + 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 04ec2d43..b0d6b0f1 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 @@ -1025,6 +1025,23 @@ 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(); + 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.