Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Creating interaction features in Data Analysis Python - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Interaction Features Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of interaction feature multiplication
What is the output DataFrame after creating an interaction feature by multiplying columns 'A' and 'B'?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df['A_B'] = df['A'] * df['B']
print(df)
A{'A': [1, 2, 3], 'B': [4, 5, 6], 'A_B': [4, 5, 6]}
B{'A': [1, 2, 3], 'B': [4, 5, 6], 'A_B': [5, 7, 9]}
C{'A': [1, 2, 3], 'B': [4, 5, 6], 'A_B': [1, 2, 3]}
D{'A': [1, 2, 3], 'B': [4, 5, 6], 'A_B': [4, 10, 18]}
Attempts:
2 left
💡 Hint
Multiply each value in column 'A' by the corresponding value in column 'B'.
❓ data_output
intermediate
1:30remaining
Number of unique interaction features created
Given a DataFrame with columns 'X', 'Y', and 'Z', how many unique pairwise interaction features can be created by multiplying two different columns?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'X': [1,2], 'Y': [3,4], 'Z': [5,6]})

# Interaction features are created by multiplying pairs of different columns
pairs = [(a,b) for i,a in enumerate(df.columns) for b in df.columns[i+1:]]
print(len(pairs))
A3
B6
C9
D2
Attempts:
2 left
💡 Hint
Count all unique pairs without repetition or order.
🔧 Debug
advanced
2:00remaining
Identify the error in interaction feature creation
What error will this code raise when trying to create an interaction feature by multiplying columns 'A' and 'B'?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': ['4', '5', '6']})
df['A_B'] = df['A'] * df['B']
AValueError: invalid literal for int() with base 10
BTypeError: unsupported operand type(s) for *: 'int' and 'str'
CNo error, output is a column of strings repeated
DTypeError: can't multiply sequence by non-int of type 'int'
Attempts:
2 left
💡 Hint
Check the data types of columns before multiplication.
🚀 Application
advanced
2:30remaining
Creating polynomial interaction features with scikit-learn
Which code snippet correctly creates interaction features (degree 2) from a numeric DataFrame using scikit-learn's PolynomialFeatures?
Data Analysis Python
import pandas as pd
from sklearn.preprocessing import PolynomialFeatures

df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]})
poly = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False)
X_poly = poly.fit_transform(df)
print(X_poly)
A
poly = PolynomialFeatures(degree=2, interaction_only=False, include_bias=True)
X_poly = poly.fit_transform(df)
B
poly = PolynomialFeatures(degree=3, interaction_only=True, include_bias=False)
X_poly = poly.fit_transform(df)
C
poly = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False)
X_poly = poly.fit_transform(df)
D
poly = PolynomialFeatures(degree=2, interaction_only=True, include_bias=True)
X_poly = poly.fit_transform(df)
Attempts:
2 left
💡 Hint
Interaction features exclude powers of single features and bias adds a column of ones.
🧠 Conceptual
expert
1:30remaining
Effect of interaction features on model complexity
How does adding interaction features between numeric variables affect the complexity of a linear regression model?
AIt increases the number of features, which can improve model flexibility but may cause overfitting if not controlled.
BIt decreases the number of features, simplifying the model and reducing overfitting risk.
CIt has no effect on model complexity because interaction features are ignored by linear regression.
DIt always improves model accuracy without any risk of overfitting.
Attempts:
2 left
💡 Hint
Think about how adding new features changes the model's ability to fit data.

Practice

(1/5)
1. What is the main purpose of creating interaction features in data analysis?
easy
A. To capture the combined effect of two or more features on the target
B. To remove irrelevant features from the dataset
C. To reduce the size of the dataset by merging columns
D. To convert categorical data into numerical data

Solution

  1. Step 1: Understand interaction features

    Interaction features combine two or more features to capture their joint effect on the target variable.
  2. Step 2: Compare options

    Only To capture the combined effect of two or more features on the target describes capturing combined effects, which is the purpose of interaction features.
  3. Final Answer:

    To capture the combined effect of two or more features on the target -> Option A
  4. Quick Check:

    Interaction features capture combined effects = A [OK]
Hint: Interaction features combine columns to show joint effects [OK]
Common Mistakes:
  • Confusing interaction features with feature selection
  • Thinking interaction features reduce dataset size
  • Mixing interaction features with encoding categorical data
2. Which of the following Python code snippets correctly creates an interaction feature df['A_B'] by multiplying columns 'A' and 'B' in a DataFrame df?
easy
A. df['A_B'] = df['A'] * df['B']
B. df['A_B'] = df['A'].concat(df['B'])
C. df['A_B'] = df['A'].append(df['B'])
D. df['A_B'] = df['A'] + df['B']

Solution

  1. Step 1: Identify correct operation for interaction

    Interaction features are often created by multiplying columns to capture joint effects.
  2. Step 2: Check code syntax

    df['A_B'] = df['A'] * df['B'] uses multiplication (*) between columns, which is correct syntax in pandas.
  3. Final Answer:

    df['A_B'] = df['A'] * df['B'] -> Option A
  4. Quick Check:

    Multiplying columns creates interaction feature = D [OK]
Hint: Multiply columns with * to create interaction features [OK]
Common Mistakes:
  • Using + instead of * for interaction
  • Using append or concat which are for combining rows or DataFrames
  • Syntax errors from wrong method calls
3. Given the DataFrame df below:
   A  B
0  2  3
1  4  5
2  6  7

What will be the output of df['A_B'] = df['A'] * df['B']; df['A_B'].tolist()?
medium
A. [2, 4, 6]
B. [5, 9, 13]
C. [6, 20, 42]
D. [3, 5, 7]

Solution

  1. Step 1: Multiply each row's values in columns A and B

    Row 0: 2*3=6, Row 1: 4*5=20, Row 2: 6*7=42
  2. Step 2: Convert the new column to list

    The list is [6, 20, 42]
  3. Final Answer:

    [6, 20, 42] -> Option C
  4. Quick Check:

    Row-wise multiplication = [6, 20, 42] [OK]
Hint: Multiply row values, then list() to see results [OK]
Common Mistakes:
  • Adding instead of multiplying values
  • Confusing column names or indices
  • Returning original columns instead of interaction
4. Consider this code snippet to create an interaction feature:
df['A_B'] = df['A'] * df['B']
df['A_B'] = df['A_B'].apply(lambda x: x if x > 10 else 0)

What is the error in this code if df['A'] and df['B'] contain numeric values?
medium
A. The apply function should be used on rows, not on a column
B. No error; the code correctly creates and modifies the interaction feature
C. The lambda function syntax is incorrect
D. Multiplying columns directly causes a runtime error

Solution

  1. Step 1: Check multiplication of columns

    Multiplying two numeric columns in pandas works correctly and creates a new column.
  2. Step 2: Check apply with lambda on the new column

    Applying a lambda function to modify values conditionally is valid syntax and works on a Series.
  3. Final Answer:

    No error; the code correctly creates and modifies the interaction feature -> Option B
  4. Quick Check:

    Column multiplication and apply lambda are valid = A [OK]
Hint: apply with lambda on Series is valid for conditional changes [OK]
Common Mistakes:
  • Thinking apply only works on DataFrame rows
  • Assuming multiplication causes errors with numeric columns
  • Misreading lambda function syntax
5. You have a dataset with columns age and income. You want to create an interaction feature to capture how age modifies the effect of income on spending. Which code snippet correctly creates this interaction feature and then normalizes it by dividing by the maximum value?
hard
A. df['age_income'] = (df['age'] * df['income']) / df['age'].max()
B. df['age_income'] = df['age'] * df['income'] / df['income'].min()
C. df['age_income'] = (df['age'] + df['income']) / df['income'].max()
D. df['age_income'] = (df['age'] * df['income']) / (df['age'] * df['income']).max()

Solution

  1. Step 1: Create interaction by multiplying age and income

    Multiplying captures how age modifies income effect.
  2. Step 2: Normalize by dividing by max of the interaction feature

    Dividing by (df['age'] * df['income']).max() scales the new feature between 0 and 1.
  3. Final Answer:

    df['age_income'] = (df['age'] * df['income']) / (df['age'] * df['income']).max() -> Option D
  4. Quick Check:

    Multiply then normalize by max interaction = B [OK]
Hint: Multiply columns then divide by max of product to normalize [OK]
Common Mistakes:
  • Normalizing by max of only one column instead of product
  • Adding columns instead of multiplying for interaction
  • Using min instead of max for normalization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard