Lesson 2 · Unit 01
Logistic regression
Squash a score into a probability, then pick a threshold
What you practise
- Squash a score into a probability with the sigmoid, log loss and why it punishes confident mistakes, the decision boundary, moving the threshold
Try a question from this lesson
Pick one
A straight line is fitted to hours studied against passed (1) or failed (0). For 12 hours it outputs 1.4. Why is that a problem if you want a probability?
Correct answer
A probability cannot be above 1
Why
A line keeps climbing for ever, so far enough out it leaves the range 0 to 1, in both directions. A classifier needs a score that flattens out near 0 and near 1 instead.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
The sigmoid
Logistic regression keeps the linear score z = w·x + b and squashes it:
sigmoid(z) = 1 / (1 + e^(-z))
A large positive z gives nearly 1, a large negative z nearly 0, and z = 0 gives exactly 0.5. Read the output as the probability of the positive class.
def sigmoid(z):
return 1 / (1 + np.exp(-z))
print(sigmoid(0)) # 0.5
print(sigmoid(4).round(3)) # 0.982
print(sigmoid(-4).round(3)) # 0.018Concept
Log loss
Squared error is the wrong ruler for probabilities. Logistic regression uses log loss: for each row, minus the log of the probability the model gave to the true class.
Sure and right costs almost nothing. Sure and wrong costs a fortune, and the cost grows without limit as that probability nears 0.
for p in [0.99, 0.5, 0.01]:
print(p, -np.log(p).round(2))
# 0.99 0.01
# 0.5 0.69
# 0.01 4.6110 more exercises in the app
- 2× type the answer
- 5× multiple choice
- 1× fill the blanks
- 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.