Let's pick up where we left off in the previous lesson. We have the
coefficients of the best-fit-line, slope = 1.8
and
intercept = -7.1
. We can use these values to make predictions
about the data. In the following equation we add a hat above the estimated
coefficients to indicate that they are estimations. We then also add a hat
above the
Let's see what the predicted value of
When we have a simple prediction equation for y_vals = intercept + slope * x_vals
as we did. However, sometimes
the predict equation is either more complicated, or is coming from a library,
and we may want to make these predictions using a function instead. We
will show how to do this two ways. First, we
will use a for loop, which is the more intuitive way to do this. Then we
will use a method called list comprehension, which also uses a for loop, but
has some optimizations under the hood to not only make it a more concise
way to write this code but also a more efficient application, in terms of
computation time.
We collect all the predicted
Now that we have a model, we can evaluate how well it fits the data. We can
do this by calculating the
mean squared error (MSE),
which is the sum of the
squared differences between the actual
The MSE is a measure of how close the data is to the fitted regression line, the smaller the better. Since we calculate the MSE for the linear regression model of 0.86, we can say that the model fits the data well. And we will keep this in our minds for the next section where we will do this same estimation but using a regression tree, then we can make a proper comparison of how the two performed.