Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.exp

Computes the natural exponential of each element in scalar or array-like input. It supports broadcasting, optional output storage, and selective computation with where.

How it works

np.exp is a universal function that applies the natural exponential element by element. It accepts scalar or array-like input, supports broadcasting and optional masked or preallocated output, and returns a scalar for scalar input or an array otherwise.

Syntax

numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature])

Parameters

NameTypeDescription
xarray_likeInput values.
outndarray, None, or tuple of ndarray and None, optionalDestination for the result; it may be an ndarray, None, or a tuple of ndarray and None, and must accommodate the broadcast shape.
wherearray_like, optionalBoolean or array-like selector controlling where computation occurs; false positions remain unchanged in the output.
casting{'no','equiv','safe','same_kind','unsafe'}, optionalCasting rule used for operands and output.
order{'C','F','A','K'}, optionalMemory layout order for the result.
dtypedtype, optionalData type used for the calculation and result.
subokbool, optionalWhether subclasses of ndarray are propagated.
signaturestring or None, optionalGeneralized-ufunc signature, when applicable.

Returns

ndarray or scalar: output array containing the element-wise exponential of x; scalar input produces a scalar.

Key points

  • The function applies the exponential operation independently to every input element.
  • Without an out argument, the result is newly allocated.
  • The returned array contains the element-wise exponential values.
  • Scalar input produces a scalar result rather than an array.
  • The optional out argument provides storage for the computed result.

Common mistakes

  • An out array must have a broadcast-compatible shape; otherwise the call cannot store the result.

Example: Element-wise exponentials

import numpy as np

x = np.array([-1.0, 0.0, 1.0])
result = np.exp(x)
print(result)

out = np.empty_like(x)
np.exp(x, out=out)
print(out)

Computes the exponential element by element, then demonstrates writing the result into a supplied output array.

When to use it

Exponentiating transformed data

Convert log-scale values or model scores into multiplicative-scale values.

Vectorized numerical calculations

Compute exponential terms across vectors or matrices without explicit Python loops.

Memory-conscious output storage

Reuse an existing array as the destination for results.

Interview questions

QWhat does np.exp compute and what does it return?

np.exp(x) computes the natural exponential element by element. For array-like input it produces an array, while scalar input produces a scalar.

Follow-up: How would you avoid allocating a second result array? Pass a compatible array through the out keyword.

QHow does where affect np.exp?

The where argument selects positions at which the ufunc computes. Positions where it is false are left unchanged in the output array.

Follow-up: What should be done before using where with a newly allocated output? Initialize the output so skipped positions contain intentional values.

QWhat is the purpose and shape requirement of out?

out receives the computed values and must have a shape compatible with broadcasting the inputs. If it is omitted or None, NumPy allocates and returns a fresh result.

Follow-up: Can a scalar result be expected for a one-element array? No; the documentation distinguishes scalar input from array input, so an array input returns an array.

  • numpy.expm1
  • numpy.exp2

Source: numpy.exp. Checked 2026-10-15.