Model Complexity
Degree 1(Underfitting)
Training Error (MSE)26.58

When Lines Fail

We've learned Linear Regression (Standard Lines). But what if the data curves? A straight line will completely miss the trend. This is called 'Underfitting'.
Continue

1. The Curved Reality

Look at this data. It's not a line. It's a curve (Parabola). If we force a straight line through it (Degree 1), the error is huge.
Continue

2. Bending the Line

The model is stubborn: `y = wx + b` is always straight. So we trick it! Instead of just `x`, we give it `x²` as a new feature. Now it can learn curves.
Continue

3. Degree 2 (Quadratic)

With `x²` added, the model fits perfectly! Equation: `y = w1*x + w2*x² + b`. This is Polynomial Regression.
Continue

4. Too Much Power (Overfitting)

What if we add `x³`, `x⁴`... up to `x¹⁰`? The model becomes too flexible. It starts chasing noise instead of the trend. This is 'Overfitting'.
Continue

Interactive Fitting

You are the Data Scientist. Drag the slider to increase Model Complexity (Degree). Watch the Error (MSE) go down, but the 'Wobble' (Overfitting) go up!
Continue

Implementation

We don't need a new algorithm. We just transform the data before feeding it to Linear Regression.

polynomial.py
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# 1. Original Data (Just X)
X = [[1], [2], [3]]

# 2. Add Powers (Feature Engineering)
# Degree 2 adds 'x^2' column
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

print(X_poly)
# Output:
# [[1, 1, 1],  <-- 1, x, x^2
#  [1, 2, 4],
#  [1, 3, 9]]

# 3. Train Standard Linear Regression
model = LinearRegression()
model.fit(X_poly, y)
Using Scikit-Learn
AlgoAnimator: Interactive Data Structures