🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.exp()

AI & DATA SCIENCE // np-exp

np.exp() computes e (Euler's number, approximately 2.71828) raised to the power of each element in an array.

Syntax

np.exp(arr)

Deep Dive Course

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.

editor.html
import numpy as np

arr = np.array([0, 1, 2])
print(np.exp(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.exp() showing how it is used in production NumPy code.

editor.html
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)
localhost:3000

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.

editor.html
import numpy as np

arr = np.array([0, 1, 2])
print(np.exp(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([0, 1, 2])
print(np.exp(arr))
Example 02Advanced Example
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)

Best Practices

  • 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
  • Watch for inf/nan appearing downstream of np.exp() on large inputs, and add a numerical-stability adjustment if it's a realistic risk
  • 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

Interview Question

Why is it common practice to subtract the maximum value from an array before applying np.exp() when computing a softmax?

Hint: Think about numerical overflow, not the mathematical correctness of the result.

Subtracting the maximum value shifts every element to be 0 or negative before exponentiating, which keeps np.exp()'s output bounded between 0 and 1 for every element, avoiding the risk of large positive inputs overflowing to inf. Because softmax normalizes by dividing by the sum of all the exponentials, this shift is mathematically exactly cancelled out in the final ratio, so the result is identical to computing softmax without the shift — it's purely a numerical-stability trick, not a change to the underlying math.

Exercises

MediumPractice using np.exp() in a real scenario.
View Solution
import numpy as np

arr = np.array([0, 1, 2])
print(np.exp(arr))

Frequently Asked Questions

Why is it common practice to subtract the maximum value from an array before applying np.exp() when computing a softmax?

Subtracting the maximum value shifts every element to be 0 or negative before exponentiating, which keeps np.exp()'s output bounded between 0 and 1 for every element, avoiding the risk of large positive inputs overflowing to inf. Because softmax normalizes by dividing by the sum of all the exponentials, this shift is mathematically exactly cancelled out in the final ratio, so the result is identical to computing softmax without the shift — it's purely a numerical-stability trick, not a change to the underlying math.

Related Functions

np-lognp-powernp-clip