Sub topic 5
Decision boundaries and the 0.5 threshold
This chapter assumes you know the sigmoid model from the previous chapter, where a linear score z = w·x + b becomes a probability p = σ(z). It also assumes basic NumPy and scikit-learn. Here you will see how that probability becomes a class label, and why the boundary between the classes is a straight line or plane.
From probability to label
The model outputs a probability, but most applications need a decision: approve or reject, spam or not spam. The usual rule has one parameter, a threshold t:
- Predict class 1 if p ≥ t.
- Predict class 0 otherwise.
The default is t = 0.5, which predicts the more likely class. The threshold is not part of the fitted model. You can change it after training without refitting.
Why the boundary is linear
The sigmoid is monotonic, and σ(0) = 0.5. So p ≥ 0.5 exactly when z ≥ 0:
p >= 0.5 <=> w·x + b >= 0The decision boundary is the set of points where w·x + b = 0. With two features this is a line, with three it is a plane, and in general it is a hyperplane. This is why logistic regression is called a linear classifier, even though its probability curve is S-shaped.
Take w = (2, 1) and b = −1. The boundary is 2x₁ + x₂ = 1, or x₂ = 1 − 2x₁. 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.
Distance and confidence
The score z is proportional to the signed distance from the boundary:
distance = z / ||w||For the point (1, 0), z = 2·1 + 0 − 1 = 1. Then p = σ(1) ≈ 0.73, and the distance is 1/√5 ≈ 0.45. The point (0, 1) has z = 0, so it lies on the boundary and p = 0.5. Points far from the boundary get probabilities near 0 or 1.
Checking it in code
The following code fits a model with scikit-learn. It then confirms that the labels from predict match the sign of the score and computes points on the boundary line.
import numpy as np
from sklearn.linear_model import LogisticRegression
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)
clf = LogisticRegression().fit(X, y)
w = clf.coef_[0]
b = clf.intercept_[0]
z = X @ w + b
print(np.array_equal((z > 0).astype(int), clf.predict(X)))
# Boundary: w[0]*x1 + w[1]*x2 + b = 0, so x2 = -(w[0]*x1 + b) / w[1]
x1 = np.array([-2.0, 0.0, 2.0])
x2 = -(w[0] * x1 + b) / w[1]
print(np.column_stack([x1, x2]))
# Signed distance of the first example from the boundary
print(z[0] / np.linalg.norm(w))The first line should print True. The boundary formula divides by w[1], so it fails when that weight is zero, which gives a vertical boundary. In that case solve for x₁ instead.
Moving the threshold
A threshold t other than 0.5 moves the boundary but keeps it linear. Apply the inverse sigmoid (the logit) to both sides of p ≥ t:
p >= t <=> z >= logit(t) = ln(t / (1 - t))The new boundary is w·x + b = logit(t). It is parallel to the old one, shifted by logit(t)/‖w‖ along the direction of w.
| Threshold t | logit(t) | Effect |
|---|---|---|
| 0.1 | −2.20 | Very permissive. Many points predicted positive. |
| 0.3 | −0.85 | Predict positive more often. |
| 0.5 | 0.00 | Boundary at z = 0. |
| 0.7 | 0.85 | Predict positive less often. |
| 0.9 | 2.20 | Very strict. Few points predicted positive. |
You can check the equivalence numerically, reusing z from the previous block:
t = 0.3
p = 1.0 / (1.0 + np.exp(-z))
shift = np.log(t / (1 - t))
print(np.array_equal(p >= t, z >= shift))This prints True. In practice you apply the threshold to predict_proba output, as in the scikit-learn workflow chapter. This identity explains what happens geometrically.
Properties and edge cases
- Scaling w changes sharpness, not position. The boundary w·x + b = 0 is the same as 10w·x + 10b = 0. The larger weights only make probabilities rise from 0 to 1 over a narrower band. This is why regularization, which shrinks w, makes predictions less confident without moving the boundary much.
- A probability near 0.5 is a weak prediction. Points close to the boundary can flip class after a small change in the data or a retrain.
- Linear in the features you give it. If the true boundary is a circle, a line in (x₁, x₂) fails. Adding features such as x₁², x₂², and x₁x₂ makes the boundary linear in the expanded space but curved in the original one.
PolynomialFeaturesin scikit-learn does this expansion. - The boundary is not the whole story. Two models can share the same boundary and give different probabilities. Accuracy depends only on the boundary, while log loss depends on the probabilities too.
- 0.5 is a convention. It minimizes the error rate only when both kinds of mistake cost the same and the training class mix matches deployment. Later chapters cover choosing t when that fails.
Common errors
- Plotting the boundary with the wrong sign, by forgetting the bias or dividing by
w[0]instead ofw[1]. - Interpreting
clf.coef_from a model fitted on unscaled data as comparable across features. The boundary is still correct, but the weight sizes depend on each feature's units. - Changing the threshold and then reporting accuracy as if nothing changed. Moving t trades one error type for the other.
Exercise
A model has w = (3, −4) and b = 2. (a) Is the point (1, 2) predicted class 1 at t = 0.5? (b) Which z-value defines the boundary when t = 0.8? Give the number to two decimals.
Show answer
(a) z = 3·1 − 4·2 + 2 = −3, which is below 0, so p = σ(−3) ≈ 0.05 and the prediction is class 0. (b) The boundary is z = logit(0.8) = ln(0.8/0.2) = ln 4 ≈ 1.39.
Exercise
You multiply every weight and the bias of a fitted model by 5. What happens to the 0.5-threshold predictions, and what happens to the probabilities?
Show answer
The predictions do not change, because the sign of z is unchanged and the boundary w·x + b = 0 is the same. The probabilities move toward 0 and 1, because z is five times larger, so the model looks more confident.