When fitting a model, there are often interactions between multiple variables. For example, if we are predicted disease, excercise and diet together may work together to impact the result of health. For this, we will need to model interaction effects. In this article, we will learn how to build a polynomial regression model in Sklearn.
To fit a polynomial model, we use the
PolynomialFeatures class from the
preprocessing module. We first create an instance of the class. Next, we call the
fit_tranform method to transform our x (features) to have interaction effects. We then pass this transformation to our linear regression model as normal.
from sklearn.linear_model import LinearRegression from sklearn.datasets import load_boston from sklearn.preprocessing import PolynomialFeatures boston = load_boston() features = boston.data target = boston.target # poly model interaction = PolynomialFeatures( degree = 3, include_bias = False, interaction_only = True) interaction_x = interaction.fit_transform(features) # linear regression regression = LinearRegression() # Fit the linear regression model = regression.fit(interaction_x, target)