use crate::data::{BinaryTargets, ClassTargets, DenseMatrix, RegressionTargets};
use crate::ensemble::{
ExtraTreesClassifier, ExtraTreesClassifierParams, ExtraTreesRegressor,
ExtraTreesRegressorParams, RandomForestClassifier, RandomForestClassifierParams,
RandomForestRegressor, RandomForestRegressorParams,
};
use crate::tree::MaxFeatures;
use crate::tree::{
DecisionTreeClassifier, DecisionTreeClassifierParams, DecisionTreeRegressor,
DecisionTreeRegressorParams, Splitter,
};
const SEED: u64 = 19;
fn tie_heavy() -> (DenseMatrix, RegressionTargets, BinaryTargets, ClassTargets) {
let rows = 60_usize;
let columns = 5_usize;
let mut values = Vec::with_capacity(rows * columns);
let mut targets = Vec::with_capacity(rows);
let mut labels = Vec::with_capacity(rows);
let mut classes = Vec::with_capacity(rows);
for row in 0..rows {
for column in 0..columns {
values.push((((row * 37 + column * 11) % 23) as f32) / 4.0 - 3.0);
}
targets.push(((row % 7) as f32) - 3.0);
labels.push(u8::from(row % 3 == 0));
classes.push([3_u8, 7, 10][row % 3]);
}
(
DenseMatrix::new(values, rows, columns).unwrap(),
RegressionTargets::new(targets).unwrap(),
BinaryTargets::new(labels).unwrap(),
ClassTargets::new(classes).unwrap(),
)
}
fn unique_optimum() -> (DenseMatrix, RegressionTargets, BinaryTargets) {
let rows = 32_usize;
let mut values = Vec::with_capacity(rows * 2);
let mut targets = Vec::with_capacity(rows);
let mut labels = Vec::with_capacity(rows);
for row in 0..rows {
values.push(row as f32);
values.push((row * 3) as f32);
targets.push(if row < 8 {
0.0
} else if row < 20 {
5.0
} else {
11.0
});
labels.push(u8::from(row >= 16));
}
(
DenseMatrix::new(values, rows, 2).unwrap(),
RegressionTargets::new(targets).unwrap(),
BinaryTargets::new(labels).unwrap(),
)
}
fn one_tree_forest_regressor() -> RandomForestRegressorParams {
RandomForestRegressorParams::default()
.with_n_estimators(1)
.with_bootstrap(false)
.with_max_features(MaxFeatures::All)
.with_random_state(SEED)
}
fn one_tree_forest_classifier() -> RandomForestClassifierParams {
RandomForestClassifierParams::default()
.with_n_estimators(1)
.with_bootstrap(false)
.with_max_features(MaxFeatures::All)
.with_random_state(SEED)
}
fn standalone_regressor() -> DecisionTreeRegressorParams {
DecisionTreeRegressorParams::default()
.with_max_features(MaxFeatures::All)
.with_random_state(SEED)
}
fn standalone_classifier() -> DecisionTreeClassifierParams {
DecisionTreeClassifierParams::default()
.with_max_features(MaxFeatures::All)
.with_random_state(SEED)
}
#[test]
fn a_forest_of_one_and_a_standalone_tree_are_bitwise_identical() {
let (x, y, _, _) = tie_heavy();
let view = x.as_view();
let forest = RandomForestRegressor::fit(&view, &y, one_tree_forest_regressor()).unwrap();
let tree = DecisionTreeRegressor::fit(&view, &y, standalone_regressor()).unwrap();
assert_eq!(forest.core.trees.len(), 1);
assert_eq!(
&forest.core.trees[0],
tree.packed(),
"the shared grower produced two different trees from one derived seed"
);
}
#[test]
fn both_classifier_leaf_flavours_match_their_forest_of_one() {
let (x, _, labels, classes) = tie_heavy();
let view = x.as_view();
let forest = RandomForestClassifier::fit(&view, &labels, one_tree_forest_classifier()).unwrap();
let tree = DecisionTreeClassifier::fit(&view, &labels, standalone_classifier()).unwrap();
assert_eq!(forest.binary_trees().len(), 1);
assert_eq!(&forest.binary_trees()[0], tree.packed_binary());
assert_eq!(forest.classes(), tree.classes());
let forest =
RandomForestClassifier::fit_multiclass(&view, &classes, one_tree_forest_classifier())
.unwrap();
let tree =
DecisionTreeClassifier::fit_multiclass(&view, &classes, standalone_classifier()).unwrap();
let crate::ensemble::forest::model::Forest::Multiclass(trees) = &forest.core.forest else {
unreachable!("multiclass fixture");
};
assert_eq!(trees.len(), 1);
assert_eq!(&trees[0], tree.packed_multiclass());
assert_eq!(forest.classes(), tree.classes());
}
#[test]
fn an_extra_trees_of_one_and_a_randomized_standalone_tree_are_bitwise_identical() {
let (x, y, labels, _) = tie_heavy();
let view = x.as_view();
let ensemble = ExtraTreesRegressor::fit(
&view,
&y,
ExtraTreesRegressorParams::default()
.with_n_estimators(1)
.with_max_features(MaxFeatures::All)
.with_random_state(SEED),
)
.unwrap();
let tree = DecisionTreeRegressor::fit(
&view,
&y,
standalone_regressor().with_splitter(Splitter::Random),
)
.unwrap();
assert_eq!(&ensemble.core.trees[0], tree.packed());
let ensemble = ExtraTreesClassifier::fit(
&view,
&labels,
ExtraTreesClassifierParams::default()
.with_n_estimators(1)
.with_max_features(MaxFeatures::All)
.with_random_state(SEED),
)
.unwrap();
let tree = DecisionTreeClassifier::fit(
&view,
&labels,
standalone_classifier().with_splitter(Splitter::Random),
)
.unwrap();
assert_eq!(&ensemble.binary_trees()[0], tree.packed_binary());
}
#[test]
fn the_two_families_do_not_collapse_into_each_other() {
let (x, y, _, _) = tie_heavy();
let view = x.as_view();
let forest = RandomForestRegressor::fit(&view, &y, one_tree_forest_regressor()).unwrap();
let randomized = ExtraTreesRegressor::fit(
&view,
&y,
ExtraTreesRegressorParams::default()
.with_n_estimators(1)
.with_max_features(MaxFeatures::All)
.with_random_state(SEED),
)
.unwrap();
assert_ne!(forest.core.trees[0], randomized.core.trees[0]);
}
#[test]
fn the_reference_property_holds_where_the_optimum_is_unique() {
let (x, y, labels) = unique_optimum();
let view = x.as_view();
let forest = RandomForestRegressor::fit(&view, &y, one_tree_forest_regressor()).unwrap();
let tree = DecisionTreeRegressor::fit(&view, &y, standalone_regressor()).unwrap();
assert_eq!(forest.predict(&view).unwrap(), tree.predict(&view).unwrap());
let forest = RandomForestClassifier::fit(&view, &labels, one_tree_forest_classifier()).unwrap();
let tree = DecisionTreeClassifier::fit(&view, &labels, standalone_classifier()).unwrap();
assert_eq!(forest.predict(&view).unwrap(), tree.predict(&view).unwrap());
assert_eq!(
forest.predict_proba(&view).unwrap(),
tree.predict_proba(&view).unwrap()
);
for seed in [0_u64, 1, 2, 3, 4] {
let forest = RandomForestRegressor::fit(
&view,
&y,
one_tree_forest_regressor()
.with_random_state(seed)
.with_max_depth(Some(2)),
)
.unwrap();
let tree = DecisionTreeRegressor::fit(
&view,
&y,
standalone_regressor()
.with_random_state(seed)
.with_max_depth(Some(2)),
)
.unwrap();
assert_eq!(&forest.core.trees[0], tree.packed(), "seed {seed}");
}
}
#[test]
fn the_identity_is_of_member_zero_and_not_of_the_public_seed() {
let (x, y, _, _) = tie_heavy();
let view = x.as_view();
let forest =
RandomForestRegressor::fit(&view, &y, one_tree_forest_regressor().with_n_estimators(3))
.unwrap();
let tree = DecisionTreeRegressor::fit(&view, &y, standalone_regressor()).unwrap();
assert_eq!(&forest.core.trees[0], tree.packed());
assert_ne!(&forest.core.trees[1], tree.packed());
assert_ne!(&forest.core.trees[2], tree.packed());
}