Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #607

Interaction Features

An interaction feature captures how two features' combined effect differs from what you'd expect by looking at each one separately — a pattern a linear model can never discover on its own without being handed the combination directly.

Why Interactions Matter — A Concrete Example

Consider predicting loan default from "age" and "income" separately. Neither feature alone might be a great predictor. But the combination "older age, low income" is often unusually predictive — it can suggest a recent job loss or retirement without adequate savings, a distinct risk pattern that neither raw feature communicates on its own. A plain linear model with just age and income as separate terms cannot represent this — it needs the interaction term age × income explicitly added as a feature.

Formula

\[ \hat{y} = b_0 + b_1x_1 + b_2x_2 + b_3(x_1 \times x_2) \]

The new term \(b_3(x_1 \times x_2)\) lets the model's effective slope on \(x_1\) actually depend on the value of \(x_2\) (and vice versa) — exactly what "interaction" means statistically.

Python Implementation

import pandas as pd

df = pd.DataFrame({"age": [25, 45, 65], "income": [80000, 40000, 20000]})

df["age_income_interaction"] = df["age"] * df["income"]
print(df)

# scikit-learn's PolynomialFeatures can also generate interaction terms specifically,
# without the squared terms, using interaction_only=True
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False)
X_interact = poly.fit_transform(df[["age", "income"]])
print(poly.get_feature_names_out())   # ['age' 'income' 'age income']  -- no age^2 or income^2

Domain-Driven vs Automatic Interactions

ApproachHowTradeoff
Domain-drivenManually multiply two features you have a specific hypothesis aboutInterpretable, targeted, but relies on you already suspecting the right pair
Automatic (all pairs)PolynomialFeatures(interaction_only=True) across every feature pairComprehensive, but the feature count grows combinatorially and most pairs won't be meaningful
Model-nativeTree-based models (Random Forest, XGBoost) can capture interactions automatically via sequential splitsNo manual feature engineering needed, but less interpretable which interactions the model actually used

Practical Use Cases

  • Credit risk (age × income, debt × employment status)
  • Marketing response models (discount_offered × customer_loyalty_tier)
  • Any domain where "the effect of A depends on B" is a plausible, testable hypothesis

Common Mistakes

  • Generating every possible pairwise interaction automatically on a wide dataset — creates a huge number of mostly-noise features and significantly raises overfitting risk.
  • Adding an interaction term without also keeping the original individual features in the model — this can make the interaction's coefficient hard to interpret correctly.
  • Assuming tree-based models need explicit interaction features — they often discover useful interactions natively through sequential splits, making manual interaction engineering more valuable for linear models specifically.

Interview Relevance

Q: "When would you add an interaction feature instead of relying on the model to find the interaction itself?" For linear/logistic regression, which can't represent interactions unless explicitly given the interaction term — tree-based models, by contrast, can often capture interactions natively through their split structure without manual feature engineering.

Practice Question

You suspect that a marketing discount is more effective for new customers than returning ones. Propose the specific interaction feature you'd add to a linear model to test this hypothesis.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →