Twenty questions, but for data.
Direct answer
A decision tree predicts by asking a chain of questions about a data point's features, such as "is income above X?", moving down branches until it reaches a leaf with a prediction. Trees are easy to interpret, but deep trees overfit. Random forests combine many trees to improve accuracy.
Example
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
model = DecisionTreeClassifier(max_depth=3).fit(X, y)
print(model.predict([[5.1, 3.5, 1.4, 0.2]]))
Strengths and limits
| Strengths | Limits |
|---|---|
| easy to explain | overfit easily |
| handles mixed data | unstable to small changes |
| little preparation needed | less accurate than ensembles |
Control complexity
Limit max_depth or require a minimum number of samples per leaf.
Where Teyro fits
Teyro's Machine Learning for Beginners course turns this into short lessons you can play in a few minutes a day. It has 200 lessons, and you can start free.
The bottom line
Great for understanding your data. Use ensembles for accuracy.


