Friday, December 27, 2024

Impact of Regularization on Decision Tree Regression


Decision Tree Regularization Explained – Underfitting vs Overfitting

๐ŸŒณ Decision Tree Regularization – A Story of Simplicity vs Complexity

Imagine you're trying to draw a smooth curve through a messy set of points. Do you draw a simple line… or a super detailed zig-zag that passes through every point?

That exact dilemma is called: Underfitting vs Overfitting

This blog walks you through it using decision trees—step by step.


๐Ÿ“š Table of Contents


๐ŸŽฒ Step 1: Generating Data

X = np.sort(5 * rng.rand(80, 1), axis=0) y = np.sin(X).ravel() y[::5] += 3 * (0.5 - rng.rand(16))

This creates:

  • 80 random points between 0 and 5
  • A sine curve as the base pattern
  • Noise added every 5th point
๐Ÿ‘‰ Real-world data is never clean—noise simulates reality.

⚙️ Step 2: Two Competing Models

ModelSettingsBehavior
Model 1max_depth=2Simple (Underfits)
Model 2max_depth=5, min_samples_leaf=10Complex (Balanced)
regr_1 = DecisionTreeRegressor(max_depth=2) regr_2 = DecisionTreeRegressor(max_depth=5, min_samples_leaf=10) regr_1.fit(X, y) regr_2.fit(X, y)

๐Ÿ“ The Math (Made Easy)

1. Model Error

\[ Error = Bias^2 + Variance + Noise \]

Simple Meaning:

  • Bias → Too simple (misses pattern)
  • Variance → Too complex (fits noise)
  • Noise → Randomness in data
๐Ÿ‘‰ Good model = Balance between bias and variance

2. Tree Depth Effect

\[ Depth \uparrow \Rightarrow Variance \uparrow \]

\[ Depth \downarrow \Rightarrow Bias \uparrow \]

Meaning:

  • Deeper trees → more flexible → risk overfitting
  • Shallow trees → more rigid → risk underfitting

3. Leaf Constraint

\[ Leaf\ Size \uparrow \Rightarrow Smoother\ Model \]

This prevents tiny splits that memorize noise.


๐Ÿ’ป Step 3: Predictions

X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis] y_1 = regr_1.predict(X_test) y_2 = regr_2.predict(X_test)

๐Ÿ–ฅ️ CLI Output (Conceptual)

Click to Expand
Model 1 (Depth=2):
- Smooth curve
- Misses fluctuations

Model 2 (Depth=5):

* Follows data closely
* Captures more detail

  

๐Ÿ“Š Understanding the Plot

  • Orange dots → Actual noisy data
  • Blue line → Shallow tree
  • Green line → Deeper tree

๐Ÿ”ต Shallow Tree (max_depth=2)

  • Captures overall trend
  • Misses detail
  • Underfitting

๐ŸŸข Deeper Tree (max_depth=5)

  • Captures more patterns
  • More flexible
  • Risk of overfitting
๐Ÿ‘‰ The goal is NOT perfect fit—it's generalization.

๐Ÿ›ก️ What is Regularization?

Regularization controls how complex your model becomes.

Key Techniques:

  • max_depth → limits tree size
  • min_samples_leaf → prevents tiny splits

Think of it like:

“Don’t let the model memorize—force it to learn patterns.”

๐Ÿ’ก Key Takeaways

  • Shallow trees = simple but may underfit
  • Deep trees = powerful but may overfit
  • Regularization balances both
  • Math helps explain model behavior clearly

๐ŸŽฏ Final Insight

A perfect model is not the one that fits the training data best…

It’s the one that performs best on unseen data.

No comments:

Post a Comment

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts