Decision trees and readable rules.
Follow a sequence of splits and understand why deep trees memorise.
Start with the meaning.
A decision tree asks a sequence of questions. At each internal node, one feature is compared with a threshold or category rule. Depending on the answer, the example moves left or right. At a leaf, the tree returns a prediction. A classification leaf may choose the most common class among the training examples that reached it; a regression leaf may return their average. A path from root to leaf can be read as an if/else rule.
The fitting algorithm chooses splits that make resulting groups more uniform according to a criterion. For classification, impurity measures how mixed the labels are in a group. A group with one class is pure, while a mixed group is less pure. For regression, the criterion often reflects variation of target values within leaves. Splits are chosen from training examples, so a tree may discover a useful rule or a coincidence tied to that sample.
How the idea works.
A tree can keep splitting until leaves contain very few examples. Training error may then become tiny, because the model has enough branches to isolate unusual rows. But a rule based on one rare row is fragile. Maximum depth, minimum samples per leaf, and related hyperparameters constrain complexity. A shallow tree is easier to read and often generalises better, though it can underfit a genuinely complex pattern.
Trees handle nonlinear relationships and interactions without requiring you to write each interaction explicitly. A split on area can be followed by a different split on rooms only in one area range. They also work without numeric feature scaling because a threshold depends on order, not distance. However, a small change in training data can alter the chosen splits, especially near ties. A tree's apparent readability does not prove its rule is stable.
Feature importance values from a fitted tree summarise how much particular splits reduced its training criterion. They do not prove causality, and some importance methods favor features with many possible split points. Looking at one path explains one prediction under that tree, but it does not tell whether the data collection process was fair or whether the path will remain valid on future cases.
Follow the code.
from sklearn.tree import DecisionTreeClassifier, export_text
X = [[1], [2], [3], [7], [8], [9]]
y = ["small", "small", "small", "large", "large", "large"]
model = DecisionTreeClassifier(max_depth=2, random_state=0)
model.fit(X, y)
print(export_text(model, feature_names=["measurement"]))
print(model.predict([[8]]))fit examines the six feature rows and their labels. A threshold between 3 and 7 separates the classes, so the displayed text representation contains a simple branch on measurement. The exact threshold chosen by the implementation may be a midpoint. The row [[8]] follows the branch for larger measurements and receives 'large'. max_depth=2 limits how many split levels the tree may create; the learner does not need all of them for this clear pattern.
The output is a rule learned from examples, not a rule the programmer wrote by hand. The labels have no intrinsic numerical meaning. If a new value lies far outside the training range, the tree still sends it to one of its existing leaves rather than explaining that it has never seen that region. A product may need a separate out-of-range policy.
What the result does not say.
A tree is useful for communicating conditional structure, but a deep tree quickly becomes difficult to read. Even a short path can encode a shortcut from biased data. The final test must reflect future conditions.
For categorical columns, encoding rules depend on the implementation. A category's numeric code should not accidentally imply an order that the category does not have. Preprocessing remains part of the complete model.
One clear idea.
A tree learns if/else splits; control its depth and judge the resulting rule on unseen data.
The ML cookbook
Complete, explained Python workflows sit alongside this reading course.