Page11/12
Polynomial Fitting & Curve Fitting · Page 1 of 1
Polynomial Regression
Polynomial Fitting & Curve Fitting
Polynomial Fitting with polyfit
Fit a polynomial of degree n to data points:
# Fit a quadratic (degree 2) to noisy data
coeffs = np.polyfit(x, y, deg=2)
# Returns [a, b, c] for y = ax² + bx + c
# Evaluate the polynomial
y_fit = np.polyval(coeffs, x)
Polynomial Degrees
| Degree | Formula | Use Case |
|---|---|---|
| 1 | y = ax + b | Linear regression |
| 2 | y = ax² + bx + c | Quadratic |
| 3 | y = ax³ + ... | Cubic |
| n | General n-th order | Complex curves |
Caution: Overfitting
Higher degrees fit noise, not signal. Watch your validation error!
# Compare different degrees
for deg in [1, 2, 3, 5]:
coeffs = np.polyfit(x, y, deg)
y_fit = np.polyval(coeffs, x)
rmse = np.sqrt(np.mean((y - y_fit)**2))
print(f"Degree {deg}: RMSE = {rmse:.4f}")
Multi-dimensional Fitting
For more complex models, use least squares:
# y = a*x1 + b*x2 + c*x3 + noise
# Stack features as columns, solve: X @ w = y
X = np.column_stack([x1, x2, x3, np.ones_like(x1)])
w = np.linalg.lstsq(X, y, rcond=None)[0]
Best practice: Use train/validation split to choose polynomial degree.
main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…