Print
Implementing Multiple Linear Regression in Python
Read It: Implementing Multiple Linear Regression in Python
In this course we will be implementing multiple linear regression in Python. In particular, we will be using the .ols
command from the statsmodels.formula.api
library that you learned about in Lesson 8. More information on this command canbe found in the documentation. Below we will demonstrate how to implement multiple linear regression through two videos. The first will set up the data, while the second will focus on the acutal implementation.
Watch It: Video - Data Set Up Visualization (6:50 minutes)
Watch It: Video - Multiple Linear Regression (9:21 minutes)
Try It: DataCamp - Apply Your Coding Skills
Using the partial code below, implement a multiple linear regression model. Your response variable should be 'y' and the remaining variables should be used as explanatory variables.
# This will get executed each time the exercise gets initialized.
# libraries
import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
# create some data
df = pd.DataFrame({'y': np.random.uniform(10.0, 25.0, 50),
'x1': np.random.uniform(0.001, 0.999, 50),
'x2': np.random.uniform(0.001, 0.999, 50),
'x3': np.random.uniform(175, 250, 50),
'x4': np.random.uniform(5.7, 6.9, 50)})
# implement model
results = ...
print(...)
# libraries
import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
# create some data
df = pd.DataFrame({'y': np.random.uniform(10.0, 25.0, 50),
'x1': np.random.uniform(0.001, 0.999, 50),
'x2': np.random.uniform(0.001, 0.999, 50),
'x3': np.random.uniform(175, 250, 50),
'x4': np.random.uniform(5.7, 6.9, 50)})
# implement model
results = smf.ols('y ~ x1 + x2 + x3 + x4', data = df, hasconst = True).fit()
print(results.summary())