Bird
Raised Fist0
ML Pythonml~20 mins

Polynomial regression pipeline in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Polynomial Regression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of polynomial regression prediction
Given the following code that fits a polynomial regression model and predicts a value, what is the output?
ML Python
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1], [2], [3], [4], [5]])
y = np.array([1, 4, 9, 16, 25])  # y = x^2

poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

model = LinearRegression()
model.fit(X_poly, y)

pred = model.predict(poly.transform([[6]]))
print(round(pred[0], 2))
A36.0
B35.0
C30.0
D42.0
Attempts:
2 left
💡 Hint
Remember that the model fits y = x^2, so predicting for x=6 should be close to 36.
Model Choice
intermediate
1:30remaining
Best model choice for polynomial regression pipeline
You want to build a pipeline that fits a polynomial regression model to data. Which of the following pipeline components is the correct choice to transform the input features before fitting a linear regression?
AMinMaxScaler followed by LinearRegression
BStandardScaler followed by LinearRegression
CPolynomialFeatures followed by LinearRegression
DPCA followed by LinearRegression
Attempts:
2 left
💡 Hint
Polynomial regression requires creating polynomial features before fitting a linear model.
Hyperparameter
advanced
1:30remaining
Choosing the polynomial degree hyperparameter
In a polynomial regression pipeline, which effect does increasing the degree hyperparameter have on the model?
AIt has no effect on model complexity
BIt increases model complexity and may cause overfitting
CIt decreases model complexity and reduces overfitting
DIt always improves model generalization
Attempts:
2 left
💡 Hint
Higher degree polynomials can fit data more closely but risk fitting noise.
Metrics
advanced
1:30remaining
Evaluating polynomial regression with R² score
After fitting a polynomial regression model, you compute the R² score on test data and get 0.95. What does this value indicate?
AThe model explains 95% of the variance in the test data
BThe model has 95% accuracy in classification
CThe model's predictions are 95% correct on average
DThe model has 95% error rate
Attempts:
2 left
💡 Hint
R² score measures how well the model explains variance in continuous data.
🔧 Debug
expert
2:30remaining
Debugging pipeline with incorrect feature transformation
You build a pipeline with PolynomialFeatures(degree=3) and LinearRegression. After training, predictions are constant and do not change with input. What is the most likely cause?
APolynomialFeatures was not fitted before transforming the input
BLinearRegression was not fitted with the transformed features
CDegree parameter was set to 1 instead of 3
DInput data was not reshaped to 2D array before transformation
Attempts:
2 left
💡 Hint
PolynomialFeatures expects 2D input; wrong shape can cause incorrect transformations.

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

  1. Step 1: Understand linear regression limitation

    Linear regression fits straight lines, which cannot capture curves in data.
  2. Step 2: Role of polynomial regression

    Polynomial regression fits curved lines by adding powers of features, capturing non-linear patterns.
  3. Final Answer:

    To fit curved relationships between variables -> Option A
  4. 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

  1. Step 1: Order of pipeline steps

    PolynomialFeatures must come before LinearRegression to transform data first.
  2. Step 2: Correct usage of classes and parameters

    PolynomialFeatures takes degree parameter; LinearRegression does not take degree.
  3. Final Answer:

    pipeline = Pipeline([('poly', PolynomialFeatures(degree=2)), ('linear', LinearRegression())]) -> Option A
  4. 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

  1. Step 1: Understand data and model

    X = [[1],[2],[3]] with y = [1,4,9] fits y = x^2 perfectly.
  2. Step 2: Predict for X=4 using polynomial degree 2

    Model learns y = x^2, so prediction at 4 is 4^2 = 16.
  3. Final Answer:

    [16.0] -> Option D
  4. Quick Check:

    4 squared = 16 [OK]
Hint: Polynomial degree 2 fits squares; predict 4^2 = 16 [OK]
Common Mistakes:
  • Ignoring polynomial transformation
  • Predicting linear value instead of squared
  • Rounding errors without np.round
4.

Identify the error in this polynomial regression pipeline code:

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

pipeline = Pipeline([
    ('linear', LinearRegression()),
    ('poly', PolynomialFeatures(degree=3))
])

pipeline.fit(X_train, y_train)
medium
A. LinearRegression should not be used in pipeline
B. The order of pipeline steps is incorrect
C. PolynomialFeatures degree must be 2, not 3
D. Missing import for X_train and y_train

Solution

  1. Step 1: Check pipeline step order

    PolynomialFeatures must come before LinearRegression to transform data first.
  2. Step 2: Confirm degree and imports

    Degree 3 is valid; imports for data are assumed outside snippet.
  3. Final Answer:

    The order of pipeline steps is incorrect -> Option B
  4. Quick Check:

    PolynomialFeatures before LinearRegression [OK]
Hint: PolynomialFeatures must be first in pipeline [OK]
Common Mistakes:
  • Swapping order of steps
  • Thinking degree must be 2
  • Confusing missing data imports with pipeline error
5.

You want to model a dataset with a complex curve. You try polynomial regression with degree=2 but the fit is poor. What is the best next step?

hard
A. Remove polynomial features and use linear regression only
B. Decrease the polynomial degree to avoid overfitting
C. Increase the polynomial degree to capture more complexity
D. Use degree=2 but reduce training data size

Solution

  1. Step 1: Understand model complexity and fit

    Degree 2 polynomial may be too simple for complex curves, causing poor fit.
  2. Step 2: Adjust polynomial degree

    Increasing degree allows model to fit more complex patterns, improving fit quality.
  3. Final Answer:

    Increase the polynomial degree to capture more complexity -> Option C
  4. Quick Check:

    Higher degree = better complex fit [OK]
Hint: Raise degree to fit complex curves better [OK]
Common Mistakes:
  • Lowering degree when fit is poor
  • Removing polynomial features unnecessarily
  • Reducing data size instead of model complexity