Bird
Raised Fist0
ML Pythonml~20 mins

Polynomial features 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 Features Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Polynomial Feature Expansion
Which of the following best describes what polynomial features do to the original input data in machine learning?
AThey normalize the features to have zero mean and unit variance.
BThey create new features by multiplying existing features up to a specified degree, allowing models to learn nonlinear relationships.
CThey reduce the number of features by selecting only the most important ones.
DThey convert categorical features into numerical codes.
Attempts:
2 left
💡 Hint
Think about how polynomial features help linear models capture curves.
Predict Output
intermediate
2:00remaining
Output of Polynomial Feature Transformation
What is the output of the following Python code using sklearn's PolynomialFeatures with degree=2 on input [[2, 3]]?
ML Python
from sklearn.preprocessing import PolynomialFeatures
import numpy as np
poly = PolynomialFeatures(degree=2, include_bias=False)
X = np.array([[2, 3]])
X_poly = poly.fit_transform(X)
print(X_poly)
A[[2. 3. 5. 6. 9.]]
B[[1. 2. 3. 4. 6. 9.]]
C[[2. 3. 4. 6. 9.]]
D[[2. 3. 4. 5. 6.]]
Attempts:
2 left
💡 Hint
Remember include_bias=False means no constant 1 column.
Model Choice
advanced
2:00remaining
Choosing a Model for Polynomial Features
You have expanded your dataset with polynomial features of degree 3. Which model below is most suitable to avoid overfitting and handle the increased feature space?
ARidge Regression (linear regression with L2 regularization)
BLinear Regression without regularization
CDecision Tree with no depth limit
DK-Nearest Neighbors with k=1
Attempts:
2 left
💡 Hint
Think about how to control complexity when features increase.
Hyperparameter
advanced
1:30remaining
Effect of Degree Parameter in Polynomial Features
What is the main effect of increasing the degree parameter in PolynomialFeatures on the dataset?
AIt increases the number of features exponentially, which can lead to overfitting.
BIt decreases the number of features, simplifying the model.
CIt normalizes the features to a smaller range.
DIt converts continuous features into categorical bins.
Attempts:
2 left
💡 Hint
Think about how many new features are created as degree grows.
Metrics
expert
2:30remaining
Evaluating Model Performance with Polynomial Features
You trained a polynomial regression model with degree 4 and got training R² = 0.95 but test R² = 0.60. What does this indicate and which metric would best help diagnose the problem?
AThe model underfits; use Mean Absolute Error (MAE) to check errors.
BThe model is underfitting; use confusion matrix to analyze.
CThe model is perfect; use accuracy score to confirm.
DThe model overfits; use cross-validation score to assess generalization.
Attempts:
2 left
💡 Hint
High train score but low test score usually means overfitting.

Practice

(1/5)
1. What is the main purpose of using PolynomialFeatures in machine learning?
easy
A. To create new features by adding powers and combinations of existing features
B. To reduce the number of features in the dataset
C. To normalize the data between 0 and 1
D. To split the dataset into training and testing sets

Solution

  1. Step 1: Understand the role of PolynomialFeatures

    PolynomialFeatures generates new features by raising existing features to powers and combining them, helping models learn curves.
  2. Step 2: Compare with other options

    Feature reduction, normalization between 0 and 1, and splitting into training/testing sets describe different preprocessing steps, not feature creation with powers.
  3. Final Answer:

    To create new features by adding powers and combinations of existing features -> Option A
  4. Quick Check:

    PolynomialFeatures = create new polynomial features [OK]
Hint: PolynomialFeatures adds powers and combos of features [OK]
Common Mistakes:
  • Confusing feature creation with normalization
  • Thinking it reduces features instead of expanding
  • Mixing it up with data splitting
2. Which of the following is the correct way to import and create polynomial features of degree 2 using scikit-learn?
easy
A. from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=2)
B. from sklearn.linear_model import PolynomialFeatures poly = PolynomialFeatures(2)
C. import PolynomialFeatures from sklearn.preprocessing poly = PolynomialFeatures(degree=2)
D. from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(3)

Solution

  1. Step 1: Check the correct import statement

    PolynomialFeatures is in sklearn.preprocessing, so 'from sklearn.preprocessing import PolynomialFeatures' is correct.
  2. Step 2: Verify the degree parameter

    To create degree 2 features, use degree=2 in the constructor.
  3. Final Answer:

    from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=2) -> Option A
  4. Quick Check:

    Import from preprocessing and set degree=2 [OK]
Hint: Import from preprocessing and set degree=2 [OK]
Common Mistakes:
  • Importing from wrong module
  • Forgetting 'degree=' keyword
  • Setting wrong degree value
3. Given the code below, what is the output of X_poly?
from sklearn.preprocessing import PolynomialFeatures
import numpy as np
X = np.array([[2, 3]])
poly = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly.fit_transform(X)
print(X_poly)
medium
A. [[2 3 5 6 9]]
B. [[1 2 3 4 6 9]]
C. [[2 3 4 6 9]]
D. [[2 3 4 5 6 9]]

Solution

  1. Step 1: Understand PolynomialFeatures output with degree=2 and include_bias=False

    Features include original features, their squares, and pairwise products: [x1, x2, x1^2, x1*x2, x2^2].
  2. Step 2: Calculate values for X = [2, 3]

    x1=2, x2=3; x1^2=4, x1*x2=6, x2^2=9; so output is [[2, 3, 4, 6, 9]].
  3. Final Answer:

    [[2 3 4 6 9]] -> Option C
  4. Quick Check:

    Polynomial features = original + squares + products [OK]
Hint: Output includes original, squares, and cross-products [OK]
Common Mistakes:
  • Including bias term when include_bias=False
  • Miscomputing squares or products
  • Adding extra features not in degree 2
4. Identify the error in the following code snippet that uses PolynomialFeatures:
from sklearn.preprocessing import PolynomialFeatures
X = [[1, 2], [3, 4]]
poly = PolynomialFeatures(degree=3)
X_poly = poly.fit_transform(X)
print(X_poly)
medium
A. X should be a NumPy array, not a list of lists
B. No error; code runs correctly
C. Missing import for NumPy
D. Degree 3 is not supported by PolynomialFeatures

Solution

  1. Step 1: Check input type compatibility

    PolynomialFeatures accepts lists or arrays, so X as list of lists is valid.
  2. Step 2: Verify degree parameter and imports

    Degree 3 is supported; no NumPy import needed if not used explicitly.
  3. Final Answer:

    No error; code runs correctly -> Option B
  4. Quick Check:

    PolynomialFeatures accepts lists and degree 3 [OK]
Hint: PolynomialFeatures accepts lists; degree 3 is valid [OK]
Common Mistakes:
  • Assuming input must be NumPy array
  • Thinking degree 3 is invalid
  • Expecting import errors without NumPy usage
5. You have a dataset with 3 features and want to add polynomial features up to degree 3. How many features will the transformed dataset have if include_bias=False?
hard
A. 10
B. 20
C. 16
D. 19

Solution

  1. Step 1: Use formula for number of polynomial features

    Number of features = C(n + d, d) - 1 if include_bias=False, where n=3, d=3.
  2. Step 2: Calculate combinations

    C(3+3, 3) = C(6, 3) = 20; subtract 1 for no bias gives 19 features.
  3. Final Answer:

    19 -> Option D
  4. Quick Check:

    Features = combinations(6,3)-1 = 19 [OK]
Hint: Use combinations(n+d, d) minus bias if excluded [OK]
Common Mistakes:
  • Forgetting to subtract bias feature
  • Using wrong combination formula
  • Confusing degree with number of features