Last updated

Polynomial Regression

Definition: When data follows a curve rather than a straight line, polynomial regression fits a curved line instead. You choose the "degree" — how bendy the curve can be.

Example — fit a curve

Here y grows like x squared. A degree-2 polynomial captures that curve:

import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([1, 4, 9, 16, 25, 36, 49, 64])

coeffs = np.polyfit(x, y, 2)   # degree 2 = a curve
model = np.poly1d(coeffs)

print("Predicted y at x = 9:", round(model(9), 1))
print("Predicted y at x = 10:", round(model(10), 1))

Choosing the degree

  • Degree 1 = straight line
  • Degree 2 = one curve (a parabola)
  • Very high degree = wiggly line that overfits — it memorises the data instead of learning the trend

💡 Watch out: a more complex curve is not always better. Overfitting makes predictions on new data worse, not better.

Try it Yourself
Output

          
Ad · responsive