Linear models are the bedrock of ML, but the world is rarely straight. Polynomial Regression allows us to adapt linear algorithms to non-linear datasets by projecting features into higher dimensions.
1Beyond the Straight Line
When data trends follow a curve (like population growth or viral spread), a straight line creates high Bias (Underfitting). Polynomial Regression solves this by adding powers of the independent variables (xΒ², xΒ³, etc.) to the equation, allowing the 'line' to bend and follow the data points more closely.
2Algebraic Transformation
In Scikit-Learn, we don't use a different model; we use a different preprocessor. PolynomialFeatures transforms your single feature X into a matrix containing X, XΒ², XΒ³, and so on. We then feed this transformed matrix into a standard Linear Regression model, which fits the curve mathematically.
3The Overfitting Trap
The biggest danger in Polynomial Regression is High Variance (Overfitting). If you set the degree too high, the curve will bend perfectly to hit every single training point, capturing random noise instead of the actual trend. This makes the model useless for predicting new, unseen data.
4Step-by-Step Breakdown
Linear regression is great, but real-world data rarely falls in a straight line. Sometimes we need a curve. That's where Polynomial Regression comes in.
It is actually a 'linear' model, but we engineer new features by raising our inputs to a power (like x-squared) before training.
Checkpoint: If X is [10] and you apply PolynomialFeatures(degree=2), what will the output look like?
- β[10, 20]
- β[10, 100]
Once transformed, we use the standard LinearRegression class. It fits a straight line in the high-dimensional space, which looks curved in 2D.
The result is a model that can follow complex data patterns, like the salary growth of a professional over their entire career.
Warning: Setting the degree too high (e.g., degree=10) causes OVERFITTING. The model memorizes noise instead of the signal.
Checkpoint: What happens to a model when it is 'Overfitted'?
- βBetter accuracy on new data
- βPoor performance on new data
Mastering curves is a major step toward Deep Learning. You can now model complex, non-linear realities.
Transform Real Data into Polynomial Features. Finish transforming a single feature into degree-2 polynomial features and confirm the new shape and values.
Level Up π
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Describe the Curve Shape in Words, Not Just the Plot
A plotted polynomial curve conveys underfitting or overfitting visually, but a screen reader user gets nothing from the image alone β pair every fitted-curve plot with a text summary like 'the curve follows the general upward trend but does not chase every individual point', so the bias/variance takeaway isn't locked inside a `<canvas>` or `<img>`.
<figure><img src="fit.png" alt="Degree-2 curve follows the overall trend without fitting every noisy point" /></figure>SEO Implications
- 1
Rank for 'Bias-Variance Tradeoff' Searches, Not Just 'Polynomial Regression'
Underfitting and overfitting are the concepts learners actually search for when a model performs badly β a page that explicitly ties PolynomialFeatures and the degree parameter to those search terms captures more relevant traffic than one that only describes the API surface.
Best Practices
Always Scale Features Before Generating High-Degree Polynomial Terms
Raising a feature to the 5th or 10th power can produce enormous numeric values that destabilize gradient-based solvers and make coefficients hard to interpret. Apply StandardScaler (or similar) either before PolynomialFeatures or as part of the same Pipeline.
Pick the Degree with Cross-Validation, Not by Eyeballing the Training Fit
A curve that looks like it fits the training data perfectly is often overfit β use k-fold cross-validation or a held-out validation set to compare degrees objectively instead of trusting how the training-set plot looks.
Frequent Bugs
Calling PolynomialFeatures().fit_transform() separately on the training and test sets, producing feature matrices with different columns or scales.
Fit the PolynomialFeatures transformer once on the training data only, then use transform() (not fit_transform()) on the test data β exactly as you would with a scaler β so both sets get the same polynomial expansion.
Real-World Examples
Modeling Salary Growth Over a Career
A model predicting salary from years of experience is a textbook case where the relationship curves β early-career raises come faster than late-career ones β so a degree-2 or degree-3 PolynomialFeatures transform captures the diminishing-returns shape a straight line cannot.
poly = PolynomialFeatures(degree=3)
X_poly = poly.fit_transform(X_train)
model = LinearRegression().fit(X_poly, y_train)