This pipeline shows how polynomial regression learns to fit curved data by adding extra features that are powers of the original input. It transforms data, trains a model, and improves predictions over time.
Data Flow - 5 Stages
1Raw Data Input
1000 rows x 1 column→Collect original single feature data points→1000 rows x 1 column
x = [1, 2, 3, 4, 5]
↓
2Polynomial Feature Expansion
1000 rows x 1 column→Create new features by raising input to powers 1 and 2 (x and x^2)→1000 rows x 2 columns
x = [1, 2, 3], transformed to [[1, 1], [2, 4], [3, 9]]
↓
3Train/Test Split
1000 rows x 2 columns→Split data into 800 training rows and 200 testing rows→Train: 800 rows x 2 columns, Test: 200 rows x 2 columns
Train features shape: (800, 2), Test features shape: (200, 2)
↓
4Model Training
Train: 800 rows x 2 columns→Fit linear regression model on polynomial features→Trained model with 2 coefficients plus intercept
Model learns weights for x and x^2 terms
↓
5Model Evaluation
Test: 200 rows x 2 columns→Predict and compare to true values to compute loss and R^2 score→Loss scalar and R^2 score scalar
Loss decreases as model learns polynomial relationship
3
0.30
0.85
Model fits data better, accuracy improves
4
0.20
0.90
Loss continues to decrease, model converging
5
0.15
0.92
Final epoch shows good fit with low loss and high accuracy
Prediction Trace - 4 Layers
Layer 1: Input Feature
Layer 2: Polynomial Feature Expansion
Layer 3: Linear Model Prediction
Layer 4: Output Prediction
Model Quiz - 3 Questions
Test your understanding
What does the polynomial feature expansion stage do?
ACalculates loss and accuracy
BSplits data into training and testing sets
CAdds new features by raising input to powers
DPredicts output values
Key Insight
Polynomial regression improves simple linear models by adding powers of input features, allowing the model to fit curved patterns in data. Training shows loss decreasing and accuracy increasing, confirming the model learns the relationship well.
Practice
(1/5)
1.
What is the main purpose of using polynomial regression instead of simple linear regression?
easy
A. To fit curved relationships between variables
B. To reduce the number of features
C. To speed up training time
D. To handle missing data automatically
Solution
Step 1: Understand linear regression limitation
Linear regression fits straight lines, which cannot capture curves in data.
Step 2: Role of polynomial regression
Polynomial regression fits curved lines by adding powers of features, capturing non-linear patterns.
Final Answer:
To fit curved relationships between variables -> Option A
Quick Check:
Polynomial regression = curved fit [OK]
Hint: Polynomial regression fits curves, not just straight lines [OK]
Common Mistakes:
Thinking polynomial regression reduces features
Assuming it speeds up training
Believing it handles missing data automatically
2.
Which of the following is the correct way to create a polynomial regression pipeline in Python using sklearn?
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
pipeline = Pipeline([
('poly', PolynomialFeatures(degree=2)),
('linear', LinearRegression())
])
easy
A. pipeline = Pipeline([('poly', PolynomialFeatures(degree=2)), ('linear', LinearRegression())])
B. pipeline = Pipeline([('linear', LinearRegression()), ('poly', PolynomialFeatures(degree=2))])
C. pipeline = Pipeline([('poly', LinearRegression()), ('linear', PolynomialFeatures(degree=2))])
D. pipeline = Pipeline([('poly', PolynomialFeatures()), ('linear', LinearRegression(degree=2))])
Solution
Step 1: Order of pipeline steps
PolynomialFeatures must come before LinearRegression to transform data first.
Step 2: Correct usage of classes and parameters
PolynomialFeatures takes degree parameter; LinearRegression does not take degree.
Final Answer:
pipeline = Pipeline([('poly', PolynomialFeatures(degree=2)), ('linear', LinearRegression())]) -> Option A
Quick Check:
PolynomialFeatures before LinearRegression [OK]
Hint: Put PolynomialFeatures before LinearRegression in pipeline [OK]
Common Mistakes:
Swapping order of pipeline steps
Passing degree to LinearRegression
Omitting degree in PolynomialFeatures
3.
Given the following code, what will print(y_pred) output?
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
X = np.array([[1], [2], [3]])
y = np.array([1, 4, 9])
pipeline = Pipeline([
('poly', PolynomialFeatures(degree=2)),
('linear', LinearRegression())
])
pipeline.fit(X, y)
y_pred = pipeline.predict(np.array([[4]]))
print(np.round(y_pred, 2))
medium
A. [10.0]
B. [8.0]
C. [4.0]
D. [16.0]
Solution
Step 1: Understand data and model
X = [[1],[2],[3]] with y = [1,4,9] fits y = x^2 perfectly.
Step 2: Predict for X=4 using polynomial degree 2
Model learns y = x^2, so prediction at 4 is 4^2 = 16.