Classification
Predicting a category: decision boundaries, probabilities and thresholds.
Lesson 21 of 6610 minBeginner
You will learn to
- Define binary and multiclass classification
- Explain scores, probabilities and thresholds
- Read a decision boundary
Before this lesson
Intuition
Classification is the task of predicting a category: spam or not spam, which of two brands a customer will buy, which of five species a flower belongs to. The target takes values from a short list of classes.
- Binary classification has two classes, usually called positive and negative. The positive class is the one you care about finding, such as "customer will leave".
- Multiclass classification has three or more classes, and each row belongs to exactly one.
Most models do not output a class directly. They output a score for each class, often turned into a probability between 0 and 1. A probability of 0.8 for "buys" means the model is fairly sure. To get a class you need a rule, and the simplest rule is a threshold: predict the positive class when its probability is at least the threshold (0.5 by default).
The threshold is a business decision, not a technical detail. If missing a customer who will leave is costly, lower the threshold and catch more of them, accepting more false alarms. If every alarm triggers an expensive action, raise it.
Picture it
A model with two inputs divides the plane into regions. Points on one side of the boundary are predicted as class A and points on the other as class B. The further a point is from the boundary, the more confident the model is. A straight boundary belongs to simple models such as logistic regression. Flexible models, such as trees and neighbours, draw bent and fragmented boundaries, which can follow the data closely and can also overfit (see overfitting and bias-variance).
The math
A common way to get probabilities is to compute a score as a weighted sum of the inputs and squeeze it through the sigmoid function:
The sigmoid maps any number to the range 0 to 1. A score of 0 gives 0.5, large positive scores give values near 1, and large negative scores give values near 0. The decision boundary is where , which for a weighted sum is a straight line (or a flat surface with more inputs).
For multiclass problems the same idea extends with the softmax function, which turns one score per class into probabilities that add up to 1.
The predicted class for a threshold is
Changing moves the decision, not the model. The probabilities stay the same.
Worked example
A model scores six customers on the chance of buying (1 = buys). The numbers are illustrative.
| Customer | Probability | Actually bought |
|---|---|---|
| A | 0.90 | yes |
| B | 0.80 | yes |
| C | 0.65 | no |
| D | 0.40 | yes |
| E | 0.30 | no |
| F | 0.10 | no |
Three thresholds give three different sets of predictions. TP is a predicted buyer who bought, FP a predicted buyer who did not, FN a missed buyer and TN a correct "no".
| Threshold | Predicted buyers | TP | FP | FN | TN | Accuracy | Precision | Recall |
|---|---|---|---|---|---|---|---|---|
| 0.35 | A, B, C, D | 3 | 1 | 0 | 2 | 5/6 = 0.83 | 3/4 = 0.75 | 3/3 = 1.00 |
| 0.50 | A, B, C | 2 | 1 | 1 | 2 | 4/6 = 0.67 | 2/3 = 0.67 | 2/3 = 0.67 |
| 0.70 | A, B | 2 | 0 | 1 | 3 | 5/6 = 0.83 | 2/2 = 1.00 | 2/3 = 0.67 |
Precision is TP / (TP + FP): of the customers we flagged, how many bought. Recall is TP / (TP + FN): of the customers who bought, how many we found. Lowering the threshold to 0.35 finds all the buyers (recall 1.00) at the cost of a false alarm. Raising it to 0.70 gives no false alarms (precision 1.00) but misses customer D. The default of 0.5 is not best for any of the measures here. This trade-off is the subject of precision-recall curves.
Accuracy alone would not have told the story: 0.83 appears twice with very different behaviour.
Common mistakes
- Treating 0.5 as a law. It is a default. Choose the threshold from the cost of each kind of mistake, using validation data.
- Reading a score as a probability. Some models give scores that are not calibrated: "0.9" does not mean 90% of such cases are positive. See calibration.
- Judging by accuracy on imbalanced data. If 95% of the rows are negative, always predicting "negative" has 95% accuracy and is useless. Look at precision and recall, and see imbalanced data.
- Encoding unordered classes as numbers 1, 2, 3. A model may read an order into them. Pass them as categories and use a multiclass method.
- Choosing the threshold on the test set. That makes the test score optimistic. Choose it on validation data and keep the test rows for the final check.
Try it in mlflow
Train a classification pipeline
- Open Pipelines and choose New pipeline.
- Set Task to Classification, pick a dataset (for example the juice sample, where the target Purchase tells which of two brands a customer bought) and choose the Target column.
- Open the pipeline, go to the Models part, pick a few algorithms and train from Review & run.
- Open the run and compare the algorithms on the leaderboard.