Fix off-by-one in tree growth loop for DecisionTreeClassifier and BaseTreeRegressor - #464
Conversation
Changed the tree growth loop in base_tree_regressor.rs and decision_tree_classifier.rs. Added two tests. Removed the unused `depth` method in BaseTreeRegressor.
|
Reviewed the diff and traced through the logic against the new tests — nice fix, and good catch on top of the issue report. A couple of notes: Correctness looks right, and slightly better than the issue's suggestion. The issue proposed Dead code removal is safe. One thing worth double-checking: Question on the Otherwise this looks solid: targeted, minimal diff, and both regressor/classifier paths get coverage. Thanks for tackling this! |
|
I found the actual root cause behind that The bugBoth trees gate whether a node is eligible to split using
With the classifier's Proposed fix1. Fix the off-by-one in // before
if n <= self.parameters().min_samples_split {
return false;
}
// after — align with base_tree_regressor.rs's convention
if n < self.parameters().min_samples_split {
return false;
}2. Update the let parameters = DecisionTreeClassifierParameters {
max_depth: Some(3),
min_samples_leaf: 1,
min_samples_split: 2, // matches base_tree_regressor's equivalent test
seed: None,
criterion: SplitCriterion::Gini,
};I traced this dataset through both formulas: for 3. Optional hardening: add a small dedicated test with Want me to post this as a follow-up review comment on PR #464, or open a separate GitHub issue for the Citations: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #464 +/- ##
===========================================
+ Coverage 43.97% 64.01% +20.03%
===========================================
Files 85 96 +11
Lines 7281 8359 +1078
===========================================
+ Hits 3202 5351 +2149
+ Misses 4079 3008 -1071 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Also added a fix for issue 463. |
#466) Add min_samples_split_boundary regression tests for DecisionTreeClassifier and BaseTreeRegressor: a node holding exactly min_samples_split samples must still split, and a node with fewer samples must stay a leaf. The full_depth tests now also assert tree depth (3), guarding the public DecisionTreeClassifier::depth accessor. Bump patch version to 0.6.15. Fixes #465
Changed the tree growth loop in base_tree_regressor.rs and decision_tree_classifier.rs. Added two tests.
Removed the unused
depthmethod in BaseTreeRegressor.Fixes #462
Checklist
Current behaviour
Last level of tree not fully constructed.
New expected behaviour
Last level of tree fully constructed.
Change logs
Added
Two tests.
Changed