.power()
Anonymous contributor
Published Jun 27, 2024
Contribute to Docs
In the NumPy library, the .power()
method raises each element in the first array to the power of the corresponding element in the second array and computes the result. It returns an array without commas separating the elements. To view the output with commas, use .repr()
.
Syntax
numpy.power(x1, x2, out=None, where=True)
x1
: A number or an array of numbers used as the base.x2
: A number or an array of numbers used as the exponent for elements inx1
.out
: An optional parameter that allows us to store the output array. If not specified, a new array will be allocated for the result.where
: An optional parameter that determines the elements on which the method is to be applied.- If the condition is
True
for a particular element, the power is computed. - If the condition is
False
for a particular element, the original element is retained. - If not provided, the power is computed for all elements.
- If the condition is
Example
The below example shows how to use .power()
method:
# Importing the numpy libraryimport numpy as npprint("2^3 is equal to", np.power(2,3))# Input Arrayarray1 = [2, 3, 4, 5, 6]array2 = [6, 5, 4, 2, 2]print ("\nOutput array : ", np.power(array1, array2))
The code above generates the following output:
2^3 is equal to 8Output array : [64 243 256 25 36]
Codebyte Example
In this codebyte example, .power()
method computes the power where each element in array1
serves as the base raised to the power of the corresponding element in array2
:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.