Polynomial features help us find patterns that are not just straight lines. They let us use curves to better understand data.
0
0
Polynomial features in Data Analysis Python
Introduction
When you want to predict something that changes in a curve, not a straight line.
When simple straight-line models don't fit your data well.
When you want to add more detail to your model by including powers of your features.
When you want to explore relationships between features by multiplying them together.
Syntax
Data Analysis Python
from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=2, include_bias=False) X_poly = poly.fit_transform(X)
degree sets the highest power of features to include.
include_bias=False means we don't add a column of ones automatically.
Examples
This creates features with powers up to 2, including the bias (column of ones).
Data Analysis Python
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)This creates features with powers up to 3, without adding a bias column.
Data Analysis Python
poly = PolynomialFeatures(degree=3, include_bias=False) X_poly = poly.fit_transform(X)
This creates only interaction features (products of different features), no powers like x².
Data Analysis Python
poly = PolynomialFeatures(degree=2, interaction_only=True) X_poly = poly.fit_transform(X)
Sample Program
This code takes a simple list of numbers and creates new features: the original number and its square.
Data Analysis Python
import numpy as np from sklearn.preprocessing import PolynomialFeatures # Sample data: one feature with 3 values X = np.array([[1], [2], [3]]) # Create polynomial features of degree 2 poly = PolynomialFeatures(degree=2, include_bias=False) X_poly = poly.fit_transform(X) print('Original data:') print(X) print('\nPolynomial features (degree=2):') print(X_poly)
OutputSuccess
Important Notes
Polynomial features can make your model more complex, so watch out for overfitting.
Always scale your data before creating polynomial features for better results.
Higher degree means more features, which can slow down your model.
Summary
Polynomial features add powers and combinations of your original data to capture curves.
Use them when straight lines don't fit your data well.
Be careful: more features can mean more complexity and slower models.