logistic function
sigmoid function
Python programming
mathematical functions
data science

How to calculate a logistic sigmoid function in Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The logistic sigmoid function maps real numbers into the interval from 0 to 1. It is common in logistic regression, neural networks, and probability-like scoring systems.

The textbook formula is simple, but a naive implementation can overflow for very large positive or negative inputs. A good Python implementation should therefore be both readable and numerically stable.

The Basic Formula

The sigmoid function is:

sigmoid(x) = 1 / (1 + exp(-x))

A simple NumPy implementation looks like this:

python
1import numpy as np
2
3
4def sigmoid(x):
5    x = np.asarray(x, dtype=np.float64)
6    return 1.0 / (1.0 + np.exp(-x))
7
8
9x = np.array([-2.0, 0.0, 2.0])
10print(sigmoid(x))

This is fine for moderate inputs and is often enough for educational code.

Numerical Stability for Large Values

For very large magnitudes, the naive formula can overflow or underflow because of exp(-x) or exp(x). A stable implementation handles positive and negative values separately.

python
1import numpy as np
2
3
4def stable_sigmoid(x):
5    x = np.asarray(x, dtype=np.float64)
6    return np.where(
7        x >= 0,
8        1.0 / (1.0 + np.exp(-x)),
9        np.exp(x) / (1.0 + np.exp(x)),
10    )
11
12
13print(stable_sigmoid(np.array([-1000.0, 0.0, 1000.0])))

This is a better default for reusable utility code.

Scalars, Vectors, and Matrices

Because the function uses NumPy arrays internally, the same implementation can handle scalars, vectors, and matrices.

python
print(stable_sigmoid(0.5))
print(stable_sigmoid(np.array([0.5, 1.5, -2.0])))
print(stable_sigmoid(np.array([[1.0, -1.0], [2.0, -2.0]])))

The np.asarray conversion is important because it makes list inputs behave predictably too.

The Derivative

If you are implementing optimization code manually, the derivative is often useful.

sigmoid'(x) = sigmoid(x) * (1 - sigmoid(x))

python
1def sigmoid_derivative(x):
2    s = stable_sigmoid(x)
3    return s * (1.0 - s)
4
5
6print(sigmoid_derivative(np.array([-2.0, 0.0, 2.0])))

This derivative appears frequently in logistic models and neural-network backpropagation examples.

Connection to Classification

In logistic regression and binary classification, sigmoid output is often interpreted as a probability-like score, and a threshold converts that score into a class label.

python
1scores = stable_sigmoid(np.array([-1.2, 0.1, 2.7]))
2preds = (scores >= 0.5).astype(int)
3print(scores)
4print(preds)

The threshold does not have to be 0.5. In real applications, threshold choice should reflect the costs of false positives and false negatives.

SciPy Shortcut

If SciPy is available, scipy.special.expit is a well-tested sigmoid implementation.

python
1from scipy.special import expit
2import numpy as np
3
4print(expit(np.array([-5.0, 0.0, 5.0])))

This is often preferable in scientific code because it saves you from maintaining your own implementation.

Common Pitfalls

A common mistake is using the naive formula on very large values and then being surprised by overflow warnings or unstable results.

Another issue is forgetting to convert lists or integers to floating-point arrays when vectorized numeric behavior is expected.

Developers also sometimes hardcode the classification threshold inside the sigmoid helper itself. Keep the mathematical function separate from downstream decision policy.

Finally, do not confuse sigmoid output with a guaranteed calibrated probability. It is a bounded score, and calibration depends on the model and training process.

If this function will be reused across a codebase, add a few simple numeric tests around large positive and negative values. Stable behavior at the extremes is where many homegrown implementations quietly fail.

Summary

  • The logistic sigmoid maps real values into the interval from 0 to 1.
  • The basic formula is simple, but a numerically stable version is safer for real code.
  • NumPy implementations naturally support scalars and arrays.
  • The derivative is s * (1 - s) when s is the sigmoid output.
  • Keep thresholding and classification policy separate from the sigmoid function itself.

Course illustration
Course illustration

All Rights Reserved.