Cheat sheets › Chapter 4
Classification cheat sheet
Predict categories instead of numbers, and score the results honestly. Every lesson in one page: the key idea, the pseudocode and the terms.
Logistic regression
Some questions have only two answers. Will this student pass? Is this email spam? This kind of problem is called classification. Here, students who passed sit up top and students who failed sit on the floor.
# question: will a student pass? (yes or no)
score = w × hours + b # a straight line, like before
chance = sigmoid(score) # squash it between 0 and 1
if chance > 0.5:
guess "pass"
else:
guess "fail"
# learning adjusts w and b so the chances match realityTerms: classification, sigmoid, threshold
Sigmoid and softmax
A model first produces a raw score, which can be any number at all. The sigmoid turns it into a chance between 0 and 1. The numbers under the bars are the raw scores; the bar heights are the chances.
# two answers: sigmoid sigmoid(score) = 1 / (1 + e^(−score)) sigmoid(-4) → 0.02 sigmoid(0) → 0.5 sigmoid(4) → 0.98 # many answers: softmax scores = [cat 2.0, dog 1.0, bird 0.1] boosted = e^score for each # [7.39, 2.72, 1.11] chances = boosted / sum(boosted) # [0.66, 0.24, 0.10] # chances always add up to 1
Decision boundaries
Each student is now a dot on a map: how long they studied (one direction) and how long they slept (the other). Teal passed, coral failed. Can you see where one group ends and the other begins?
# each student has two clues: hours studied, hours slept
point = [studied, slept]
# a straight boundary: one line splits the map
if studied + 0.9 × slept > 7.3:
guess "pass"
else:
guess "fail"
# curvier models can draw curvy boundariesTerms: decision boundary
k-nearest neighbors
Here is our map of students again, plus a new one (yellow) whose result we don’t know yet. The idea behind k-nearest neighbors is simple: you are probably like the people closest to you.
new_point = [3.8, 3.6] k = 3 distances = distance from new_point to every example closest = the k examples with the smallest distance votes = count the answers among closest guess = the answer with the most votes # no training step: it just remembers every example
Naive Bayes
We have 100 old emails, already sorted by a person: 40 spam (coral) and 60 normal (teal). So before reading a single word, the chance that any email is spam is 40%.
# learn from 100 old emails
chance(spam) = 40 / 100
chance("free" | spam) = 0.60 chance("free" | normal) = 0.05
chance("money" | spam) = 0.50 chance("money" | normal) = 0.10
# new email: "free money"
spam_score = 0.4 × 0.60 × 0.50 # 0.12
normal_score = 0.6 × 0.05 × 0.10 # 0.003
chance it is spam = spam_score / (spam_score + normal_score) # 98%Terms: naive
Support vector machines
A support vector machine picks the line with the most breathing room on both sides, like the middle of the widest possible street between the groups. That gap is the margin. A wide margin leaves room for new points that land a bit off.
# many lines can split the two groups # SVM picks the one with the widest gap on both sides line = the line with the biggest margin support_vectors = the points touching the margin edges # curvy data? lift it into a new dimension first height = distance_from_center² then split with a flat sheet
Terms: margin, support vectors, kernel trick
The confusion matrix
We test a spam filter on 20 emails where we know the truth. Each block is an email, colored by the truth (coral = spam, teal = normal). The ball on top is what the model guessed.
# test a spam filter on 20 emails we already know the answer for
for each email:
truth = is it really spam?
guess = did the model say spam?
put it in one of four boxes:
caught spam (said spam, was spam)
missed spam (said normal, was spam)
false alarm (said spam, was normal)
correctly normal (said normal, was normal)
accuracy = right answers / all answersTerms: confusion matrix, false positive, false negative, accuracy
Precision, recall and F1
Look only at the emails the model called spam: the left column. 6 really were spam and 3 were false alarms. precision = 6 / 9 = 67%. High precision means you can trust a “yes”.
caught = 6 # said spam, was spam false_alarms = 3 # said spam, was normal missed = 2 # said normal, was spam precision = caught / (caught + false_alarms) # 6 / 9 = 67% recall = caught / (caught + missed) # 6 / 8 = 75% F1 = 2 × precision × recall / (precision + recall) # 71%
ROC curves and AUC
The model gives each email a spam score from 0 to 1. Coral balls are real spam, teal are normal. The yellow wall is the cutoff: everything to its right gets flagged as spam.
scores = model's spam score for each email (0 to 1)
for cutoff from 1.0 down to 0.0:
catch_rate = share of real spam scored above cutoff
alarm_rate = share of normal email scored above cutoff
plot point (alarm_rate, catch_rate)
AUC = area under that curve
# 1.0 = perfect, 0.5 = no better than a coin flipRevise with flashcards and quizzes. Free practice for every lesson in this chapter.
Open in the app