np.exp(x) computes the exponential function element-wise, which grows extremely quickly for even moderately large positive inputs — large enough values can overflow to inf, while very negative inputs smoothly approach, but never quite reach, 0. It shows up constantly in statistics and machine learning, for example as a core building block of the sigmoid and softmax functions used to convert raw scores into probabilities.
1Understanding np.exp()
np.exp(x) computes the exponential function element-wise, which grows extremely quickly for even moderately large positive inputs — large enough values can overflow to inf, while very negative inputs smoothly approach, but never quite reach, 0. It shows up constantly in statistics and machine learning, for example as a core building block of the sigmoid and softmax functions used to convert raw scores into probabilities.
Large positive inputs to np.exp() can silently overflow to inf, which then propagates as nan through later operations like division — a common numerical-stability trick, used in real softmax implementations, is subtracting the maximum value from an array before exponentiating it, which doesn't change the final normalized result but keeps every input to exp() at or below 0.
import numpy as np
arr = np.array([0, 1, 2])
print(np.exp(arr))2Practical Example
Here is a real-world application of np.exp() showing how it is used in production NumPy code.
import numpy as np
scores = np.array([1.0, 2.0, 3.0])
exp_scores = np.exp(scores - np.max(scores))
softmax = exp_scores / np.sum(exp_scores)
print(softmax)3Best Practices
Follow these guidelines when working with np.exp():
1. Subtract the array's maximum value before applying np.exp() in something like a softmax computation, to avoid overflow while keeping the mathematical result unchanged
2. Watch for inf/nan appearing downstream of np.exp() on large inputs, and add a numerical-stability adjustment if it's a realistic risk
3. Use np.log() together with np.exp(), or np.log1p()/np.expm1() for values near zero, rather than working with raw exponentials when numerical precision near small values matters
Tip: Large positive inputs to np.exp() can silently overflow to inf, which then propagates as nan through later operations like division — a common numerical-stability trick, used in real softmax implementations, is subtracting the maximum value from an array before exponentiating it, which doesn't change the final normalized result but keeps every input to exp() at or below 0.
import numpy as np
arr = np.array([0, 1, 2])
print(np.exp(arr))