Polynomial Regression
# CHAPTER 11
Polynomial Regression
1. Introduction
So far, we have forced our models to draw perfectly straight lines through data. But the real world is rarely a straight line. If you plot the spread of a virus over time, it shoots up exponentially. If you plot a car's fuel efficiency against its speed, it forms a curve (terrible at 10mph, great at 55mph, terrible at 100mph). If you use a straight Linear Regression line on curved data, you will suffer from massive Underfitting. In this chapter, we introduce Polynomial Regression, giving our model the mathematical flexibility to draw curves.2. Learning Objectives
By the end of this chapter, you will be able to:- Identify non-linear relationships in data.
- Understand the math behind Polynomial terms ($x^2$, $x^3$).
-
Transform raw features using
PolynomialFeatures.
-
Fit a Polynomial Regression model in
scikit-learn.
- Recognize the severe risks of Overfitting with high-degree polynomials.
3. The Math: Bending the Line
In Simple Linear Regression, the formula is a straight line: $y = (m1 \times x) + b$To make the line curve, we simply add a "squared" version of our input feature to the equation! $y = (m
1 \times x) + (m2 \times x^2) + b$This is a Degree-2 Polynomial (a Parabola/U-shape). If the data wiggles twice (like an S-shape), we add a cubed term: $y = (m
1 \times x) + (m2 \times x^2) + (m3 \times x^3) + b$*The magic:* Even though the line is curving, the algorithm to find the weights ($m1, m2$) is the exact same Linear Regression algorithm! We are just feeding it engineered features.
4. Mini Project: Student Score Prediction Curve
Let's predict a student's Exam Score based on the Hours Studied. Notice the data curves: studying 1 hour vs 2 hours is a big jump, but studying 9 hours vs 10 hours has diminishing returns.5. The Danger of Degrees (Overfitting)
Thedegree parameter is the most dangerous hyperparameter in regression.
- Degree = 1: A straight line. (Underfitting a curve).
- Degree = 2 or 3: A smooth, logical curve. (The Sweet Spot).
- Degree = 15: The model creates a hyper-complex, chaotic squiggle that perfectly touches every single data point, but goes wildly off the chart in between points. This is extreme Overfitting (High Variance). It has memorized the training data and will fail miserably in the real world.
6. Pipeline Implementation
In production, you should combine the transformation and the regression into a singlePipeline to prevent messy code.
7. Common Mistakes
-
Forgetting to transform Test Data: If you use
PolynomialFeatureson your Training Data, your model is now trained to expect 3 columns ($1, x, x^2$). If you try to runmodel.predict()on raw Test Data that only has 1 column ($x$), the code will crash. You MUST transform the test data first, or use aPipeline!
- Extrapolation is catastrophic: Linear regression goes straight off the chart. Polynomial regression curves off the chart. If you predict data far outside your training range with a Degree-3 polynomial, the prediction will likely skyrocket to negative infinity or millions.
8. Best Practices
- Keep Degrees Low: In 99% of real-world scenarios, a Degree of 2 or 3 is the maximum you should ever use. If your data requires a Degree of 10 to fit, you should be using a completely different algorithm like a Random Forest.
9. Exercises
-
1.
If your raw input feature $X$ is
[3], what will the output array look like after passing it throughPolynomialFeatures(degree=3)? (Hint: don't forget the bias column of 1s).
-
2.
Write a Python snippet using
make_pipelineto create a Degree-4 polynomial regression model.
10. MCQ Quiz with Answers
Why is Polynomial Regression still considered a "Linear" Regression model under the hood?
What is the immediate risk of setting the polynomial degree parameter very high (e.g., degree=20)?
11. Interview Questions
-
Q: Explain how setting the
degreehyperparameter relates to the Bias-Variance tradeoff in Polynomial Regression.
- Q: Why is Extrapolation (predicting outside the training range) particularly dangerous when using Polynomial Regression compared to Simple Linear Regression?
12. FAQs
Q: Can I use Polynomial Regression with multiple features (e.g., Size and Bedrooms)? A: Yes!PolynomialFeatures will not only square Size and square Bedrooms, but it will create interaction terms (Size * Bedrooms), allowing the model to find complex relationships between different features!