Polynomial Regression 📈
When Lines Don't Fit
If your data points clearly will not fit a straight line, you might need Polynomial Regression. It fits a curved line to the data.
✨ Creating a Model
NumPy has a method polyfit() to make a polynomial model.
python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score
x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
# 3 means 3rd degree polynomial
model = np.poly1d(np.polyfit(x, y, 3))
line = np.linspace(1, 22, 100)
plt.scatter(x, y)
plt.plot(line, model(line))
plt.show()📊 R-Squared Score
Just like in Linear Regression, we need to know how well the model fits the data.
python
print(r2_score(y, model(x))) # 0.94 (Excellent fit)💡 Did You Know?
Over-fitting happens if you choose a degree that is too high. The curve might pass through every point but fail to predict new data accurately.