ML Polynomial Regression
Polynomial regression bends a curve - instead of a straight line - through your data when the relationship between x and y isn't linear.
When a Straight Line Isn't Enough
Linear regression assumes the relationship between x and y is a straight line. Many real relationships aren't - traffic speed versus time of day, or reaction rate versus temperature, often rise, peak, and fall. When a scatter plot shows a clear curve rather than a straight trend, forcing a straight line through it produces a poor, misleading fit. Polynomial regression solves this by fitting a curved line instead.
Fitting a Curve with numpy.polyfit()
NumPy's polyfit() function fits a polynomial of a chosen degree to your data and returns its coefficients. Wrapping those coefficients with numpy.poly1d() gives you a callable model function you can use to predict new values or plot a smooth curve.
Example
import numpy as np
x = [1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13]
y = [100, 90, 80, 60, 60, 55, 60, 65, 70, 92, 100]
# Fit a 3rd-degree polynomial
coefficients = np.polyfit(x, y, 3)
model = np.poly1d(coefficients)
print(model)
print("Prediction at x=11:", model(11))Plotting the Fitted Curve
To visualize the fit, generate a smooth range of x values with numpy.linspace(), run them through the model, and plot the result over the original scatter points.
Example
import numpy as np
import matplotlib.pyplot as plt
x = [1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13]
y = [100, 90, 80, 60, 60, 55, 60, 65, 70, 92, 100]
coefficients = np.polyfit(x, y, 3)
model = np.poly1d(coefficients)
smooth_x = np.linspace(1, 13, 100)
plt.scatter(x, y)
plt.plot(smooth_x, model(smooth_x), color="green")
plt.title("Polynomial Regression Fit (degree 3)")
plt.show()- Use polynomial regression when a scatter plot shows a clear curve, not a straight trend.
- Higher degree means a more flexible curve, but also a higher risk of overfitting noisy data.
- numpy.polyfit(x, y, degree) returns the polynomial's coefficients.
- numpy.poly1d() turns those coefficients into a usable, callable function.
- R-squared (from sklearn.metrics.r2_score) measures how well the curve fits, just like r does for linear regression.
Example
import numpy as np
from sklearn.metrics import r2_score
x = [1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13]
y = [100, 90, 80, 60, 60, 55, 60, 65, 70, 92, 100]
coefficients = np.polyfit(x, y, 3)
model = np.poly1d(coefficients)
predicted = model(x)
print("R-squared:", r2_score(y, predicted))Exercise: ML Polynomial Regression
When is polynomial regression generally preferred over simple linear regression?