2.1.10Data Preprocessing & Feature Engineering

Feature creation and interaction terms

3,169 words14 min readdifficulty · medium

What Are We Really Doing?

The goal: increase the expressive power of your feature space so linear models can capture nonlinear relationships, and complex models can learn faster with less data.

Why Feature Creation Matters

The Representation Bottleneck: Most ML algorithms learn coefficients/weights on features. If your features don't encode the right relationships, no amount of training will help.

Figure — Feature creation and interaction terms

Mathematical Reality: A linear model y=w1x1+w2x2+by = w_1x_1 + w_2x_2 + b can NEVER learn y=x1x2y = x_1 \cdot x_2 no matter how much data you have—the multiplicative relationship isn't in the hypothesis space. But if you create x3=x1x2x_3 = x_1 \cdot x_2, suddenly y=w3x3+by = w_3x_3 + b works perfectly.

Types of Feature Creation

1. Polynomial Features

Derivation from scratch:

  • Why? Real-world relationships are rarely linear. Physics: E=12mv2E = \frac{1}{2}mv^2 (kinetic energy), A=πr2A = \pi r^2 (area).
  • How many features? For nn features at degree dd:
New features=(n+dd)1\text{New features} = \binom{n + d}{d} - 1
  • For n=2,d=2n=2, d=2: (42)1=61=5\binom{4}{2} - 1 = 6 - 1 = 5 new features: x12,x1x2,x22,x1,x2x_1^2, x_1x_2, x_2^2, x_1, x_2
  • Why subtract 1? The constant term 1 is redundant with the bias.

But yield might peak at moderate temperature (too hot/cold is bad) and depend on the combination of temperature and water availability.

Solution: Create degree-2 polynomial features:

[T,R,T2,TR,R2][T, R, T^2, TR, R^2]

Model becomes:

yield=w1T+w2R+w3T2+w4(TR)+w5R2+b\text{yield} = w_1T + w_2R + w_3T^2 + w_4(TR) + w_5R^2 + b

Why this works:

  • T2T^2 with negative w3w_3 captures the inverted-U shape (optimal temperature)
  • TRTR captures interaction: rain helps more when it's warm
  • R2R^2 might capture diminishing returns (too much rain floods crops)

Concrete numbers:

  • Original: T=25°C,R=10mmT=25°C, R=10\text{mm} → features [25,10][25, 10]
  • Expanded: [25,10,625,250,100][25, 10, 625, 250, 100]
  • If learned weights are [2,3,0.05,0.8,0.1][2, 3, -0.05, 0.8, -0.1]:
yield=2(25)+3(10)0.05(625)+0.8(250)0.1(100)+5=50+3031.25+20010+5=243.75\text{yield} = 2(25) + 3(10) - 0.05(625) + 0.8(250) - 0.1(100) + 5 = 50 + 30 - 31.25 + 200 - 10 + 5 = 243.75

2. Interaction Terms

Why multiplication? It encodes joint effect where the impact of one feature depends on the value of another.

Mathematical intuition: Consider a model with interaction:

y=β1x1+β2x2+β3(x1x2)+ϵy = \beta_1 x_1 + \beta_2 x_2 + \beta_3 (x_1 \cdot x_2) + \epsilon

Rearrange:

y=x1(β1+β3x2)+β2x2+ϵy = x_1(\beta_1 + \beta_3 x_2) + \beta_2 x_2 + \epsilon

The effective coefficient of x1x_1 is (β1+β3x2)(\beta_1 + \beta_3 x_2)—it changes with x2x_2! This is moderation/synergy.

Naive model:

sales=50ad_spend+200is_weekend+1000\text{sales} = 50 \cdot \text{ad\_spend} + 200 \cdot \text{is\_weekend} + 1000

Issue: This says ads have the same 50/\50/\backslash impact on weekdays and weekends. But weekend shopers might be more responsive!

Add interaction:

interaction=ad_spend×is_weekend\text{interaction} = \text{ad\_spend} \times \text{is\_weekend}

New model:

sales=50ad_spend+200is_weekend+80(ad_spend×is_weekend)+1000\text{sales} = 50 \cdot \text{ad\_spend} + 200 \cdot \text{is\_weekend} + 80 \cdot (\text{ad\_spend} \times \text{is\_weekend}) + 1000

Interpretation:

  • Weekday (is_weekend=0\text{is\_weekend}=0): sales =50ad_spend+1000= 50 \cdot \text{ad\_spend} + 1000
    • Each ad dollar → +$50 sales
  • Weekend (is_weekend=1\text{is\_weekend}=1): sales =50ad_spend+200+80ad_spend+1000= 50 \cdot \text{ad\_spend} + 200 + 80 \cdot \text{ad\_spend} + 1000
    • Simplify: sales =130ad_spend+1200= 130 \cdot \text{ad\_spend} + 1200
    • Each ad dollar → +$130 sales (2.6× more effective!)

Why this step? The interaction term 80ad_spendis_weekend80 \cdot \text{ad\_spend} \cdot \text{is\_weekend} only activates on weekends, adding extra slope to the ad_spend effect.

3. Ratio and Difference Features

When to use:

  • Ratios: Normalize by scale (price-to-earnings ratio, clicks-per-impression)
  • Differences: Capture change (temperature delta, account balance change)
  • Rates: Efficiency metrics (revenue per employee, error per request)

A model might learn: high debt → high risk. But 50kdebton50k debt on 80k income is different from 50kon50k on 30k income!

Create:

debt_to_income_ratio=total_debtannual_income=5000080000=0.625\text{debt\_to\_income\_ratio} = \frac{\text{total\_debt}}{\text{annual\_income}} = \frac{50000}{80000} = 0.625

Now the model sees relative burden, not absolute numbers. Someone with 0.625 ratio is safer than someone with 1.8 ratio, regardless of actual dollar amounts.

Why this step? Division normalizes by the denominator, creating a scale-invariant feature.

4. Domain-Specific Features

Create:

  • price_lag1 = yesterday's price
  • price_diff = today's price - yesterday's price (daily change)
  • rolling_mean_7 = average price over last 7 days (trend)
  • volatility = std(last 7 days) (risk measure)

Why? These capture temporal patterns: trends, mean-reversion, volatility clustering.

Create Haversine distance:

d=2rarcsin(sin2(ϕ2ϕ12)+cos(ϕ1)cos(ϕ2)sin2(λ2λ12))d = 2r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2-\phi_1}{2}\right) + \cos(\phi_1)\cos(\phi_2)\sin^2\left(\frac{\lambda_2-\lambda_1}{2}\right)}\right)

where ϕ\phi = latitude, λ\lambda = longitude, rr = Earth radius.

Why? Distance is the primary driver of fare. Raw coordinates don't directly encode this.

Common Mistakes

[!recall]- Explain It to a 12-Year-Old

Imagine you're trying to predict how fun a video game is using just two numbers: graphics quality (1-10) and story quality (1-10).

If you just add them up (fun=graphics+story\text{fun} = \text{graphics} + \text{story}), you're saying a game with great graphics but no story (10 + 2) is just as fun as a game with okay graphics and story (6 + 6). But that's wrong!

Feature creation is like creating a NEW number that captures what you really care about:

  • graphics×story\text{graphics} \times \text{story} = a "synergy score." If BOTH are good, this number is huge (10 × 10 = 100). If one is terrible, even if the other is great, this is small (10 × 2 = 20). This interaction captures that both need to be good together.
  • graphics2\text{graphics}^2 captures that going from 9→10 in graphics quality might matter MORE than going from 2→3 (diminishing returns at the low end, excitement at the high end).

By creating these new numbers from the old ones, you help the computer see patterns it couldn't see before. It's like giving it new "senses" to understand the world.

[!mnemonic] PRIME Features

Polynomial (powers for curves) Ratios (normalize by scale) Interactions (joint effects, products) Mathematical (logs, sqrt, domain transforms) Engineered (lags, rolling, domain-specific)

Practical Implementation

import numpy as np
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import Ridge
 
# Sample data: predicting sales from ad_spend and website_traffic
X_train = np.array([[100, 500], [150, 800], [200, 1200], [80, 400]])
y_train = np.array([5000, 7500, 12000, 3800])
 
# Create polynomial features with interactions
poly = PolynomialFeatures(degree=2, include_bias=False, interaction_only=False)
X_poly = poly.fit_transform(X_train)
 
# Features are now: [ad, traffic, ad^2, ad*traffic, traffic^2]
print("Original shape:", X_train.shape)  # (4, 2)
print("Polynomial shape:", X_poly.shape)  # (4, 5)
print("Feature names:", poly.get_feature_names_out(['ad_spend', 'traffic']))
# ['ad_spend', 'traffic', 'ad_spend^2', 'ad_spend*traffic', 'traffic^2']
 
# Always scale after creating features!
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_poly)
 
# Train model
model = Ridge(alpha=1.0)
model.fit(X_scaled, y_train)
 
# Manual interaction creation example
X_manual = X_train.copy()
interaction = X_train[:, 0] * X_train[:, 1]  # ad_spend * traffic
X_with_interaction = np.column_stack([X_train, interaction])
print("With manual interaction:", X_with_interaction.shape)  # (4, 3)

When to Use Feature Creation

Use polynomial features when:

  • You have small feature sets (< 20 features) to avoid explosion
  • Nonlinear relationships are suspected (physics, economics, biology)
  • Using linear models that need help capturing curves
  • You have sufficient data (nn \gg new feature count) to avoid overfitting

Use interaction terms when:

  • Domain knowledge suggests synergy (medication + diet, temperature + humidity)
  • A/B testing reveals different effects for different segments
  • Exploratory data analysis shows crossing effects in plots
  • Modeling moderating variables (the effect of X on Y depends on Z)

Use domain features when:

  • You have temporal data (lags, rolling stats, seasonality)
  • You have spatial data (distances, coordinates, neighborhoods)
  • You have text/categorical data (encoding tricks, embedings)
  • There are known physical laws to encode (conservation laws, rate equations)

Avoid feature creation when:

  • Using tree-based models (XGBoost, Random Forest) for polynomial features—they capture interactions and nonlinearity naturally through splits
  • You have too many features already (high-dimensional, genomics)—add more features cautiously
  • Your model is already overfitting—simplify first, add features later
  • You lack domain expertise and are creating features blindly—this is feature engineering roulette

Connections


#flashcards/ai-ml

What is the primary goal of feature creation in machine learning? :: To increase the expressive power of the feature space by transforming and combining existing variables, allowing models to capture relationships that aren't present in the raw data.

Why can't a linear model y=w1x1+w2x2+by = w_1x_1 + w_2x_2 + b ever learn a multiplicative relationship y=x1x2y = x_1 \cdot x_2? :: Because multiplication is not in the hypothesis space of linear combinations. The model can only learn additive weighted sums, not products of features. Creating the interaction term x3=x1x2x_3 = x_1 \cdot x_2 explicitly adds this relationship.

How many features result from applying degree-2 polynomial expansion to 3 original features?
(3+22)1=(52)1=101=9\binom{3+2}{2} - 1 = \binom{5}{2} - 1 = 10 - 1 = 9 features (including the 3 original linear terms,3 squared terms, and 3 pairwise interaction terms).
What does an interaction term x1×x2x_1 \times x_2 capture that individual features x1x_1 and x2x_2 cannot?
It captures how the effect of one feature depends on the value of the other (moderation/synergy). The coefficient of x1x_1 effectively becomes (β1+β3x2)(\beta_1 + \beta_3 x_2), varying with x2x_2.
Why should you create ratio features like debt-to-income instead of using absolute values?
Ratios create scale-invariant features that capture relative relationships. $50k debt means different things for30k vs \80k income—the ratio normalizes by the denominator to encode relative burden.
State the hierarchical principle for interaction terms.
Always include lower-order terms when including higher-order terms. If x1x2x_1 x_2 is in your model, both x1x_1 and x2x_2 must also be included to allow the model to separate additive and multiplicative effects.
Why must you scale features after creating polynomial or interaction terms?
Because squared and interaction features have much larger magnitudes (e.g., x12[0,10000]x_1^2 \in [0, 10000] if x1[0,100]x_1 \in [0, 100]), creating ill-conditioned optimization landscapes. Standardization puts all features on comparable scales for stable gradient descent.
When should you avoid creating polynomial features for tree-based models like Random Forest?
Tree-based models naturally capture nonlinearity and interactions through recursive splits on single features. Adding polynomial features is redundant and increases dimensionality without benefit, potentially slowing training.
What is the correct order: split data then create features, or create features then split data?
Split data FIRST, then create features using only training data statistics. Computing aggregations on the full dataset before splitting causes target leakage—test information influences feature values.
For time-series prediction, why create lag features like price_lag_1 and rolling averages?
They capture temporal dependencies: lag features encode autocorrelation (today depends on yesterday), rolling averages capture trends, and rolling standard deviations capture volatility. Raw current values lack this temporal context.

Concept Map

limited expressiveness

solved by

derives

includes

includes

includes

expands via

product of

example

boosts

lets linear models capture

log, sqrt, ratios

Raw features

Representation bottleneck

Feature creation

New variables

Polynomial features

Interaction terms

Domain transformations

Degree d expansion

Two features together

temperature × is_sunny

Expressive power

Nonlinear relationships

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Feature creation ka matlab hai ke hum apne existing data se naye variables banate hain jo hidden patterns ko expose karte hain. Socho agar tumhe ghar ki price predict karni hai sirf square footage aur number of bedrooms se—ye pura picture nahi dete. Agar tum create karo "price per square foot" (price / sqft), tab suddenly model ko efficiency ka idea mil gaya. Ye transformation data ko model ke liye "digestible" bana deta hai.

Interaction terms bohot powerful hain kyunki ye capture karte hain ke do features sath milkar kaise kaam karte hain. Example: temperature akela aur sunny weather akela ice cream sales ko fully explain nahi kar sakte, lekin "temperature × is_sunny" ye compound effect capture kar leta hai. Jab dono high hain, tab interaction term ka value explode hota hai, jo model ko bata hai ke ye combination special hai.

Domain knowledge inject karna bhi zaroori hai. Agar tum time-series data pe kaam kar rahe ho, tab "lag features" (kal ki value) aur "rolling averages" (pichle 7 din ka mean) banao. Agar geospatial data hai, tab latitude-longitude se Haversine distance calculate karo. Physics ya business rules ko explicitly features mein encode karne se model ko seekhna easy ho jata hai—warna wo raw numbers se ye relationships discover karne mein struggle karega. Feature engineering wahi hai jo average models ko great banata hai, kyunki sahi features ke sath even simple linear regression bhi nonlinear patterns capture kar sakti hai!

Go deeper — visual, from zero

Test yourself — Data Preprocessing & Feature Engineering

Connections