Skip to content

Give each tree its own seed in random forest regressor/classifier - #475

Merged
Mec-iS merged 2 commits into
smartcorelib:mainfrom
slievens:rf-seed
Oct 3, 2026
Merged

Mec-iS merged 2 commits into
smartcorelib:mainfrom
slievens:rf-seed

Conversation

@slievens

@slievens slievens commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #474

Checklist

  • [ x] My branch is up-to-date with main branch.
  • [ x] Everything works and tested on latest stable Rust.
  • [ x] Coverage and Linting have been applied

Current behaviour

In RandomForestClassifier and BaseForestRegressor, every tree gets the same seed (parameters.seed). Thus all trees use the same random feature samples (and the same random thresholds with Splitter::Random). With bootstrap: false, all trees in the regressor are the same.

New expected behaviour

Each tree gets its own fixed seed: parameters.seed + tree index. The trees use different feature samples, and the results are still reproducible for a given seed.

Change logs

Changed

  • RandomForestClassifier and BaseForestRegressor (and through it RandomForestRegressor and ExtraTreesRegressor): give each tree its own seed.

Added

  • each_tree_gets_different_feature_sample tests for BaseForestRegressor and RandomForestClassifier.

…est classifier.

Before this change, every tree of the forest used the same seed. Thus all trees picked the
same feature samples. Now each tree uses seed + tree index resulting in different features
being chosen.

Add a test that checks that the trees split on different features.
@slievens
slievens requested a review from Mec-iS as a code owner October 3, 2026 08:53
@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.75%. Comparing base (9eaae9e) to head (46651cc).
⚠️ Report is 193 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #475       +/-   ##
===========================================
+ Coverage   43.97%   63.75%   +19.77%     
===========================================
  Files          85       96       +11     
  Lines        7281     8498     +1217     
===========================================
+ Hits         3202     5418     +2216     
+ Misses       4079     3080      -999     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Mec-iS

Mec-iS commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Consider a small test showing that fitting twice with the same seed gives identical forests. That locks in reproducibility, which is the main promise of this change.

Add a test to check that the output of the random forest regressor
is deterministic given a certain seed.
@slievens

slievens commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Test added in 46651cc

@Mec-iS
Mec-iS merged commit 86b54bc into smartcorelib:main Oct 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Each tree in random forest uses the same seed

2 participants