In this lab, you'll explore interactions in the Ames Housing dataset.
You will be able to:
- Implement interaction terms in Python using the
sklearn
andstatsmodels
packages - Interpret interaction variables in the context of a real-world problem
You'll use a couple of built-in functions, which we imported for you below:
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
If you still want to build a model in the end, you can do that, but this lab will just focus on finding meaningful insights in interactions and how they can improve
regression = LinearRegression()
Create a baseline model which includes all the variables we selected from the Ames housing data set to predict the house prices. Then use 10-fold cross-validation and report the mean
ames = pd.read_csv('ames.csv')
continuous = ['LotArea', '1stFlrSF', 'GrLivArea', 'SalePrice']
categoricals = ['BldgType', 'KitchenQual', 'SaleType', 'MSZoning', 'Street', 'Neighborhood']
## code here
Next, create all possible combinations of interactions, loop over them and add them to the baseline model one by one to see how they affect the
You will create a for
loop to loop through all the combinations of 2 predictors. You can use combinations
from itertools to create a list of all the pairwise combinations. To find more info on how this is done, have a look here.
Since there are so many different neighbourhoods we will exclude
from itertools import combinations
# code to find top interactions by R^2 value here
It looks like the top interactions involve the Neighborhood_Edwards feature so lets add the interaction between LotArea and Edwards to our model.
We can interpret this feature as the relationship between LotArea and SalePrice when the house is in Edwards or not.
Separate all houses that are located in Edwards and those that are not. Run a linear regression on each population against SalePrice
. Visualize the regression line and data points with price on the y axis and LotArea on the x axis.
# Visualization code here
Use 10-fold cross-validation to build a model using the above interaction.
# code here
Our statsmodels
to see if this interactions are significant.
# code here
What is your conclusion here?
# formulate your conclusion
You should now understand how to include interaction effects in your model! As you can see, interactions can have a strong impact on linear regression models, and they should always be considered when you are constructing your models.