Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
Short answer: numpy.exp(x) returns ex (Euler’s number, about 2.718281, raised to the power x) for every element of x, and returns a scalar when x is a scalar. It is the inverse of numpy.log. This guide is for researchers and analysts who need to back-transform log-scale results, build exponential models, or choose between exp, expm1, exp2 and math.exp. Checked against the NumPy 2.5 manual on 2026-11-07; the outputs below were produced with NumPy 2.5.1.
What np.exp is used for in research
The NumPy manual defines numpy.exp as the calculation of “the exponential of all elements in the input array”. Typical research uses are variations of undoing a natural-log transform: turning log-odds from a logistic model into odds, natural-log fold-changes into fold-changes, or fitted log-rates from a Poisson model into rates. It is also the building block of exponential growth and decay models and of the likelihoods you evaluate when fitting them (see exponential regression and curve fitting in SciPy), and it reverses a log transformation. For real input the result is always positive. The manual notes that if x = ln y then ex = y, which is exactly the inverse relationship with np.log.
Install or access
NumPy’s official install page (numpy.org/install) lists these routes. With pip: pip install numpy. With conda: conda create -n my-env, then conda activate my-env, then conda install numpy. With a virtual environment: python -m venv my-env, activate it (source my-env/bin/activate on macOS and Linux, my-envScriptsactivate on Windows), then pip install numpy. Verify the install with:
import numpy as np
print(np.__version__)
Syntax and arguments
numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature])
| Argument | What it does (NumPy 2.5 manual) |
|---|---|
x |
array_like input values. |
out |
Optional ndarray, None or tuple. Must have a shape the inputs broadcast to. With None (default) a freshly allocated array is returned. |
where |
Optional array_like broadcast over the input. Where True, the ufunc result is computed; elsewhere out keeps its original value. If out was left as None, the False positions stay uninitialised. |
casting, order, dtype, subok, signature |
Standard keyword-only ufunc arguments. |
The function returns an ndarray, or a scalar if x is a scalar.
Step-by-step: exponentiate and back-transform
- Import NumPy and call the function on a scalar.
import numpy as np np.exp(0) # 1.0 np.exp(1) # 2.718281828459045 - Apply it to an array. The operation is element-wise.
np.exp(np.array([0.0, 1.0, 2.0])) # array([1. , 2.71828183, 7.3890561 ]) - Back-transform log-scale values. Illustrative example: if a model reports log2 fold-changes of -1, 0, 1 and 2, the fold-changes are 2x. Either use
np.exp2or multiply by ln 2 first:lfc = np.array([-1.0, 0.0, 1.0, 2.0]) np.exp2(lfc) # array([0.5, 1. , 2. , 4. ]) np.exp(lfc * np.log(2)) # array([0.5, 1. , 2. , 4. ])Use the base that matches how the values were logged:
np.expundoes natural logs, not log2 or log10. - Round-trip check with np.log.
np.exp(np.log(5.0))printed4.999999999999999, not exactly 5.0. Compare with a tolerance (np.isclose), never with==. - Compute only some elements with where and out.
a = np.array([0.0, 1.0, 2.0, 3.0]) mask = np.array([True, False, True, False]) o = np.zeros(4) np.exp(a, out=o, where=mask) # array([1. , 0. , 7.3890561 , 0. ])Supplying
outis what keeps the skipped positions defined. Omitoutand those positions are uninitialised, as the manual warns. - Complex input. For x = a + ib, ex = ea eib, with eib = cos b + i sin b, so the imaginary part contributes a phase of magnitude 1.
np.exp(1 + 1j*np.pi/2)returned approximately2.718jwith a real part near 1.7e-16 (floating-point rounding of cos(π/2)). The NumPy manual’s own example evaluates a complex grid and plotsnp.abs(out)andnp.angle(out).
Output and how to read it
- Shape and type: the output has the shape of the broadcast input. In the run used for this guide, integer input returned float64 and float32 input stayed float32.
- Positive values: for real input every result is greater than 0; very negative inputs approach 0.0 rather than going negative.
- Special values: in the same run,
np.exp(-np.inf)gave0.0,np.exp(np.inf)gaveinfandnp.exp(np.nan)gavenan. - Fold-change, odds or rate: a result of 2.0 from a log-scale effect means a doubling on the original scale; a result below 1 means a decrease.
Troubleshooting
RuntimeWarning: overflow encountered in exp
np.exp(np.array([1000.0])) returned [inf] and emitted a RuntimeWarning rather than raising an error. An inf in a downstream mean or regression often traces back to an un-scaled input. Standardise the input, or work on the log scale (for example, sum log-likelihoods instead of multiplying exponentials).
math.exp raises OverflowError instead
The Python documentation notes that CPython raises OverflowError on overflow, for instance math.exp(1000.0) (it printed “math range error” in our run). That differs from NumPy’s inf-plus-warning, which matters if you swap one for the other in a pipeline.
TypeError: only 0-dimensional arrays can be converted to Python scalars
math.exp accepts a single number, not an array. math.exp(np.array([1, 2])) raised this TypeError in our run; use np.exp for arrays.
Small x: exp(x) – 1 loses precision
The Python math documentation shows that exp(1e-5) - 1 gives 1.0000050000069649e-05 (accurate to 11 places) while expm1(1e-5) gives 1.0000050000166668e-05. NumPy’s np.expm1 (listed under “See also” on the np.exp manual page as exp(x) – 1) is built for this; prefer it for tiny effect sizes or rates.
Uninitialised values with where
See step 5: always pass out when you use where.
Related functions and free alternatives
| Function | Computes | Use when | Source |
|---|---|---|---|
numpy.exp(x) |
ex, element-wise | Arrays or scalars; back-transform of natural logs | NumPy manual |
numpy.expm1(x) |
exp(x) – 1 | x close to 0, where exp(x) – 1 loses precision | NumPy manual (See also); Python math docs |
numpy.exp2(x) |
2x | Values on a log2 scale | NumPy manual (See also) |
math.exp(x) |
ex for one number | Single floats in plain Python; the docs call it “usually more accurate than math.e ** x or pow(math.e, x)” | Python 3 math docs |
NumPy and the standard-library math module are both free and open source. For the same function in another environment, see exponential in MATLAB. To build a two-variable grid before evaluating exp over it, see NumPy meshgrid.
FAQ
What does np.exp do?
It computes ex for each element of the input, where e is about 2.718281. It returns a scalar for scalar input and an ndarray otherwise.
What is the inverse of np.exp?
np.log, the natural logarithm: if x = ln y then ex = y. Floating-point rounding means the round trip can differ in the last digit.
What is the difference between np.exp and math.exp?
np.exp works element-wise on arrays and returned inf with a warning on overflow in our run; math.exp takes a single number and raises OverflowError.
When should I use np.expm1 instead of np.exp(x) – 1?
For small x. The Python documentation’s example shows exp(1e-5) – 1 accurate to only 11 places, while expm1 keeps full precision.
Does np.exp work on complex numbers?
Yes. ea+ib = ea(cos b + i sin b).
Sources
- NumPy v2.5 Manual, numpy.exp, read 2026-11-07.
- NumPy, Installing NumPy, read 2026-11-07.
- Python documentation, math module (exp, expm1), read 2026-11-07.







