Sub topic 1
What classification asks of a model
Classification means predicting which category an example belongs to, such as spam or not spam. This chapter defines the task and the vocabulary the rest of the tutorial uses. It also sets up the question that logistic regression answers.
Prerequisites
This chapter assumes you know:
- Basic Python and NumPy.
- Linear regression and what a loss function is.
- Basic probability, including events and conditional probability.
- How a train/test split works.
The task
A classifier learns a function from data. Each example has a feature vector x (the inputs) and a label y (the category). In binary classification, y is 0 or 1. Class 1 is called the positive class. Which category counts as positive is your choice, and it affects how you read metrics later.
Examples:
- Is this transaction fraudulent (1) or legitimate (0)?
- Is this tumor malignant (1) or benign (0)?
- Will this customer cancel next month (1) or stay (0)?
The label is discrete, so the model's output has to be discrete or convertible to a discrete answer. That separates classification from regression, where the target is a continuous number.
Labels versus probabilities
A model can answer in two ways:
- A hard label: "this is class 1."
- A probability: "the chance this is class 1 is 0.83."
Logistic regression produces the second kind. It estimates the conditional probability P(y = 1 | x). You then turn that probability into a label by applying a threshold. A common default is 0.5, but you can change it.
Keeping the probability step separate has a practical benefit. The same model can serve different costs. A fraud team might flag anything above 0.2, while an automated system might act only above 0.95.
What a good classifier needs
A useful classifier has to satisfy several requirements:
- Its outputs should be valid probabilities, between 0 and 1.
- Its loss function should reward confident correct answers and punish confident wrong ones.
- Its training should work on data where the classes are not evenly split.
- Its results should be judged with measures suited to categories, not only squared error.
The next chapters build these in order. First you will see why a straight-line regression model fails the first requirement. Then you will derive the sigmoid, define log loss, and fit the weights.
A first look at the data
The snippet below creates a small two-feature dataset and counts the classes. Checking the class balance is a good first step on any classification problem.
import numpy as np
rng = np.random.default_rng(0)
X = np.vstack([rng.normal(-1, 1, (100, 2)), rng.normal(1, 1, (100, 2))])
y = np.array([0] * 100 + [1] * 100)
print(X.shape)
print(np.bincount(y))This prints (200, 2) and [100 100]: 200 examples, two features, and 100 examples in each class.
Exercise
A model outputs 0.83 for an email. What is the difference between that number and the label "spam"? Which one would you change if you wanted the system to flag more emails?
Show answer
0.83 is an estimated probability that the email is spam. The label comes from comparing that probability to a threshold. To flag more emails, lower the threshold, for example from 0.5 to 0.3. The model itself does not need to change.