Polynomial basis and overfitting
Quick answer
Polynomial regression expands input features into powers and products, then fits a linear model to those expanded columns. It can describe curved relationships while remaining linear in its fitted coefficients. Examples are standalone local Python 3.13.5 snippets; library examples use scikit-learn 1.8.0. ByteMind execution is unverified.
Expand two features
from sklearn.preprocessing import PolynomialFeaturesp = PolynomialFeatures(degree=2,include_bias=False)print(p.fit_transform([[2,3]]).astype(int).tolist())Expected output:
[[2, 3, 4, 6, 9]]The first-order terms are followed by squares and the interaction.
How it works
With one input x, a degree-two model is b+w1x+w2x². With two inputs a,b, degree two includes a,b,a²,a*b,b²; the product is an interaction. PolynomialFeatures creates these columns, but it does not learn target relationships. Use include_bias=False when a downstream estimator already fits an intercept. A degree d expansion in p variables has C(p+d,d)-1 nonconstant columns, so memory and computation grow rapidly. Higher degree can lower training error while amplifying noise, making the curve unstable, especially outside the training range. Select degree and penalty using validation, not training error alone. A pipeline can expand features, scale the expanded columns, then fit Ridge; scaling only on the training fold avoids leakage and makes penalties more comparable. Use domain knowledge and limits on degree, input range and number of features. Evaluate errors on unseen points and check extrapolation separately.
Fit a curved relationship
from sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import PolynomialFeaturesfrom sklearn.linear_model import LinearRegressionX=[[-2],[-1],[0],[1],[2]]y=[5,2,1,2,5]for d in [1,2]: m=make_pipeline(PolynomialFeatures(d,include_bias=False),LinearRegression()).fit(X,y) print(round(float(m.predict([[3]])[0]),6))Expected output:
3.010.0The degree-two basis can represent this noiseless quadratic; x=3 is extrapolation, not evidence of universal accuracy.
Common mistakes
Do not add a bias column and separately fit an intercept unnecessarily. Do not assume degree 12 is superior to degree 2 just because training loss is smaller. Avoid massive feature expansion without a resource estimate.
Practice and next steps
Expand [a,b]=[-2,3] through degree two. Estimate the number of nonconstant columns for p=3,d=2 before trying a larger dataset. Complete the knowledge check and coding exercise. Next: Robust loss and scaling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Polynomial basis and overfitting — knowledge check
Apply polynomial basis and overfitting to new examples.
Which term represents a degree-two interaction?
How many nonconstant degree-two columns for two inputs?
Why can polynomial regression be called linear?
What is a risk of high polynomial degree?
When should expanded-feature scaling be fitted?