0
0
ML Pythonml~5 mins

Creating interaction features in ML Python

Choose your learning style9 modes available
Introduction

Interaction features help models learn how two or more things together affect the result. They show combined effects that single features alone might miss.

When you think two features together change the outcome differently than alone, like age and exercise affecting health.
When simple features don't explain the data well, and you want to add more detail.
When you want to improve model accuracy by capturing relationships between features.
When working with linear models that don't automatically learn feature combinations.
When you want to explore how features work together before training a model.
Syntax
ML Python
interaction_feature = feature1 * feature2

You create interaction features by multiplying two or more features.

This works for numeric features; for categorical features, you may need encoding first.

Examples
This creates a new feature by multiplying age and exercise hours.
ML Python
df['age_exercise'] = df['age'] * df['exercise_hours']
Combines income and education years to capture their joint effect.
ML Python
df['income_education'] = df['income'] * df['education_years']
For categorical features encoded as numbers, multiply to create interaction.
ML Python
df['gender_smoking'] = df['gender_encoded'] * df['smoking_status_encoded']
Sample Model

This example creates an interaction feature by multiplying age and exercise hours. Then it trains a simple linear model to predict health score. The output shows the error and predictions.

ML Python
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# Sample data
data = {
    'age': [25, 32, 47, 51, 62],
    'exercise_hours': [3, 0, 1, 4, 2],
    'health_score': [80, 60, 70, 85, 75]
}
df = pd.DataFrame(data)

# Create interaction feature
df['age_exercise'] = df['age'] * df['exercise_hours']

# Features and target
X = df[['age', 'exercise_hours', 'age_exercise']]
y = df['health_score']

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# Train model
model = LinearRegression()
model.fit(X_train, y_train)

# Predict
y_pred = model.predict(X_test)

# Calculate error
mse = mean_squared_error(y_test, y_pred)

print(f"Mean Squared Error: {mse:.2f}")
print(f"Predictions: {y_pred.round(2).tolist()}")
OutputSuccess
Important Notes

Interaction features can increase model complexity, so use them wisely.

Always check if interaction features improve your model by comparing metrics.

For many features, consider automated tools to create interactions to avoid too many combinations.

Summary

Interaction features combine two or more features to capture their joint effect.

They help models learn relationships that single features miss.

Created by multiplying numeric features or encoded categorical features.