Feature Description
Implement feature importance calculation for both RandomForestClassifier and RandomForestRegressor. Feature importance helps users understand which features contribute most to predictions.
Implementation Requirements
Core Methods:
feature_importances(&self) -> Option<Vec<f32>> - Returns importance scores for each feature
- Based on Gini impurity decrease (classification) or variance reduction (regression)
- Aggregate importances across all trees in the forest
Algorithm:
- For each tree, track impurity decrease at each split
- Accumulate importance by feature index
- Normalize: importance_i = total_decrease_i / sum(all_decreases)
- Average across all trees
Testing:
- Comprehensive tests for both classifier and regressor
- Verify importances sum to 1.0
- Test with known feature relationships
- Verify reproducibility with random_state
Examples:
- Update
examples/random_forest_iris.rs to show feature importance
- Update
examples/random_forest_regression.rs to show feature importance
- Add visualization of top features
Documentation:
- Add theory section to
book/src/ml-fundamentals/ensemble-methods.md
- Explain Gini importance vs permutation importance
- When to use feature importance for model interpretation
Acceptance Criteria
References
- Breiman (2001): Random Forests - Section on variable importance
- sklearn RandomForestClassifier.feature_importances_
Feature Description
Implement feature importance calculation for both RandomForestClassifier and RandomForestRegressor. Feature importance helps users understand which features contribute most to predictions.
Implementation Requirements
Core Methods:
feature_importances(&self) -> Option<Vec<f32>>- Returns importance scores for each featureAlgorithm:
Testing:
Examples:
examples/random_forest_iris.rsto show feature importanceexamples/random_forest_regression.rsto show feature importanceDocumentation:
book/src/ml-fundamentals/ensemble-methods.mdAcceptance Criteria
feature_importances()method for RandomForestClassifierfeature_importances()method for RandomForestRegressorReferences