intermediatemachine-learning

Decision Trees

Flowchart-style questions that split your data into smaller groups until you can make a prediction.

Key Points

What it is

A decision tree is a model that looks like a flow chart. You start at the top (root) and ask a question about one feature, like 'is age < 30?'. Depending on the answer, you go down one branch. At the next node, you ask another question. You keep going until you reach a leaf node that gives you a prediction. For regression, the leaf gives a number (average of training values in that leaf). For classification, it gives a class (majority vote). Trees are easy to draw and explain, even to non-experts.

Why it exists

Linear models assume a straight line relationship. But many real problems have interactions and non-linearities that a straight line cannot capture. A decision tree can model complex decision boundaries like 'if young and male and lives in city, then buy product; else not'. Trees exist because they are intuitive – humans naturally think in if-then-else rules. They are also non-parametric, meaning they can fit any shape if you let them grow deep enough.

What it measures

A decision tree measures impurity (how mixed the classes are) at each node. The goal is to choose splits that decrease impurity the most. For classification, common measures are Gini impurity: probability of misclassifying a random element if you guessed according to the class distribution. And entropy: the amount of disorder. For regression, the measure is variance. The tree also measures feature importance: total reduction in impurity from splits on that feature, averaged across all nodes.

Intuition

Imagine you are trying to guess if someone will like a movie. You ask questions one by one. 'Do they like action movies?' If yes, go to the next question: 'Are they under 30?' If yes, guess 'Yes, they will like it'. If no to action, ask 'Do they like romance?' If yes, guess 'Maybe'. That is a decision tree. Each question splits the group into subgroups that are more similar to each other. The tree is built automatically by a computer that tries all possible questions and picks the one that makes the subgroups most pure.

Formulas

Gini impurity for a node

Gini=1−∑k=1Kpk2\text{Gini} = 1 - \sum_{k=1}^{K} p_k^2
  • K — number of classes
  • pₖ — proportion of class k in the node
  • Gini=0 — node is pure (all one class)
  • Gini near 1 — mixed classes

Entropy (information gain)

Entropy=−∑k=1Kpklog⁡2(pk)\text{Entropy} = -\sum_{k=1}^{K} p_k \log_2(p_k)
  • log₂ — log base 2 – gives bits of information

Information gain from a split

IG=Impurity(parent)−∑jnjnImpurity(childj)\text{IG} = \text{Impurity(parent)} - \sum_{j} \frac{n_j}{n} \text{Impurity(child}_j)
  • nⱼ — number of samples in child j
  • n — samples in parent

When to use

When not to use

Common mistakes

In practice

In scikit-learn: `from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor`. Use `model = DecisionTreeClassifier(max_depth=5, min_samples_leaf=10)` to prevent overfitting. Fit, then predict. Visualize tree with `export_graphviz` or `plot_tree`. Feature importances: `model.feature_importances_`. Tune hyperparameters like `max_depth`, `min_samples_split`, `min_samples_leaf` via cross-validation. For real work, use random forest or gradient boosting instead of a single tree.

Was this concept helpful?

Spot an error or gap? Suggest an edit →

Related concepts

random-forestgradient-boostingbagging
Chirp at me!