How do you trace an individual prediction's decision path through a trained decision tree?

Quick Overview

Asks how to follow one sample from the root to a leaf of a trained decision tree and read off the conditions behind its prediction. It tests tree mechanics, the scikit-learn decision_path and apply helpers, and per-feature attribution along the path.

How do you trace an individual prediction's decision path through a trained decision tree?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

A stakeholder asks why a trained decision tree gave one specific prediction. How do you trace an individual prediction's decision path through a trained decision tree? ```hint What a node stores Each internal node holds one feature and one threshold. Think about what the comparison at a node decides, where the walk ends, and which arrays of a fitted model hold that information. ``` ### What a Strong Answer Covers - The traversal rule at each internal node (feature, threshold, which branch to take) and when the walk stops. - How to get the visited nodes and the leaf from a fitted model, for example with scikit-learn's `decision_path` and `apply` plus the `tree_` arrays. - Printing each condition next to the sample's actual feature value so the path is readable by a person. - Turning the path into per-feature contributions, and how that compares with SHAP values. ### Follow-up Questions - How would you explain a single prediction from a random forest or a gradient-boosted ensemble, where there are hundreds of paths? - The same feature is split on at three nodes of the path. How do you report its effect? - Why can path-based contributions disagree with SHAP's `TreeExplainer` values for the same prediction? - How would you find the training samples that ended up in the same leaf, and why would that help explain the prediction?

Overview: Asks how to follow one sample from the root to a leaf of a trained decision tree and read off the conditions behind its prediction. It tests tree mechanics, the scikit-learn decision_path and apply helpers, and per-feature attribution along the path.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

A stakeholder asks why a trained decision tree gave one specific prediction. How do you trace an individual prediction's decision path through a trained decision tree?

What a Strong Answer Covers Guidance

  • The traversal rule at each internal node (feature, threshold, which branch to take) and when the walk stops.
  • How to get the visited nodes and the leaf from a fitted model, for example with scikit-learn's decision_path and apply plus the tree_ arrays.
  • Printing each condition next to the sample's actual feature value so the path is readable by a person.
  • Turning the path into per-feature contributions, and how that compares with SHAP values.

Follow-up Questions Guidance

  • How would you explain a single prediction from a random forest or a gradient-boosted ensemble, where there are hundreds of paths?
  • The same feature is split on at three nodes of the path. How do you report its effect?
  • Why can path-based contributions disagree with SHAP's TreeExplainer values for the same prediction?
  • How would you find the training samples that ended up in the same leaf, and why would that help explain the prediction?