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
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
| Approach | How | Tradeoff |
|---|---|---|
| Domain-driven | Manually multiply two features you have a specific hypothesis about | Interpretable, targeted, but relies on you already suspecting the right pair |
| Automatic (all pairs) | PolynomialFeatures(interaction_only=True) across every feature pair | Comprehensive, but the feature count grows combinatorially and most pairs won't be meaningful |
| Model-native | Tree-based models (Random Forest, XGBoost) can capture interactions automatically via sequential splits | No 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.