EXERCISE
1A decision tree asks a series of yes/no questions about your data and follows the answers down a branching path to a prediction.
Save
Think of a like a game of 20 Questions. "Is the comedian ranked above 6.5?" Yes ā "Are they from the UK?" No ā "Go to the show!" Each question splits the data into smaller, more uniform groups.
The concept:
Is Rank ⤠6.5?
āāā YES ā Don't Go (all low-ranked shows were bad)
āāā NO ā Is Nationality UK?
āāā YES ā Is Age ⤠35.5?
ā āāā YES ā Go!
ā āāā NO ā Check Experience...
āāā NO ā Go! (non-UK, high rank = always good)
Building one in Python:
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
df = pd.read_csv("shows.csv")
# Convert strings to numbers (trees need numerical data)
d = {"UK": 0, "USA": 1, "N": 2}
df["Nationality"] = df["Nationality"].map(d)
d = {"YES": 1, "NO": 0}
df["Go"] = df["Go"].map(d)
# Separate features and target
features = ["Age", "Experience", "Rank", "Nationality"]
X = df[features]
y = df["Go"]
# Build the tree
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
print("Tree built successfully!")
# Output: Tree built successfully!
> š” Key Insight: Decision trees are one of the few ML models that are human-readable. You can look at the tree and understand exactly WHY it made a decision. This "" makes decision trees popular in fields where you need to justify predictions ā like medicine, finance, and law.