Sub topic 5
Decision boundaries and the 0.5 threshold
This chapter assumes you know the sigmoid function and the model p = σ(w·x + b) from the previous chapter. You should also be comfortable with NumPy arrays.
From probability to a label
The model outputs a probability. A classification needs a label. The usual rule is to predict class 1 when p ≥ 0.5 and class 0 otherwise.
The sigmoid equals 0.5 exactly when its input is 0. So the rule simplifies:
- Predict 1 when z = w·x + b ≥ 0.
- Predict 0 when z < 0.
The decision boundary is the set of points where z = 0. Because z is linear in x, the boundary is a straight line with two features, a plane with three, and a hyperplane in general.
What the weights tell you about the boundary
The vector w is perpendicular to the boundary and points toward the class 1 side. The bias b shifts the boundary away from the origin. The signed distance from a point to the boundary is z / ‖w‖. A point far from the boundary has a large |z|, so its probability is near 0 or 1. A point on the boundary has p = 0.5, so the model is undecided.
A worked example
Take w = (2, 1) and b = −1. The boundary is 2·x₁ + x₂ − 1 = 0, which is the line x₂ = 1 − 2·x₁.
import numpy as np
def sigmoid(z):
return 1.0 / (1.0 + np.exp(-z))
w = np.array([2.0, 1.0])
b = -1.0
points = np.array([[0.0, 0.0], [1.0, 0.0], [0.5, 0.0], [0.0, 1.0]])
z = points @ w + b
p = sigmoid(z)
for point, zi, pi in zip(points, z, p):
print(point, round(float(zi), 2), round(float(pi), 3), int(pi >= 0.5))The points (0.5, 0) and (0, 1) both lie on the line, so z = 0 and p = 0.5. The point (1, 0) gives z = 1 and p ≈ 0.731, so it is class 1. The origin gives z = −1 and p ≈ 0.269, so it is class 0.
Moving the threshold moves the boundary
The 0.5 cutoff is a default, not a law. For any threshold t, the rule p ≥ t is equivalent to z ≥ logit(t) = ln(t / (1 − t)). The boundary becomes w·x + b = logit(t). It stays parallel to the old one and shifts sideways.
t = 0.8
cutoff = np.log(t / (1 - t))
print(round(float(cutoff), 3))
print((z >= cutoff).astype(int))With t = 0.8 the cutoff is about 1.386. Now (1, 0) has z = 1, which falls below the cutoff, so it flips to class 0. A higher threshold makes the model more cautious about predicting class 1. How to pick t for your costs comes later in the tutorial.
Common errors
- Treating the boundary as curved. The boundary is linear in the features you give the model. To get a curved boundary, add features such as x₁² or x₁·x₂.
- Reading 0.5 as "50% sure the example is positive" for any data. If the training data was resampled or reweighted, the probabilities may not match real frequencies, and 0.5 may not be the sensible cutoff.
- Forgetting the bias. Without b, the boundary must pass through the origin.
Exercise
A model has w = (1, −2) and b = 3. Does the point (2, 3) get class 1 at threshold 0.5? What score z must a point exceed to get class 1 at threshold 0.9?
Show answer
z = 1·2 + (−2)·3 + 3 = −1, which is below 0, so p < 0.5 and the point gets class 0. At threshold 0.9 the point needs z ≥ ln(0.9/0.1) = ln 9 ≈ 2.197.