Last updated

Decision Trees

Definition: A decision tree makes predictions by asking a series of yes/no questions about the features, like a flowchart, until it reaches an answer. They are popular because they are easy to understand.

Example — train a tree

from sklearn.tree import DecisionTreeClassifier

# features: [is_weekend, is_raining]   label: 1 = stay in, 0 = go out
X = [[0,0],[0,1],[1,0],[1,1]]
y = [0,    1,    0,    1]

model = DecisionTreeClassifier()
model.fit(X, y)

print("Weekday, no rain ->", model.predict([[0, 0]])[0])
print("Weekend, raining ->", model.predict([[1, 1]])[0])

Why people like trees

  • Easy to read — you can follow the questions
  • Handle numbers and categories
  • Need little data preparation

Combine many trees and you get a Random Forest — one of the most accurate everyday ML models.

💡 Note: 1 means "stay in". The tree learned that rain is the deciding factor here.

Try it Yourself
Output

          
Ad · responsive