Concepts 🚀 Project 🧮 Labs
🧮 Labs / ML Advanced / Random Forest visualized — many trees + feature importance (wine)

Random Forest visualized — many trees + feature importance (wine)

SEE how 100 trees vote. SEE feature importance. SEE OOB error stabilize. The wine dataset (13 features, 3 classes).

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

Random Forest — VISUALIZED: WHAT: 100+ decision trees + majority vote. The wisdom of the crowd. HOW IT WORKS: (1) BOOTSTRAP: each tree gets a random sample of rows (with replacement). ~63% unique rows per tree. (2) RANDOM FEATURES: at each split, only √n_features are considered. Prevents all trees from picking the same split. (3) VOTE: final prediction = majority vote (classification) or average (regression). WHY IT WORKS (BAGGING): Single tree: high variance (change 1 row → different tree). Many trees: variance averages out. Same bias, lower variance. Mathematically: Var(average of n) = Var(single) / n. More trees = less variance. OOB SCORE (free validation): Each tree trains on ~63% of data. The other ~37% (out-of-bag) is unseen. OOB score = accuracy on OOB samples. No need for a separate validation set. FEATURE IMPORTANCE: Measured by how much each feature REDUCES gini impurity across ALL trees. Summed + averaged. Higher = more important. On wine: 'flavanoids' and 'color_intensity' are usually top — they're chemically meaningful. HOW MANY TREES: 1 tree = high variance. 10 trees = good. 100 = stable. 200+ = no improvement. The curve PLATEAUS — more trees don't help after ~100. Just wastes memory. NEVER increases overfitting (unlike max_depth). More trees = always better or same.

✏️ Try these changes
  • Change n_estimators from 100 to 5
    → fewer trees → higher variance → test accuracy drops slightly
  • Change max_features from default (sqrt) to 1.0 (all features)
    → trees are more correlated → bagging helps less
  • Set oob_score=False
    → can't track OOB — need a separate validation set
  • Use load_breast_cancer instead of load_wine
    → 30 features → different importance ranking