How is Mean Decrease in Impurity feature importance computed in a Random Forest?

Last updated 5 November 2026

MDI (Gini importance) adds up the weighted impurity reduction from every split that uses a feature, across all trees. It then averages over the forest and normalizes the scores to sum to 1.

  • Per split at node t on feature j:

    • Decrease = (N_t/N) × [I(t) − (N_L/N_t)·I(L) − (N_R/N_t)·I(R)].

    • I is Gini or entropy for classification, and variance (MSE) for regression.

    • Weighting by N_t/N means splits near the root, which affect many samples, count more.

  • Aggregation:

    1. Sum the decreases over all nodes in a tree that split on j.

    2. Normalize within each tree, then average over all trees.

    3. Normalize the final scores to sum to 1 (as in feature_importances_ in scikit-learn).

  • Properties:

    • Essentially free: it is computed from the training-time tree structure.

    • It uses training data only, so it reflects how much a feature was used to fit, not how much it helps generalization.

    • It is biased toward high-cardinality features and splits credit among correlated features.

  • https://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html
  • https://www.geeksforgeeks.org/machine-learning/how-are-featureimportances-in-randomforestclassifier-determined/
  • https://datarekha.com/interview/machine-learning/feature-importance-pitfalls/
All Random Forests interview questions