NumPy power()
The numpy.power() function raises elements of an array to the power of corresponding elements in another array, element-wise. Both arrays must be broadcastable to the same shape.
Syntax
</>
Copy
numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
| Parameter | Type | Description |
|---|---|---|
x1 | array_like | The base values. |
x2 | array_like | The exponent values. If x1.shape is not equal to x2.shape, they must be broadcastable to a common shape. |
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 power. |
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 where each element of x1 is raised to the corresponding power in x2. If both inputs are scalars, a scalar is returned.
Examples
1. Computing Power of Scalars
Computing the power of a single base raised to a single exponent.
</>
Copy
import numpy as np
# Define base and exponent
base = 2
exponent = 3
# Compute power
result = np.power(base, exponent)
# Print result
print("2^3 =", result)
Output:
2^3 = 8
