Interaction features help models learn how two or more things together affect the result. They show combined effects that single features alone might miss.
Creating interaction features in 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.
df['age_exercise'] = df['age'] * df['exercise_hours']
df['income_education'] = df['income'] * df['education_years']
df['gender_smoking'] = df['gender_encoded'] * df['smoking_status_encoded']
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.
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()}")
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.
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.