Decision tree panic - #470
Conversation
Make sure predict (and cousins) do not panic when called on a learner that hasn't been fit yet. The following learners all follow the same pattern and were easy to fix. - src/ensemble/extra_trees_regressor.rs - src/ensemble/random_forest_regressor.rs - src/naive_bayes/bernoulli.rs - src/naive_bayes/categorical.rs - src/naive_bayes/gaussian.rs - src/naive_bayes/multinomial.rs - src/tree/decision_tree_regressor.rs
Following classes got a guard against calling predict without fit. Test cases were added. - src/neighbors/knn_classifier.rs - src/neighbors/knn_regressor.rs - src/svm/svc.rs - src/svm/svr.rs
Changes to the following classes with tests. - src/linear/elastic_net.rs - src/linear/lasso.rs - src/linear/linear_regression.rs - src/linear/logistic_regression.rs - src/linear/ridge_regression.rs
Added for - src/ensemble/random_forest_classifier.rs - src/tree/decision_tree_classifier.rs Tests were added.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #470 +/- ##
===========================================
+ Coverage 43.97% 63.65% +19.67%
===========================================
Files 85 96 +11
Lines 7281 8513 +1232
===========================================
+ Hits 3202 5419 +2217
+ Misses 4079 3094 -985 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
predict changed and test added
|
@slievens Great contribution — the fix is well-scoped and the consistent error message makes it easy to match on downstream. A few issues to address and suggestions below. Issues to Address1. Inconsistent guard strategiesTwo patterns are used interchangeably with no apparent reason:
The 2. Missing test:
|
- Removed all is_none and replaced by match. - Added missing tests - Fixed typo - Fixed lint issue
|
Issues 1 to 5 addressed in commit 6e81d2e. |
|
Reviewed head Minor cleanup: I reviewed the diff and CI results, but did not run tests locally. |
- Removed redudant guard in lasso.rs - Added match + test to decision_function in svc.rs so that it no longer panics.
|
Both remaining issues have been addressed in 5526859 |
* fix: address review nits from #470 - RandomForestClassifier::predict_oob: fold the leftover is_none() guard into a single match on (&self.trees, &self.classes), matching Lasso. - XGRegressor::predict: match on (&self.parameters, &self.regressors) to remove the latent unwrap when the two fields are out of sync. - DecisionTreeClassifier: comment why nodes.is_empty() is the guard. - Add wasm_bindgen_test attributes to the new should_not_panic tests. - CHANGELOG: record the unfitted-predict fix and the MultiClassSVC error-message change. * chore: bump patch 0.6.15 -> 0.6.16 and stamp the changelog entry
Fixes #469
Checklist
Current behaviour
Many "predict" and related methods panic when fit hasn't been called.
New expected behaviour
"predict" no longer panics in said case, but returns a Result.
Change logs
Changed
Note
The not-fitted error message of
MultiClassSVC::predictchanged from "MultiClassSVC is not fitted" to the common "'fit' should be called before calling 'predict'". Code that matches on the old string needs to be updated. See the release notes.