Sometimes, the predictive power of a feature isn't constant; it changes depending on the value of another feature. For instance, the impact of advertising spending on sales might be much stronger during holiday seasons than at other times of the year. Similarly, in a medical context, the effect of a medication might depend on a patient's age. Models, especially linear ones, might struggle to capture these combined effects using only the original features. This is where interaction features become valuable.
Interaction features are new features created by combining two or more existing features. The goal is to explicitly represent the combined effect or "interaction" between these variables. The most common way to create an interaction feature is by multiplying the values of the original features.
Consider two features, f1 and f2. An interaction feature, finteraction, can be created by multiplying them:
finteraction=f1×f2
Why is this helpful? Let's look at a simple linear model:
y=β0+β1f1+β2f2
In this model, the effect of f1 on the target y is always β1, regardless of the value of f2. Similarly, the effect of f2 is always β2.
Now, let's add the interaction term:
y=β0+β1f1+β2f2+β3(f1×f2)
The effect of a change in f1 on y is now β1+β3f2. Notice that the effect of f1 is no longer constant; it depends on the value of f2. Likewise, the effect of f2 on y is β2+β3f1, depending on the value of f1. The interaction term allows the model to learn how the relationship between one feature and the target changes based on the level of another feature.
While multiplication is the most common operation for creating interactions, other operations like addition, subtraction, or division might sometimes be meaningful depending on the context, although they are used less frequently for standard interaction terms.
Creating basic interaction features in Pandas is straightforward using standard arithmetic operations on DataFrame columns.
import pandas as pd
# Sample DataFrame
data = {'feature_A': [1, 2, 3, 4, 5],
'feature_B': [10, 8, 6, 4, 2],
'target': [15, 21, 20, 19, 12]}
df = pd.DataFrame(data)
# Create an interaction feature by multiplying feature_A and feature_B
df['interaction_A_B'] = df['feature_A'] * df['feature_B']
print(df)
# feature_A feature_B target interaction_A_B
# 0 1 10 15 10
# 1 2 8 21 16
# 2 3 6 20 18
# 3 4 4 19 16
# 4 5 2 12 10
This new interaction_A_B
column can now be used alongside the original features during model training.
Scikit-learn's PolynomialFeatures
transformer, primarily used for generating polynomial terms (which we'll cover next), can also be used to create interaction features specifically.
By setting the interaction_only
parameter to True
, PolynomialFeatures
will generate only the products of combinations of features up to the specified degree
. If degree=2
, it will compute the product of all pairs of features. If degree=3
, it will compute the product of all pairs and triplets of features.
import pandas as pd
from sklearn.preprocessing import PolynomialFeatures
# Sample DataFrame
data = {'feature_A': [1, 2, 3],
'feature_B': [10, 8, 6],
'feature_C': [0, 1, 0]}
df = pd.DataFrame(data)
# Initialize PolynomialFeatures to generate interaction terms only (degree 2)
# include_bias=False prevents adding a column of ones (intercept)
poly = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False)
# Fit and transform the data
interaction_features = poly.fit_transform(df[['feature_A', 'feature_B', 'feature_C']])
# Get the names of the new features
feature_names = poly.get_feature_names_out(['feature_A', 'feature_B', 'feature_C'])
# Create a DataFrame with the new features
df_interactions = pd.DataFrame(interaction_features, columns=feature_names)
print(df_interactions)
# feature_A feature_B feature_C feature_A feature_B feature_A feature_C feature_B feature_C
# 0 1.0 10.0 0.0 10.0 0.0 0.0
# 1 2.0 8.0 1.0 16.0 2.0 8.0
# 2 3.0 6.0 0.0 18.0 0.0 0.0
Notice that PolynomialFeatures
outputs the original features along with the interaction terms (e.g., fA×fB, fA×fC, fB×fC). If you only want the interaction terms themselves, you would need to select the appropriate columns after the transformation. If interaction_only
is set to False
(the default), PolynomialFeatures
also generates polynomial terms like fA2, fB2, etc., in addition to the interaction terms.
Imagine a classification problem where two classes (blue circles and red squares) are difficult to separate using only features f1 and f2 with a linear boundary.
Data points plotted based on their original features f1 and f2. A simple linear boundary struggles to separate the classes effectively.
Now, let's create an interaction feature finteraction=f1×f2. If we plot f1 against this new interaction feature, the classes might become linearly separable.
Data points plotted using f1 and the interaction term f1×f2. The classes now appear more linearly separable.
This visualization illustrates how an interaction term can transform the feature space, potentially making it easier for certain models (especially linear ones) to find a good decision boundary.
While powerful, creating interaction features requires careful consideration:
StandardScaler
or MinMaxScaler
) before creating multiplicative interaction terms.Consider creating interaction features when:
Creating interaction features is often an iterative process. You might hypothesize certain interactions based on data exploration or domain knowledge, build models with them, evaluate performance, and refine your feature set accordingly. They represent a way to manually inject non-linearity and combined effects into your feature set, potentially leading to more accurate and insightful models.
© 2025 ApX Machine Learning