Skip to content

Data Science

Sigmoid & Logistic function

This takes O(1) time per input — just one exponent and a couple of arithmetic operations.

The idea, in plain English

Say you want a model to output not just 'yes' or 'no', but 'how confident' — a number between 0 and 1, like a probability. The sigmoid function is the tool for that job. Feed it any real number, huge or tiny, positive or negative, and it always squashes the result into that 0-to-1 range, shaped like a smooth 'S'. Feed it a very negative number, and it creeps toward 0. Feed it a very positive number, and it creeps toward 1. Feed it exactly 0, and it lands right in the middle, at 0.5.

How it works

  1. 1Take any real number z (often the weighted sum of a model's inputs, like in logistic regression).
  2. 2Compute e raised to the power of negative z (e^-z). This is what does the actual squashing.
  3. 3Plug that into the formula: sigmoid(z) = 1 / (1 + e^-z). The result always lands strictly between 0 and 1.
  4. 4To turn that probability into a yes/no decision, pick a threshold (usually 0.5). A sigmoid(z) at or above the threshold predicts 'yes'; below it predicts 'no'.

When you'd use it

This is the engine inside logistic regression (predicting a yes/no outcome from numeric inputs) and the final layer of a neural network doing binary classification. Use it anywhere you need to turn a raw, unbounded number into something that behaves like a probability.

Common beginner mistakes

  • Do not forget the negative sign in e^-z. That flips the curve and reverses which direction counts as 'more confident'.
  • Do not treat sigmoid's output as a perfectly calibrated real-world probability. It is the model's confidence given what it learned, which can still be miscalibrated or just plain wrong.
  • Do not expect the output to keep changing when you feed in a very large positive or negative z. Sigmoid saturates near 0 and 1, so extreme inputs barely move the result, and barely move the gradient during training either.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
z: -2 -1 0 1 2
Sigmoid: 0.12 0.27 0.50 0.73 0.88
Predictions: no no yes yes yes

Not sure this is the right topic? See the learning paths → or where this leads →