Compute sigmoid(z) = 1/(1+e^{−z}) for a list of z values via a loop using math.exp. Library: scipy.special.expit — numerically stable sigmoid for arrays. RESULT: sigmoid values (rounded).

By hand

With SciPy

scipy.special.expit(z) computes 1/(1+exp(−z)) in a numerically stable way (avoids overflow for large negative z). Works element-wise on lists or arrays.

naive.py
import math
z_vals = [-2, -1, 0, 1, 2]
result = []
for z in z_vals:
    s = 1 / (1 + math.exp(-z))
    result.append(round(s, 4))
print('RESULT:', result)
library.py
from scipy.special import expit
from dalib.display import set_display
set_display()

z_vals = [-2, -1, 0, 1, 2]
result = [round(float(v), 4) for v in expit(z_vals)]
print('z_vals:', z_vals)
print('RESULT:', result)
z_vals: [-2, -1, 0, 1, 2]
RESULT: [0.1192, 0.2689, 0.5, 0.7311, 0.8808]

Implementation notes

  • Sigmoid maps any real z to (0,1), making it suitable as a probability output. s(0)=0.5 is the decision boundary when thresholding at 0.5.
  • Symmetric: s(−z) = 1 − s(z). Large positive z→1; large negative z→0.
  • expit is the canonical name in scipy (inverse of the logit function). For extreme z values, direct 1/(1+exp(-z)) can overflow in exp; expit uses a numerically stable equivalent.
  • Cross-reference: normal-pdf-point (python-stats ch05) is another nonlinear function applied point-wise to transform a real input.
  • Used in: logistic-predict-given-weights (this chapter) to turn a linear score into a class probability.