NumPy exp2()
The numpy.exp2() function calculates 2 raised to the power of each element in an input array.
Syntax
</>
Copy
numpy.exp2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
| Parameter | Type | Description |
|---|---|---|
x | array_like | Input values. Each element will be used as an exponent to base 2. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the power function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with values of \(2^x\) computed element-wise. If the input is a scalar, a scalar is returned.
Examples
1. Computing 2^x for a Single Value
Here, we compute \(2^x\) for a single value.
</>
Copy
import numpy as np
# Define an exponent value
x = 3
# Compute 2^x
result = np.exp2(x)
# Print the result
print("2^3 =", result)
Output:
2^3 = 8.0
