NumPy exp()

The numpy.exp() function calculates the exponential of all elements in the input array. It computes e raised to the power of each element in the input array, where e is Euler’s number, approximately equal to 2.718.

Syntax

</>
Copy
numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput values. Exponential is calculated for each element.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the exponential.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the exponential of each input element. If the input is a scalar, a scalar is returned.


Examples

1. Calculating the Exponential of a Single Value

In this example, we calculate the exponential of a single numeric value.

</>
Copy
import numpy as np

# Define a single value
value = 1

# Calculate the exponential of the value
result = np.exp(value)

# Print the result
print("Exponential of 1:", result)

Output:

Exponential of 1: 2.718281828459045

2. Calculating Exponential for an Array

Here, we calculate the exponential values for an array of numbers.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([0, 1, 2, 3])

# Calculate the exponential of each value in the array
exp_values = np.exp(values)

# Print the results
print("Input values:", values)
print("Exponential values:", exp_values)

Output:

Input values: [0 1 2 3]
Exponential values: [ 1.          2.71828183  7.3890561  20.08553692]

3. Using the out Parameter

Utilizing an output array to store the results instead of creating a new one.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([0, 0.5, 1.5])

# Create an output array with the same shape
output_array = np.empty_like(values)

# Calculate exponential and store in output_array
np.exp(values, out=output_array)

# Print the results
print("Exponential values stored in output array:", output_array)

Output:

Exponential values stored in output array: [1.         1.64872127 4.48168907]

4. Using the where Parameter

Using a condition to calculate the exponential only for selected elements.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([0, 1, 2, 3])

# Define a mask (calculate exponential only where mask is True)
mask = np.array([True, False, True, False])

# Calculate exponential values where mask is True
result = np.exp(values, where=mask)

# Print the results
print("Exponential values with mask applied:", result)

Output:

Exponential values with mask applied: [1.        0.        7.3890561 0.       ]

Here, the exponential calculation occurs only at positions where mask=True. The other positions retain their original value.