# Reading a Decision Tree

A decision tree's rules can be printed with export_text or drawn with plot_tree, one fruit's guess can be said as a sentence by following its path from the top question to a leaf, and the leaf's mix of training fruits is the probability the tree gives, but a deep tree's rules stop being readable and every rule describes the training data, not the world.

Journeyman · AI Basics · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Read a decision tree's rules from export_text or plot_tree, and say one example's path to its guess as a sentence
- Explain what a leaf's counts and predict_proba say, why a deep tree is hard to read, and why its rules describe the training data, not the world

## Key takeaways

- export_text prints a tree's questions top to bottom, and show_weights=True adds how many training rows of each class reached each leaf.
- Following one example's answers from the top question to its leaf turns the guess into a sentence a person can check.
- predict_proba gives the share of each class among the training rows in that leaf, a confidence learned from those rows, not a guarantee.
- A deep tree has too many rules to read, and every rule describes the rows the tree trained on. Different training rows can move the cut-offs.

A packer at Three Rivers holds up a fruit the machine boxed as a pear and asks, "Why a pear?" With most models, the honest answer is a page of numbers. With a decision tree, it's a sentence: "Because it weighs more than 104 grams, its skin isn't very red, and it isn't very sweet." Being able to say why a model guessed what it did is called **interpretability**, and you'll need it whenever a guess affects someone who can ask: a customer whose order was flagged, a manager deciding whether to trust the machine, or you, checking that the rules make sense before anyone relies on them.

The example is `fruit.csv` from *Exercise: Fruit Sorter*, and the tree is the one *Decision Trees* built with three questions. Each code block is one notebook cell, run in order.

## The rules, top to bottom

```python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text

fruit = pd.read_csv("fruit.csv")
features = ["weight_g", "diameter_cm", "sugar_brix", "skin_redness"]
X = fruit[features]
y = fruit["fruit"]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y)

tree = DecisionTreeClassifier(max_depth=3, random_state=0)
tree.fit(X_train, y_train)
print(export_text(tree, feature_names=features, show_weights=True))
```

```text
|--- weight_g <= 104.00
|   |--- weights: [0.00, 0.00, 120.00] class: plum
|--- weight_g >  104.00
|   |--- skin_redness <= 5.05
|   |   |--- sugar_brix <= 12.85
|   |   |   |--- weights: [9.00, 93.00, 0.00] class: pear
|   |   |--- sugar_brix >  12.85
|   |   |   |--- weights: [17.00, 14.00, 0.00] class: apple
|   |--- skin_redness >  5.05
|   |   |--- sugar_brix <= 11.25
|   |   |   |--- weights: [2.00, 9.00, 0.00] class: pear
|   |   |--- sugar_brix >  11.25
|   |   |   |--- weights: [92.00, 4.00, 0.00] class: apple
```

Each step to the right is one question deeper. Under each question come its two answers: `<=` for yes, `>` for no. Each leaf shows how many training fruits ended there, in the order of `tree.classes_` (apple, pear, plum), and the class most of them were. Read the five leaves and the tree's whole idea shows: small means plum; for the rest, less red and less sweet means pear.

## One fruit's path, as a sentence

```python
one = X_test.iloc[[2]]
print(one)
print("Guess:", tree.predict(one)[0])
print("Probabilities:", tree.predict_proba(one).round(2))
```

```text
     weight_g  diameter_cm  sugar_brix  skin_redness
116       124          6.7        12.2           4.4
Guess: pear
Probabilities: [[0.09 0.91 0.  ]]
```

Follow its answers down the rules, and say them aloud: *it weighs more than 104 grams, its redness is 5.05 or less, and its sugar is 12.85 or less, so the tree calls it a pear, as 93 of the 102 training fruits that answered the same way were.* That route is the fruit's **decision path**.

`predict_proba` is that leaf's mix: 9 apples, 93 pears and no plums out of 102 is 0.09, 0.91 and 0. It's the tree's confidence, learned from 102 fruits, not a guarantee. Look at the leaf with 17 apples and 14 pears: a fruit landing there is called an apple at about 0.55, barely better than a coin toss, and the rules alone don't show that. Always read the counts.

## The same tree as a picture

`plot_tree` draws the tree with matplotlib, the plotting package the course installed:

```python
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree

fig, ax = plt.subplots(figsize=(12, 6))
plot_tree(tree, feature_names=features, class_names=tree.classes_, filled=True, ax=ax)
plt.show()
```

![The fruit tree drawn as boxes joined by lines, three questions deep: the top box asks whether the weight is at most 104 grams; its yes branch is a box of 120 plums, and its no branch asks whether the skin redness is at most 5.05, then whether the sugar is at most 12.85 or 11.25, ending in four leaves of apples and pears whose counts match the text rules; each box also shows its Gini impurity, its number of training fruits and its majority class, colored by that class](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/2cec5361d4f448f1a0a9d06cbe648080.webp)

Each box shows its question, its Gini impurity, its number of training fruits (`samples`), their counts (`value`) and the majority `class`; `filled=True` colors each box by that class, paler when the box is mixed. The top box says `class = apple` only because 120, 120 and 120 tie, and a tie goes to the class listed first.

## When the rules stop being readable

Five leaves fit in a sentence. Remove the depth limit, and look again:

```python
deep = DecisionTreeClassifier(random_state=0)
deep.fit(X_train, y_train)
rules = export_text(deep, feature_names=features)
print("Depth:", deep.get_depth(), "Leaves:", deep.get_n_leaves())
print("Lines of rules:", len(rules.splitlines()))

apple = X_test.iloc[[12]]
print("Actually:", y_test.iloc[12])
print("Deep tree's guess:", deep.predict(apple)[0])
print("Questions on its path:", deep.decision_path(apple).sum() - 1)
```

```text
Depth: 9 Leaves: 35
Lines of rules: 103
Actually: apple
Deep tree's guess: pear
Questions on its path: 7
```

`decision_path` marks the boxes a fruit passes through; minus its leaf, that's the questions it was asked. Seven questions, some asking about weight twice, to reach a leaf built from a few training fruits: technically readable, but no packer could hold 103 lines in their head, and nobody could say what that path means about pears.

## The rules describe the training data

Split the same 450 fruits with `random_state=7` instead of 42, and grow the same three-question tree:

```python
X_train7, X_test7, y_train7, y_test7 = train_test_split(
    X, y, test_size=0.2, random_state=7, stratify=y)
tree7 = DecisionTreeClassifier(max_depth=3, random_state=0)
tree7.fit(X_train7, y_train7)
print(export_text(tree7, feature_names=features))
```

```text
|--- weight_g <= 105.50
|   |--- class: plum
|--- weight_g >  105.50
|   |--- skin_redness <= 4.95
|   |   |--- sugar_brix <= 12.95
|   |   |   |--- class: pear
|   |   |--- sugar_brix >  12.95
|   |   |   |--- class: apple
|   |--- skin_redness >  4.95
|   |   |--- sugar_brix <= 11.25
|   |   |   |--- class: pear
|   |   |--- sugar_brix >  11.25
|   |   |   |--- class: apple
```

The same questions, with cut-offs that moved: 104 became 105.50, 5.05 became 4.95. Nothing about pears changed, only which fruits the tree learned from. On this tidy data the shape held; on messier data, a different sample can change whole questions. So a cut-off like "sugar at most 12.85" is where these training fruits happened to divide, not a fact about fruit, and the tree learned what goes with being a pear in this data, not what makes a fruit one.

## Check yourself

2 questions to try on the lesson page.

## So what

A shallow tree can explain every guess in a sentence a person can check, which is rare among models, as long as you read its counts as well as its rules and remember they describe its training fruits.

## Sources

- scikit-learn, [Decision Trees](https://scikit-learn.org/stable/modules/tree.html), User Guide: trees are simple to understand and interpret, can be visualized, and use a white box model whose decisions are explained by boolean logic; a class's predicted probability is the fraction of training samples of that class in the leaf; small variations in the data might result in a completely different tree.
- scikit-learn 1.9.1, [export_text](https://scikit-learn.org/stable/modules/generated/sklearn.tree.export_text.html), API reference: a text report of all the rules in a tree; `feature_names`; `show_weights=True` exports the number of samples of each class at each leaf.
- scikit-learn 1.9.1, [plot_tree](https://scikit-learn.org/stable/modules/generated/sklearn.tree.plot_tree.html), API reference: plots a decision tree; `feature_names`, `class_names` (in ascending order), `filled=True` paints nodes by their majority class, and `impurity` shows each node's impurity.
- scikit-learn 1.9.1, [DecisionTreeClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html), API reference: `predict_proba` is the fraction of samples of each class in a leaf; a tie between classes goes to the lowest index in `classes_`; `decision_path`, `get_depth` and `get_n_leaves`.

## Key terms

- [Decision Tree](https://www.ask-codey.com/glossary/decision-tree)
- [Decision Path](https://www.ask-codey.com/glossary/decision-path)
- [Interpretability](https://www.ask-codey.com/glossary/interpretability)
- [Leaf Node](https://www.ask-codey.com/glossary/leaf-node)

## Related

- [Decision Trees](https://www.ask-codey.com/lessons/ml-decision-trees)
- [Nearest Neighbors](https://www.ask-codey.com/lessons/ml-nearest-neighbors)
- [Exercise: Fruit Sorter](https://www.ask-codey.com/lessons/exercise-ml-fruit-sorter)

Source: https://www.ask-codey.com/lessons/ml-reading-a-decision-tree
