Linear Regression with Python
OLS class allows you to build Multiple Linear Regression the same way as Simple Linear Regression. But unfortunately, the
np.polyfit() function does not handle the multiple features case.
We will stick with the
Building X̃ matrix
We have the same dataset from the simple linear regression example, but it now has the mother's height as the second feature. Let's load it and look at its
Remember, we should use
OLS(y, X_tilde) to initialize the
OLS object. As you can see, the X variable already holds two features in separate columns. So to get the X_tilde, we only need to add 1s as a first column. The
sm.add_constant(X) function is doing exactly that!
Finding the parameters
Great! Now we can build the model, find the parameters and make predictions the same way we did in the previous section.
Now that our training set has 2 features, we need to provide 2 features for each new instance we want to predict. That's why
np.array([[65, 62],[70, 65],[75, 70]])was used in the example above. It predicts
yfor 3 new instances: [Father:65,Mother:62], [Father:70, Mother:65], [Father:75, Mother:70]
What does the
Select the correct answer