Coding Linear Regression
Learn how to apply linear regression by getting a bird's eye view of the complete hierarchy.
We'll cover the following...
Let’s recap our goal. We want to write a program that calculates the number of pizzas from the number of reservations. That program should follow the approach we discussed in The Math Behind the Magic: the program approximates the data with a function during the training phase. Then, during the prediction phase, it uses the function to infer the number of pizzas.
Linear regression in practice
In the general case, finding a function that approximates the data can be a lot of work. In our specific case, however, we lucked out. Our data points are roughly aligned, so that we can approximate them with an especially simple function: a line.
Let’s see what that line would look like. For now, let’s pick a line that passes by the origin of the axes, as shown in the following chart, because that will make things easier, to begin with:
Once we find the line, the training phase is over. We can say that the line is our model of the relation between reservations and pizzas.
Now we can move on to the prediction phase, where we use the line to predict the label from the input variable. For example, we have reservations. How much pizza do we expect to sell? To answer that question, we pick the point on the “Reservations” axis. From there we trace up until we cross the line, and then we trace left until we cross the “Pizzas” axis. We end up at , ...