7 routes42 sections324 units

Unit 3.8 · about 3 h

151 / 324

Feature importance caveats and class weighting effects

Importance rankings and weighted losses drive real decisions about features and thresholds. Both silently change meaning under correlation, cardinality and imbalance, so this unit teaches the distrust they deserve.

  • explain
  • measure

You can now

  • Contrast impurity, permutation and gain-based importance and predict when each misleads.
  • Measure impurity dilution under correlation and permutation stability across seeds on one dataset.
  • Explain how class weighting and resampling move recall, thresholds and probability semantics, and when to recalibrate.

Covers

  • Feature importance caveats under correlation and cardinality
  • Class weighting and resampling effects on probability semantics

Study, in this order

5 steps

  1. sklearn forest feature importance evaluation
    scikit-learn - neighbors, SVMs, trees and ensembles

    1.11.2.5 Feature importance evaluation: mean decrease in impurity, the train-only and high-cardinality flaws, the permutation alternative, and scores summing to 1.0

  2. HoML Chapter 6: feature importance in practice
    Hands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical models

    Book Chapter 6: Feature Importance (printed pp. 206-207). Notebook 06 cells: Feature Importance: the iris importance ranking loop and the MNIST pixel importance heatmap

  3. ISLP Chapter 8: random forests and variable importance
    ISLP - tree-based methods and support vector machines

    Section 8.2.1, Variable Importance Measures with Figure 8.9 (pp. 345 to 346): mean-decrease-Gini importance; then Section 8.2.2 (pp. 346 to 347): the decorrelation rationale with \(m\) near \(\sqrt{p}\)

  4. sklearn SVM unbalanced problems
    scikit-learn - neighbors, SVMs, trees and ensembles

    1.4.1.3 Unbalanced problems: class_weight multipliers including the balanced mode

  5. HoML Chapter 3: precision and recall tradeoff
    Hands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical models

    Book Chapter 3: The Precision/Recall Trade-Off (printed pp. 115-120). Notebook 03 cells: Precision/Recall Trade-off: retuning the decision threshold with precision_score and recall_score instead of trusting 0.5

Focus on

  • Impurity importance multiplies split impurity decrease by sample fraction, averages over trees and normalizes to 1. It is computed on train statistics and favors many-unique-value features, so treat it as a hypothesis, not a verdict.
  • Correlated features break both rankings. With \(x_0\) uniform, a near-copy \(x_1 = x_0\) plus 0.01 noise, a noise column and the label \(x_0 > 0.5\), a 200-tree forest splits impurity importance 0.498 and 0.498, while held-out permutation gives \(x_0\) 0.511 and \(x_1\) 0.0. Every tree needs an \(x_0\) split to finish the boundary, so \(x_0\) rescues points that a shuffled \(x_1\) misroutes but not the reverse. Yet \(x_1\) alone still scores 0.995: zero permutation importance does not mean useless.
  • class_weight balanced multiplies by \(n/(2 \times \text{class count})\), here \(\left[\begin{smallmatrix}0.546&5.957\end{smallmatrix}\right]\) at 8.5 percent prevalence (two standard-normal features, label \(x_0 + x_1 + 0.5\,\text{noise} > 2\), \(n\) 4000, 30 percent test split, seed 0). Recall rises 0.64 to 0.95 while mean predicted probability doubles from 0.082 to 0.171 against a true rate of 0.086: weighting buys recall with miscalibration.
  • After weighting or resampling, 0.5 is no longer the neutral threshold. Retune it on held-out data or recalibrate before shipping decisions.

Reproduce closed-book

Rerun the twin-plus-noise demo (2000 points, seed 0, 30 percent test split, 200 trees at seed 0) and report impurity \(\left[\begin{smallmatrix}0.498&0.498&0.004\end{smallmatrix}\right]\) with permutation \(\left[\begin{smallmatrix}0.511&0.0&0.0\end{smallmatrix}\right]\) at spread 0.017 on held-out data; then drop \(x_0\) and show \(x_1\) alone scores 0.995. Rerun the 8.5-percent-prevalence logistic pair and report the recall gain with the doubled mean probability.

Traps

  • feature_importances_ sums to 1.0, so ranks are relative: dropping a feature redistributes its mass and reshuffles the rest.
  • Permutation on train rewards overfit. Run it on held-out data with several repeats and report the spread, not just the mean.
  • Resampling to 50/50 then deciding at 0.5 exports the sampler prior. Keep original priors, recalibrate, or move the threshold explicitly.

Practice

HoML Chapter 6: importance versus permutation reconstruction

Feature Importance section: rerun the iris RandomForestClassifier ranking loop, then add permutation_importance on held-out data with 10 repeats

Explain one disagreement between the two rankings from correlation or cardinality. Skip the MNIST heatmap; the iris contrast is the exercise.

Self-test

  1. Twins read 0.498 and 0.498 in impurity but 0.511 and 0.0 in permutation. What happened?

  2. What multipliers does class_weight balanced assign at 8.5 percent prevalence, and from what formula?

  3. After weighting, mean predicted probability doubles while true prevalence stays fixed. Did accuracy necessarily improve, and what must change before deciding at 0.5?

  4. A noise feature reads 0.004 impurity against 0.0 permutation. Keep or drop it?

Completion criterion

Stop when

Your demo matches the reference impurity and permutation split, your weighting rerun shows the recall gain with the calibration cost, and you can state the threshold policy for a weighted model.

Mark passed, then open 4.1 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Hands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical models
    Aurelien Geron (O'Reilly, 2025)primaryimplementation
    Read Chapters 3, 5 and 6 and the online Appendix C notebook as the first pass through performance measures, trees, ensembles and SVMs, running notebooks 03, 05 and 06 alongside. Vary depth, estimators, learning rate, k, C and RBF width under fixed splits.
  • scikit-learn - neighbors, SVMs, trees and ensembles
    scikit-learnreferencereference
    Check official estimator constraints, complexity, preprocessing needs, missing-value behavior and probability caveats. Do not use library calls as the mechanism implementation.
  • ISLP - tree-based methods and support vector machines
    James, Witten, Hastie, Tibshirani and Taylorrequiredtheory
    Use Chapters 8 and 9 with their Python labs for the statistical account of trees, bagging, random forests, boosting and SVMs that the notebooks leave implicit, and for model-choice and feature-importance arguments.

Also in this section

  • Deep-ML - tree, ensemble and kernel problems
    Deep-MLsupportpractice
    Use 138 Best Gini-Based Split, 20 Decision Tree Learning, 307 Bagging Classifier, 21 Pegasos Kernel SVM and 280 RBF Kernel as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin, including tie and update-order conventions.
  • Probabilistic Machine Learning, Chapter 16: exemplar-based methods
    Kevin Murphysupporttheory
    Read Sections 16.1 to 16.1.3 for the KNN decision rule, the curse of dimensionality with the growing-hypercube example, and the speed section on k-d trees, hashing and approximate search.
  • XGBoost parameters and sklearn estimator interface
    XGBoost developersreferencereference
    Read the tree-booster parameter list for eta, depth, sampling and regularization names and defaults, and the sklearn page for early stopping with eval_set.
  • LightGBM parameters
    LightGBM developersreferencereference
    Read learning_rate, num_leaves, max_depth, min_data_in_leaf, feature_fraction, bagging_fraction with bagging_freq, lambda_l2 and early stopping defaults.
  • CatBoost common parameters and categorical features
    CatBoost developersreferencereference
    Read iterations, learning_rate, l2_leaf_reg, depth, grow_policy, subsample, rsm, boosting_type, use_best_model and one_hot_max_size defaults, plus native categorical handling.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels