Classification means placing an item into one of several categories that are set in advance. Deciding whether an email is "spam" or "normal", routing a help desk question to the right office, and telling whether a photo shows a cat or a dog are all classification. In machine learning, a model learns from labeled examples and then predicts a label for a new item.
Classification has a sibling called regression. Regression predicts a number (such as an exam score), while classification predicts a label (such as spam). In this post you build a small model that sorts campus help desk messages into spam and normal.
How does classification work?
There are three steps:
- Collect labeled examples. Each example is a message with the right answer next to it: "spam" or "normal".
- Turn the examples into numbers. A computer compares numbers, not text. So you pull measurable features out of each message.
- Look at the new item and decide by what it resembles most.
Here the feature is how many times each of five words appears in the message: free, click, won, scholarship and course. Every message becomes a list of five numbers.
How do you build the model in Python?
Training is just storing six labeled messages. When a new message arrives, find the closest training message and take its label (the nearest neighbor method):
words = ["free", "click", "won", "scholarship", "course"] def vector(message): message = message.lower() return [message.count(w) for w in words] training = [ ("You won a free phone, click now", "spam"), ("You won! Click for a free gift", "spam"), ("Click and win something free", "spam"), ("Which documents do I need for the scholarship", "normal"), ("How do I renew my course registration", "normal"), ("When is the scholarship paid, is course registration open", "normal"), ] def distance(a, b): return sum(abs(x - y) for x, y in zip(a, b)) def predict(message): v = vector(message) nearest = min(training, key=lambda t: distance(v, vector(t[0]))) return nearest[1] print(vector("Free scholarship, click here")) print(predict("You won! Click here, a free ticket is waiting")) print(predict("When does course selection start"))
Output[1, 1, 0, 1, 0]
spam
normalThe first line shows the message "Free scholarship, click here" turned into numbers: free once, click once, won never, scholarship once, course never. The second message looks like the three spam examples, and the third looks like the normal ones. The model gave the right answers without anyone writing a rule.
How do you measure how good the model is?
Testing a model on the examples it trained on is cheating, because it can memorize them. So you use a test set of messages it has never seen. Accuracy is the share of correct predictions out of all predictions:
test = [
("You won a free coupon, click now", "spam"),
("Free holiday, you won", "spam"),
("When are my course grades released", "normal"),
("When will the scholarship result be known", "normal"),
("Click here for the free meal scholarship", "normal"),
]
correct = 0
for message, real in test:
p = predict(message)
print(message, "|", real, "|", p)
correct += p == real
print(correct, "/", len(test), "=", correct / len(test))OutputYou won a free coupon, click now | spam | spam
Free holiday, you won | spam | spam
When are my course grades released | normal | normal
When will the scholarship result be known | normal | normal
Click here for the free meal scholarship | normal | spam
4 / 5 = 0.8Accuracy is 0.8: four of five messages are right. The wrong one is a real scholarship notice that was taken for spam. The model looked only at "free" and "click", and because those match a spam example, it got it wrong.
Are all mistakes equally costly?
No. A spam filter can make two kinds of mistakes:
| Mistake | What happens | Cost |
|---|---|---|
| Calling spam normal | An unwanted message reaches the inbox | Small, annoying |
| Calling a normal message spam | A real scholarship notice is missed | Large, a student loses an opportunity |
That is why accuracy alone is not enough. You decide which mistake is costly and tune the model for it. For example, instead of sending uncertain messages to the spam folder, you show them to a person. The lesson Which error is costly covers this in detail.
Common mistakes
- Testing on training data: The model memorizes what it saw and accuracy looks higher than it is. Always test on unseen data.
- Trusting accuracy blindly: If 95% of messages are normal, a model that says "normal" every time is 95% accurate and catches no spam at all.
- Training on too few examples: Six examples are for a demo. A real filter needs thousands.
- Choosing the wrong features: A model that looks at only five words cannot recognize a spam pattern that is not in that list.
- Missing ties in distance: If two examples are equally close, the model picks the first one in the list. That is chance, not knowledge.
What to read next
- Try the method by computing distances yourself: Nearest neighbor
- Learn accuracy and the confusion matrix: Accuracy and the confusion matrix
- Related post: What is machine learning
- Browse the whole course: AI Fundamentals
- #ai
- #classification
- #machine learning
- #python