# Decision Trees

> Recursive splitting on features to build interpretable if-then models, and how depth and leaf size control overfitting. Free notes, key concepts, resources and...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees
Last updated: 2026-11-28

Recursive splitting on features to build interpretable if-then models, and how depth and leaf size control overfitting.

Beginner · about 5 hours · difficulty 2/5

Learn first: [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing)

A decision tree asks a sequence of yes/no questions about features. Trees handle mixed feature types, need no scaling, capture interactions and are easy to visualize. On their own they overfit easily, but they are the building block of the strongest tabular models.

### How trees learn

- At each node, choose the feature and threshold that best reduce **impurity**: Gini or entropy for classification, variance (MSE) for regression.
- Split greedily until a stopping rule is hit.
- Leaves predict the majority class or mean target of their samples.

### Controlling complexity

- `max_depth`, `min_samples_leaf`, `min_samples_split`, `max_leaf_nodes`.
- **Cost-complexity pruning** (`ccp_alpha`) trims branches after growing.
- A fully grown tree memorizes training data: near-100% training accuracy, poor test accuracy.

### Interpretation

Plot the tree with `plot_tree` or export rules. Impurity-based feature importances are convenient but biased toward high-cardinality features; prefer permutation importance for conclusions.

**Good enough to move on** when you can show the overfitting curve of a tree as depth grows and pick a depth by cross-validation.

## Key concepts

- **Gini, entropy and variance splits**
- **Depth and leaf-size limits**
- **Cost-complexity pruning**
- **Visualizing trees**
- **Feature importance caveats**

## Mini project

Train decision trees of depth 1 to 20 on the UCI Heart Disease dataset, plot train versus validation accuracy, and visualize the best tree's top three levels.

## Check yourself

Why does a decision tree not need feature scaling, and why does it still overfit easily?

## Free resources

- [scikit-learn: Decision trees](https://scikit-learn.org/stable/modules/tree.html) (scikit-learn)
- [An Introduction to Statistical Learning (chapter 8)](https://www.statlearning.com/) (ISL)

Previous: [k-Nearest Neighbours and Naive Bayes](https://www.internhack.in/roadmaps/tracks/machine-learning/knn-and-naive-bayes) · Next: [Bagging and Random Forests](https://www.internhack.in/roadmaps/tracks/machine-learning/random-forests)

## More in Classical Supervised Learning

- [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing)
- [Linear Regression and Regularization](https://www.internhack.in/roadmaps/tracks/machine-learning/linear-regression)
- [Logistic Regression and Linear Classifiers](https://www.internhack.in/roadmaps/tracks/machine-learning/logistic-regression)
- [k-Nearest Neighbours and Naive Bayes](https://www.internhack.in/roadmaps/tracks/machine-learning/knn-and-naive-bayes)
- [Decision Trees](https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees)
- [Bagging and Random Forests](https://www.internhack.in/roadmaps/tracks/machine-learning/random-forests)
- [Gradient Boosting with XGBoost and LightGBM](https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting)
- [Support Vector Machines and Kernels](https://www.internhack.in/roadmaps/tracks/machine-learning/support-vector-machines)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
