Lesson 1 · Unit 01
Linear regression
Residuals, least squares and what one outlier does
What you practise
- Features and targets, a prediction as a dot product, residuals and mean squared error, the best line by least squares and by gradient descent, why features need scaling
Try a question from this lesson
Pick one
Three points: (1, 1), (2, 3), (3, 2). Line A, y = x, passes through one of them. Line B, y = 0.5x + 1, passes through none. Which has the smaller total squared error?
Hint
For each line, work out its miss at x = 1, 2 and 3. Square the three misses and add them.
Correct answer
B: its misses are smaller overall
Why
A misses by 0, 1 and -1: squared total 2. B misses by -0.5, 1 and -0.5: squared total 1.5. The best line is the one with the smallest total, and it need not touch any point.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Residuals and least squares
A residual is what is left over: the target minus the prediction, one per point.
Least squares picks the line whose squared residuals add up to the smallest total. For a straight line there is exactly one such line, and it can be solved for directly, with no descent steps.
x = np.array([1, 2, 3])
y = np.array([1, 3, 2])
resid = y - (0.5 * x + 1)
print(resid) # [-0.5 1. -0.5]
print((resid ** 2).sum()) # 1.510 more exercises in the app
- 4× type the answer
- 1× fill the blanks
- 3× multiple choice
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.