Extract Decision Paths and IF-THEN Rules from a scikit-learn Decision Tree
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
A decision tree is stored as nested dicts. An internal node has the keys `feature`, `threshold`, `left` and `right`. A leaf has only `value`. Implement three functions: `tree_from_sklearn(clf)`, `decision_path(tree, x)` and `export_rules(tree, feature_names=None, decimals=2)`.
Requirements:
- `tree_from_sklearn` converts a fitted scikit-learn `DecisionTreeClassifier` into this format. A leaf's `value` is the argmax class taken from `clf.classes_`, as a native Python value (string labels included).
- A sample goes left when `x[f] <= threshold` and right otherwise.
- `decision_path` returns `(conditions, leaf_value)`. Each condition is a tuple `(feature_idx, "<=" | ">", threshold)`, listed from the root down.
- `export_rules` returns one rule per leaf, in depth-first order with the left child first, written as `IF c1 AND c2 THEN class = v`. Thresholds are formatted to `decimals` places. Feature names come from `feature_names` when it is given and default to `x{f}` otherwise.
- A tree that is a single leaf gives an empty path and the rule `IF TRUE THEN class = v`.
Example:
```python
tree = {"feature": 0, "threshold": 2.5, "left": {"value": 0},
"right": {"feature": 1, "threshold": 1.0,
"left": {"value": 1}, "right": {"value": 2}}}
decision_path(tree, [3.0, 2.0])
# ([(0, '>', 2.5), (1, '>', 1.0)], 2)
export_rules(tree)
# ['IF x0 <= 2.50 THEN class = 0',
# 'IF x0 > 2.50 AND x1 <= 1.00 THEN class = 1',
# 'IF x0 > 2.50 AND x1 > 1.00 THEN class = 2']
```
```hint Reading the fitted tree
`clf.tree_` keeps the tree as parallel arrays indexed by node id, with node 0 as the root. Look at how it marks a node that has no children.
```
```hint Building the rules
Carry the conditions collected so far down the recursion and emit a rule only when you reach a leaf. Watch the type of the label you pull out of `clf.classes_`.
```
### Constraints & Assumptions
- The classifier is single-output, so `clf.classes_` is one array of labels.
- Features are numeric, and `x` is indexed by feature position in the same column order the model was trained on.
- Labels can be integers or strings.
### What a Strong Answer Covers
- A correct conversion from scikit-learn's node arrays, including the leaf test and turning the argmax index into a native class label.
- A path walk that applies the `<=` goes-left rule and records the conditions root first.
- A left-first depth-first export that formats names and thresholds as specified and handles the single-leaf tree.
- Why exactly one exported rule matches each sample, and the time and space cost of each function.
- Edge cases such as string labels, numpy scalar types, feature index 0 and very deep trees.
### Follow-up Questions
- How would you simplify a rule that tests the same feature more than once?
- How would you attach confidence and support to each rule?
- Could rounding thresholds to `decimals` places make an exported rule disagree with the tree? How would you prevent it?
Overview: Convert a fitted scikit-learn decision tree into nested dicts, trace the path one sample takes to its leaf, and export every root-to-leaf path as an IF-THEN rule. It tests tree traversal, reading the scikit-learn tree_ arrays, and careful handling of label types and threshold formatting.