Turning a linear regression model into a curve – polynomial regression
In the previous sections, we assumed a linear relationship between explanatory and response variables. One way to account for the violation of linearity assumption is to use a polynomial regression model by adding polynomial terms:
Here, denotes the degree of the polynomial. Although we can use polynomial regression to model a nonlinear relationship, it is still considered a multiple linear regression model because of the linear regression coefficients .
We will now discuss how to use the PolynomialFeatures
transformer class from scikit-learn to add a quadratic term () to a simple regression problem with one explanatory variable, and compare the polynomial to the linear fit. The steps are as follows:
- Add a second degree polynomial term:
from sklearn.preprocessing import PolynomialFeatures >>> X = np.array([258.0, 270.0, 294.0, … 320.0, 342.0, 368.0, … ...