Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Building Multiple Linear Regression | Multiple Linear Regression
Linear Regression with Python

Building Multiple Linear RegressionBuilding Multiple Linear Regression

The 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 OLS class.

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 X variable.

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.

Note

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 y for 3 new instances: [Father:65,Mother:62], [Father:70, Mother:65], [Father:75, Mother:70]

What does the sm.add_constant(X) do?

Select the correct answer

Everything was clear?

Section 2. Chapter 3
course content

Course Content

Linear Regression with Python

Building Multiple Linear RegressionBuilding Multiple Linear Regression

The 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 OLS class.

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 X variable.

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.

Note

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 y for 3 new instances: [Father:65,Mother:62], [Father:70, Mother:65], [Father:75, Mother:70]

What does the sm.add_constant(X) do?

Select the correct answer

Everything was clear?

Section 2. Chapter 3
some-alt