.power()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
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 in x1.
  • 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.

Example

The below example shows how to use .power() method:

# Importing the numpy library
import numpy as np
print("2^3 is equal to", np.power(2,3))
# Input Array
array1 = [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 8
Output 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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python:NumPy on Codecademy