In one of the simplest forms of supervised machine learning, the computer learns a linear relationship between a single input and its corresponding continuous-valued output. The training data consists of input examples and their corresponding output values, . Simple linear regression means finding the line that “best fits” the training examples—such that the sum of the distances from each example to the line is minimized. This line is a model that can then be used to generate outputs for new inputs.

The equation of any line can be written as where is the slope of the line and is its y-intercept. The actual distance from a point to a line is fairly complex . Fortunately, we can minimize this distance by minimizing half the square of its vertical component . (You’ll see why in a moment.) For a line specified by and , we want to minimize:

Such a function is called the cost or loss function. Its value is minimized for the best-fit model. How do we find and that minimize this function? By finding the zeroes of its partial derivatives with respect to those variables.

(We used half the square of the vertical distance precisely to simplify the partial derivative equations. The term “partial derivative” may sound daunting, but it just calculates how rapidly one variable makes the function change, when the other variable is held constant. Near the point where both partial derivatives are zero, neither variable is affecting the function’s value. This tells us the function is at its minimum.)

The partial derivative with respect to is simply

The partial derivative with respect to is simply

For simple linear regression, it is possible to set these equations to zero and solve for and . To learn more complex models, we must take a different approach called gradient descent.


Click the arrows to see different ways of expressing the same thing: