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:

- x1 – refers to the input array whose elements act as the colchoneta in the power operation.
- 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 mote 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:

**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:

**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!!