The sigmoid and logistic regression
Squash a straight line through the S-shaped sigmoid and you get logistic regression, a model whose output is a probability. Learn how w and b move the curve.

In this lesson you will
- Write the sigmoid function and describe its shape and limits
- Build the logistic regression model f(x) = g(w·x + b) from a linear part and the sigmoid
- Read the model's output as the probability that y = 1
- Explain how w and b change the steepness and position of the curve
The last lesson ended with a wish list: an output that stays between 0 and 1, flattens out far from the boundary and still has a simple boundary to learn. One small function delivers all of it.
The sigmoid function
The sigmoid (also called the logistic function) is
where is the base of natural logarithms you met in Math for ML, section 1. It takes any real number and squashes it into the range between 0 and 1. Walk through a few values:
- Large positive , say : is tiny (about 0.000045), so .
- Large negative , say : is huge (about 22,000), so .
- : , so . The midpoint.
So the graph is an S: flat near 0 on the far left, rising steeply through 0.5 at , flattening near 1 on the far right. It never actually reaches 0 or 1. It is also symmetric: .
NumPy’s np.exp works on whole arrays, so the sigmoid is one line:
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
z = np.linspace(-8, 8, 161)
print("g(-10), g(0), g(10) =", sigmoid(np.array([-10.0, 0.0, 10.0])).round(5))
bpl.plot(z, sigmoid(z), kind="line", title="The sigmoid g(z)")Logistic regression: a line, then a squash
Logistic regression feeds a linear model into the sigmoid. First compute the same quantity as linear regression,
using the vector notation from Section 2 (with one feature it’s just ). Then squash it:
The output is always between 0 and 1, and we read it as a probability:
“The probability that is 1, given the features , for these parameters.” If the model says 0.83 for a tumour, it estimates an 83 % chance it’s malignant, and therefore a 17 % chance it’s benign. You met conditional probability in Math for ML, section 4; this is that idea put to work.
To turn the probability into a yes-or-no answer, apply a threshold, usually 0.5: predict when and otherwise.
Explore the S-curve
Here are the same tumours, now with a sigmoid model. Move and and watch the curve. Switch on Show g(z) panel to see the two stages separately: the linear part turns each tumour size into a , and the sigmoid turns into a probability.
This is the sigmoid on its own. Whatever number goes in, a value between zero and one comes out.
Three things to notice:
- sets the steepness. A large makes the S sharp, so the model jumps from “surely benign” to “surely malignant” over a few millimetres. A small makes it gentle and hesitant. A negative flips it, so larger tumours would look less likely to be malignant.
- slides the curve sideways. The curve crosses 0.5 where , that is where , at . With and that’s cm.
- Far points no longer matter much. Tick Add very large tumours. The sigmoid is already almost exactly 1 out there, so these points are predicted correctly and the boundary stays put.
Your turn: a sigmoid that works on arrays
Machine learning code calls the sigmoid on whole arrays of values at once, one per example.
Exercise · Python
Write the sigmoid
Solved ✓Complete sigmoid(z) so it returns . It must work for a single number and for a NumPy array of any shape (element by element), so use np.exp, not math.exp.
import numpy as np
def sigmoid(z):
# Return 1 / (1 + e^(-z)), element by element.
return None # replace this line
print(sigmoid(0)) # should be 0.5
print(sigmoid(np.array([-2.0, 0.0, 2.0])))
Check yourself
Self-check · 4 questions