Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
7 changes: 7 additions & 0 deletions src/ensemble/base_forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ impl<
criterion: &C,
parameters: BaseForestParameters,
) -> Result<BaseForest<TX, TY, X, Y, Out>, 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() {
Expand Down
20 changes: 18 additions & 2 deletions src/ensemble/extra_trees_classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions src/ensemble/extra_trees_regressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 18 additions & 2 deletions src/ensemble/random_forest_classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions src/ensemble/random_forest_regressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Loading