Exponentiating transformed data
Convert log-scale values or model scores into multiplicative-scale values.
NumPy · Function
Computes the natural exponential of each element in scalar or array-like input. It supports broadcasting, optional output storage, and selective computation with where.
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.
numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature])| Name | Type | Description |
|---|---|---|
x | array_like | Input values. |
out | ndarray, None, or tuple of ndarray and None, optional | Destination for the result; it may be an ndarray, None, or a tuple of ndarray and None, and must accommodate the broadcast shape. |
where | array_like, optional | Boolean or array-like selector controlling where computation occurs; false positions remain unchanged in the output. |
casting | {'no','equiv','safe','same_kind','unsafe'}, optional | Casting rule used for operands and output. |
order | {'C','F','A','K'}, optional | Memory layout order for the result. |
dtype | dtype, optional | Data type used for the calculation and result. |
subok | bool, optional | Whether subclasses of ndarray are propagated. |
signature | string or None, optional | Generalized-ufunc signature, when applicable. |
ndarray or scalar: output array containing the element-wise exponential of x; scalar input produces a scalar.
out argument, the result is newly allocated.out argument provides storage for the computed result.out array must have a broadcast-compatible shape; otherwise the call cannot store the result.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.
Convert log-scale values or model scores into multiplicative-scale values.
Compute exponential terms across vectors or matrices without explicit Python loops.
Reuse an existing array as the destination for results.
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.
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.
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.expm1numpy.exp2