Python

# NumPy np.power()

The power() function in NumPy allows you to raise the elements from the first array to the power of the elements in the second array.

The operation of the NumPy power() function is applied element-wise. Think of it this way, the function takes the elements in the first array and matches them with its partner element in the second array. It then uses the component of the second array as the power.

Let us discuss this further.

## Function Syntax

The function syntax is as shown below:

numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'power'>

## Function Parameters

The function parameters are discussed in the points below:

1. x1 – refers to the input array whose elements act as the base in the power operation.
2. x2 – defines the input array whose elements work as the exponent in the power operation.

NOTE: Although the shape of both arrays can be different, they must be broadcastable to a standard shape.

## Function Return Value

As mentioned, the function returns an array with the elements of x1 raised to the power of x2 elements.

## Example 1

Consider the example shown below:

# import numpy
import numpy as np
x1 = np.array([1,2,3])
x2 = np.array([4,5,6])
print(np.power(x1, x2))

In the example above, we start by importing NumPy with the alias as np.

Next, we create two arrays, x1 and x2. Finally, we use the power() function to return the elements of x1 raised to the power of elements in x2.

The resulting output is as shown:

[  1  32 729]

## Example 2

The following example shows how to use the function with floating-point values.

x1 = np.array([2.3,4.5,5.6])
x2 = np.array([6.7,8.9,10.1])
print(np.power(x1, x2))

The code above should return an array as shown:

[2.65201821e+02 6.51015566e+05 3.60328881e+07]

## Example 3

The same case applies when the function is used on a multi-dimensional array. An example is illustrated below:

x1 = np.array([[1,2,3], [4,5,6]])
x2 = np.array([[7,8,9], [10,11,12]])
print(np.power(x1,x2))

The ab0ve code returns an array as shown:

[[          1         256       19683]
[    1048576    48828125 -2118184960]]

## Example 4

You can also raise the elements of first array with a common exponent as shown below:

x1 = np.array([[1,2,3], [4,5,6]])
print(np.power(x1, 2))

In this example, we pass a single value instead of giving an array in the second variable. This value acts as the exponent for each element in the first array.

This should return:

[[ 1  4  9]
[16 25 36]]

## Example 5

The function will return an error if the provided second array contain a negative value. An example is depicted below:

x1 = np.array([[-1,-2,-3], [-4,-5,-6]])
x2 = np.array([[-7,-8,9], [-10,-11,-12]])
print(np.power(x1, x

The code above should return an error as shown:

NOTE: This error applies only if the exponent array contains a negative value.

## Conclusion

This article covered how to use the power() function in NumPy. To explore further, check the docs.

Thanks for reading!!

### About the author

#### John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list