0
0
Data Analysis Pythondata~5 mins

Creating interaction features in Data Analysis Python

Choose your learning style9 modes available
Introduction

Interaction features help us see how two or more things work together to affect a result. They can improve predictions by showing combined effects.

When you think two variables together influence the outcome more than alone.
When building a model and want to capture combined effects of features.
When analyzing sales data to see how price and advertising together affect sales.
When studying health data to check how age and exercise together impact health.
When you want to improve machine learning model accuracy by adding new features.
Syntax
Data Analysis Python
df['new_feature'] = df['feature1'] * df['feature2']
You create interaction features by multiplying or combining columns.
The new feature shows combined effect of the original features.
Examples
This creates a new feature by multiplying age and income columns.
Data Analysis Python
df['age_income'] = df['age'] * df['income']
This feature shows interaction between price and advertising spend.
Data Analysis Python
df['price_ad'] = df['price'] * df['advertising']
Here, gender is converted to numbers and multiplied by smoker status to create an interaction.
Data Analysis Python
df['gender_smoker'] = df['gender'].map({'M':1, 'F':0}) * df['smoker']
Sample Program

This code creates a new column 'age_income' by multiplying 'age' and 'income' for each row.

Data Analysis Python
import pandas as pd

# Sample data
data = {'age': [25, 32, 47], 'income': [50000, 60000, 80000]}
df = pd.DataFrame(data)

# Create interaction feature
df['age_income'] = df['age'] * df['income']

print(df)
OutputSuccess
Important Notes

Interaction features can be created by multiplication, addition, or other combinations depending on the problem.

Make sure the original features are numeric or converted to numbers before creating interactions.

Too many interaction features can make models complex, so add only meaningful ones.

Summary

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

They are created by multiplying or combining columns in your data.

Use them to improve model understanding and prediction accuracy.