Sub topic 4
The sigmoid: turning a score into a probability
This chapter assumes you know basic NumPy, the idea of a linear score w·x + b from linear regression, and the odds and log-odds definitions from the previous chapter. Here you will see how the sigmoid function converts a score of any size into a probability. You will also see why it is the inverse of the log-odds, which properties matter for training, and which numerical mistakes to avoid.
The problem the sigmoid solves
A linear model computes a score z = w·x + b. This score can be any real number, such as -37.2 or 1204. A probability must lie strictly between 0 and 1. We need a function that maps the whole real line into that interval and keeps the ordering: a larger score gives a larger probability.
The logistic function, also called the sigmoid, does this:
sigma(z) = 1 / (1 + exp(-z))Logistic regression is the model P(y = 1 | x) = sigma(w·x + b).
Deriving it from log-odds
The previous chapter defined the log-odds (logit) of a probability p as ln(p / (1 - p)). Its range is the whole real line, so it can be modeled by a linear function. Assume the log-odds are linear in the features and call that linear quantity z:
- Start with
ln(p / (1 - p)) = z. - Exponentiate both sides:
p / (1 - p) = e^z. - Multiply out:
p = e^z (1 - p), sop + p e^z = e^z. - Solve:
p = e^z / (1 + e^z). - Divide the top and bottom by
e^z:p = 1 / (1 + e^(-z)).
The sigmoid is therefore the inverse of the logit. The model does not pass a score through a convenient squashing function. It assumes the log-odds of the positive class are linear in the features, and the sigmoid is what that assumption implies for the probability.
Properties worth memorizing
| Property | Statement | Why it matters |
|---|---|---|
| Range | 0 < sigma(z) < 1 | Outputs are valid probabilities and never exactly 0 or 1 in exact arithmetic. |
| Midpoint | sigma(0) = 0.5 | A score of zero means the model is undecided. |
| Symmetry | sigma(-z) = 1 - sigma(z) | Flipping the sign of the score swaps the two classes. |
| Monotonic | z1 < z2 implies sigma(z1) < sigma(z2) | Ranking examples by score equals ranking by probability. |
| Derivative | sigma'(z) = sigma(z)(1 - sigma(z)) | It is cheap to compute and gives a clean gradient later. |
| Maximum slope | sigma'(0) = 0.25 | The curve is steepest at the midpoint and flattens in the tails. |
A few reference values:
| z | sigma(z) |
|---|---|
| -4 | 0.018 |
| -2 | 0.119 |
| 0 | 0.500 |
| 2 | 0.881 |
| 4 | 0.982 |
Each step of 1 in z multiplies the odds by e (about 2.718), regardless of where you are on the curve. Near 0 this looks like a change of about 0.25 in probability per unit of score. In the tails it is a small change in probability, but the odds still grow by the same factor.
From features to a probability
A worked example: let w = [1.5, -0.8], b = -0.2 and x = [2, 1]. The score is 1.5*2 - 0.8*1 - 0.2 = 2.0. The probability is sigma(2.0) = 0.881, and the odds are e^2 = 7.39. Check: 0.881 / 0.119 = 7.4.
A numerically safe implementation
The textbook formula breaks for large negative z. In 1 / (1 + np.exp(-z)) with z = -1000, np.exp(1000) overflows to inf. The result is still 0.0, but NumPy emits a warning. For large arrays, or when the result feeds into a log, this becomes a real problem. The fix is to use a different but algebraically equal form for negative scores, so that the exponent is never positive:
import numpy as np
def sigmoid(z):
z = np.asarray(z, dtype=float)
out = np.empty_like(z)
pos = z >= 0
out[pos] = 1.0 / (1.0 + np.exp(-z[pos]))
ez = np.exp(z[~pos])
out[~pos] = ez / (1.0 + ez)
return out
w = np.array([1.5, -0.8])
b = -0.2
x = np.array([[2.0, 1.0], [0.0, 3.0], [-4.0, 1.0]])
z = x @ w + b
p = sigmoid(z)
print(z) # [ 2. -2.6 -7. ]
print(p.round(4)) # [0.8808 0.0691 0.0009]
print(np.allclose(sigmoid(-z), 1 - p)) # True, the symmetry property
print(sigmoid(np.array([-1000.0, 1000.0]))) # [0. 1.] with no warningFor positive scores, exp(-z) is at most 1. For negative scores, exp(z) is at most 1. Neither branch overflows. Libraries do the same thing: scipy.special.expit is a ready-made stable sigmoid, and scikit-learn uses it internally.
Saturation and its consequences
The derivative sigma(z)(1 - sigma(z)) shrinks toward 0 as |z| grows. When a score is very large, the model is very confident and the curve is nearly flat. This has three practical effects:
- A confident wrong prediction is costly for the log-loss objective, but the sigmoid alone gives a weak gradient there. The chapter on log loss shows that the loss and sigmoid combine so the gradient stays well behaved (
p - y). - Unscaled features produce huge scores early in training. The sigmoid saturates, and probabilities jump to 0 or 1.
- If the classes are perfectly separable, the weights can keep growing and the probabilities approach 0 and 1. Regularization limits this.
The sigmoid and the decision
Because sigma(0) = 0.5 and the function is monotonic, predicting class 1 when p >= 0.5 is exactly the same as predicting class 1 when z >= 0. The sigmoid is not needed to make a hard 0.5 decision, but you need it for three things: reporting probabilities, computing the loss, and applying a threshold other than 0.5. A threshold t on the probability corresponds to a threshold ln(t / (1 - t)) on the score. For example, t = 0.3 corresponds to z >= -0.847.
Common errors
- Treating the output as a calibrated probability by default. It is a probability under the model, and the model may be overconfident or underconfident.
- Applying the sigmoid twice. In scikit-learn,
decision_functionreturns the scorez, andpredict_probaalready applies the sigmoid. Passingpredict_probaoutput through another sigmoid squeezes everything into (0.5, 0.73). - Reading the 0.25 slope as a constant effect on probability. A one-unit change in a feature changes the log-odds by a fixed amount, but the change in probability depends on the starting point.
- Using the naive formula on raw large scores and ignoring overflow warnings.
Exercise
A model gives a score z = ln(3), about 1.0986. What are the odds of class 1, and what is the probability?
Show answer
The log-odds are z, so the odds are e^z = 3. The probability is odds / (1 + odds) = 3 / 4 = 0.75. This matches sigma(ln 3) = 1 / (1 + 1/3) = 0.75.
Exercise
Using sigma'(z) = sigma(z)(1 - sigma(z)), what is the slope at z = 0, and what is it at z = 4 (where sigma is about 0.982)? What does the difference tell you about training?
Show answer
At z = 0 the slope is 0.5 0.5 = 0.25. At z = 4 it is 0.982 0.018, about 0.018. The curve is about 14 times flatter at z = 4, so the sigmoid is saturated there. Large scores, for example from unscaled features, make probabilities insensitive to small changes in z.