Polynomial features help us find patterns that are not straight lines by adding powers of the original data. This lets simple models learn curves and more complex shapes.
Polynomial features in ML Python
Start learning this pattern below
Jump into concepts and practice - no test required
from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=2, include_bias=False) X_poly = poly.fit_transform(X)
degree controls the highest power of features to include.
include_bias=False means we don't add a column of ones (intercept).
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)poly = PolynomialFeatures(degree=3, include_bias=False) X_poly = poly.fit_transform(X)
poly = PolynomialFeatures(degree=1)
X_poly = poly.fit_transform(X)This example shows how to turn two features into polynomial features, train a simple model, and check how well it fits.
from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error import numpy as np # Sample data: hours studied and hours slept X = np.array([[1, 2], [2, 3], [3, 4], [4, 5]]) y = np.array([5, 7, 9, 11]) # target: test score # Create polynomial features of degree 2 poly = PolynomialFeatures(degree=2, include_bias=False) X_poly = poly.fit_transform(X) # Train linear regression on polynomial features model = LinearRegression() model.fit(X_poly, y) # Predict on training data y_pred = model.predict(X_poly) # Calculate mean squared error mse = mean_squared_error(y, y_pred) print(f"Original features:\n{X}") print(f"Polynomial features:\n{X_poly}") print(f"Predictions: {y_pred}") print(f"Mean Squared Error: {mse:.4f}")
Polynomial features can increase the number of columns quickly, so use small degrees for many features.
Always scale your data before polynomial transformation if features have very different scales.
Higher degree polynomials can cause overfitting, so test carefully.
Polynomial features add powers and combinations of original features to help models learn curves.
Use PolynomialFeatures from scikit-learn to create these new features easily.
Be careful with degree size to avoid too many features or overfitting.
Practice
PolynomialFeatures in machine learning?Solution
Step 1: Understand the role of PolynomialFeatures
PolynomialFeatures generates new features by raising existing features to powers and combining them, helping models learn curves.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.Final Answer:
To create new features by adding powers and combinations of existing features -> Option AQuick Check:
PolynomialFeatures = create new polynomial features [OK]
- Confusing feature creation with normalization
- Thinking it reduces features instead of expanding
- Mixing it up with data splitting
Solution
Step 1: Check the correct import statement
PolynomialFeatures is in sklearn.preprocessing, so 'from sklearn.preprocessing import PolynomialFeatures' is correct.Step 2: Verify the degree parameter
To create degree 2 features, use degree=2 in the constructor.Final Answer:
from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=2) -> Option AQuick Check:
Import from preprocessing and set degree=2 [OK]
- Importing from wrong module
- Forgetting 'degree=' keyword
- Setting wrong degree value
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)
Solution
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].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]].Final Answer:
[[2 3 4 6 9]] -> Option CQuick Check:
Polynomial features = original + squares + products [OK]
- Including bias term when include_bias=False
- Miscomputing squares or products
- Adding extra features not in degree 2
from sklearn.preprocessing import PolynomialFeatures X = [[1, 2], [3, 4]] poly = PolynomialFeatures(degree=3) X_poly = poly.fit_transform(X) print(X_poly)
Solution
Step 1: Check input type compatibility
PolynomialFeatures accepts lists or arrays, so X as list of lists is valid.Step 2: Verify degree parameter and imports
Degree 3 is supported; no NumPy import needed if not used explicitly.Final Answer:
No error; code runs correctly -> Option BQuick Check:
PolynomialFeatures accepts lists and degree 3 [OK]
- Assuming input must be NumPy array
- Thinking degree 3 is invalid
- Expecting import errors without NumPy usage
include_bias=False?Solution
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.Step 2: Calculate combinations
C(3+3, 3) = C(6, 3) = 20; subtract 1 for no bias gives 19 features.Final Answer:
19 -> Option DQuick Check:
Features = combinations(6,3)-1 = 19 [OK]
- Forgetting to subtract bias feature
- Using wrong combination formula
- Confusing degree with number of features
