Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 31

Decision Tree Classifier

Classify a row by a sequence of yes or no splits, and let each leaf vote with its majority class. One example is on this page.

Intermediate12 min
Decision Tree Classifier MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

A classification tree picks the split that makes the two sides purer. A leaf predicts the class that appears most often among the training rows in that leaf. The path from root to leaf is the explanation for one prediction.

Example

python
import numpy as np
from sklearn.tree import DecisionTreeClassifier

X = np.array([[1], [2], [3], [7], [8], [9]], dtype=float)
y = np.array([0, 0, 0, 1, 1, 1])
label = DecisionTreeClassifier(max_depth=1, random_state=0).fit(X, y).predict([[6]])[0]
print(f"label: {int(label)}")

Output:

text
label: 1

When to use it

  • Use it when a few thresholds should be readable by a person.
  • Limit depth. An unpruned tree can memorize every training row.
  • Class imbalance makes a leaf's majority misleading. Check the class counts.

Loading playground…