Linear Regression with Python
In the previous chapter, we used a function from NumPy to calculate the parameters.
Now we will use the class object instead of the function to represent the linear regression. This approach takes more lines of code to find the parameters, but it stores a lot of helpful information inside the object and makes the prediction more straightforward.
Building a Linear Regression model
In statsmodels, the
OLS class can be used to create a linear regression model.
We first need to initialize an
OLS class object using
Then train it using the
Which is equivalent to:
The constructor of the
OLSclass expects a specific array
X_tildeas an input, which we saw in the Normal Equation. So you need to convert your
X_tilde. This is achievable using the
When the model is trained, you can easily access the parameters using the
Making the predictions
New instances can easily be predicted using
predict() method, but you need to preprocess the input for them too:
Getting the summary
As you probably noticed, using the
OLS class is not as easy as the
polyfit() function. But using
OLS has its benefits. While training, it calculates a lot of statistical information. You can access the information using the
That's a lot of statistics. We will discuss the table's most important parts in later sections.
Choose the INCORRECT statement.
Select the correct answer