Concepts 🚀 Project 🧮 Labs
🧮 Labs / ML Advanced / Decision Tree visualized — the splits + overfitting (iris)

Decision Tree visualized — the splits + overfitting (iris)

SEE the actual tree structure with its split rules. SEE how depth 3 vs depth 10 changes the decision regions. The most interpretable model.

script.py v3.1
Python output click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
🧠 How it works

Decision Tree — VISUALIZED: Each tree node asks a YES/NO question: 'petal length <= 2.45?' LEFT = yes, RIGHT = no. Follow the path to a LEAF = your prediction. GINI IMPURITY: measures how mixed the classes are at a node. Gini = 1 - Σ(p_i)². Pure node (one class) = 0. 50/50 = 0.5. The tree picks the split that reduces gini the MOST (greedy). DEPTH = overfitting dial: Depth 1 (UNDERFIT): 1 split — only separates setosa. Train + test both low. Depth 3 (GOOD): 3-4 splits — captures the real pattern. Train ≈ test. Depth 10 (OVERFIT): 20+ splits — memorizes training. Train=1.0, test drops. DECISION REGIONS: trees create RECTANGULAR regions (axis-aligned splits). This is why trees can't learn diagonal boundaries well — need many splits. WHY TREES ARE GREAT: (1) Interpretable — you can EXPLAIN any prediction by following the path. (2) No scaling needed — splits are threshold-based. (3) Handles categorical + numerical naturally. (4) Feature importance is built-in (which features are used near the root?). WHY TREES ARE BAD (alone): (1) High variance — small data change → totally different tree. (2) Overfit easily — no max_depth = memorizes everything. (3) Can't extrapolate — predictions bounded by training data range. SOLUTION: RandomForest (many trees + voting) fixes the variance problem.

✏️ Try these changes
  • Change max_depth to None (unlimited)
    → tree grows until all leaves are pure — extreme overfitting
  • Change the split criterion from 'gini' to 'entropy'
    → uses information gain instead of gini — usually similar results
  • Change min_samples_leaf from default (1) to 10
    → each leaf must have ≥10 samples — prevents tiny overfit leaves
  • Use all 4 features: X = iris.data
    → tree uses more features — deeper tree needed for same accuracy