NumPy power() Function in Python

Python NumPy power() function is used to compute the first array elements raised to powers from the second array elements, element-wise. Both arrays must be having the same shape and each element of the first array must be raised to the corresponding positive value from the second array. If both arrays are not of the same size or the second array has negative values, this function returns a value Error.

In this article, I will explain how to use the NumPy power() function using this function how to compute the power values of elements in an array.

1. Quick Examples of Python NumPy power() Function

If you are in a hurry, below are some quick examples of how to use the power() function in NumPy.


# Below are the quick examples
# Example 1: Get the scalar as exponent of an array 
arr = np.power(4,3)

# Example 2: Get the exponents of an array 
arr = np.array([2, 3, 5, 8, 9,4])
arr1 = np.power(arr, 3)

# Example 3: Get the exponents of two 1-Darray
arr = [2, 4, 6, 5, 3]
arr1 = [2, 3, 5, 4, 1]
arr2 = np.power(arr,arr1)

# Example 4: Use negative exponent with numpy Power() function
arr = [2, 4, 6, 5, 3]
arr1 = [-2, 3, -5, 4, 1]
arr2 = np.power(arr,arr1)

# Example 5: Get the power vales of 2-D array
arr = np.array([[4,2,3],[3,2,5]])
arr1 =np.array([[3,2,3],[1,2,3]])
arr2 = np.power(arr,arr1)

# Example 6: Get the power values using ** operator
arr = np.array([[4,2,3],[3,2,5]])
arr1 =np.array([[3,2,3],[1,2,3]])
arr2 = arr**arr1

2. Syntax of NumPy power() function

Following is the syntax of numpy.power() function.


# Syntax of numpy.power() 
numpy.power(arr, arr1, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None)

2.1 Parameter of power()

Allows following parameters.

  • arr – This is the first input array or object which works as a base.
  • arr1 – This is the second input array or object which works as an exponent.
  • out – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. When a freshly allocated array is returned unless received or None.
  • where – This condition is broadcast over the input. The out array will be set to a function(ufunc) result in locations where the condition is True.

2.2 Return Value of power()

It returns an array with elements of the first array raised to exponents in the second array(element wise). It returns ValueError, if the second array elements are negative. It returns scalar if both the first and second array have scalar elements.

3. Usage of NumPy power() Function

The np.power() is a mathematical library function used to return an array containing elements of the first array raised to the power element of the second array. The numpy power() function refers elements in the first array as a base and returns it to the power of the corresponding component of the second array.

3.1 Get the scalar Power Value

We can get the scalar value as a power value of given elements by passing the elements as parameters to this function. Here, the first element is the base and the second element is the exponent. The following example computes 4 to the 3rd power which returns 64.


import numpy as np
# Get the scalar as exponent of an array 
arr = np.power(4,3)
print(arr)

# Output :
# 64

4. Get the Power for 1-D Array

Let’s take a 1-D NumPy array and compute the exponents of given array elements using power() function. For that, first initialize the 1- D Numpy array using numpy.array() function.


# Get the exponents of an array
arr = np.array([2, 3, 5, 8, 9,4])
arr1 = np.power(arr, 3)
print(arr1)

# Output :
# [ 8  27 125 512 729  64]

We can get exponents of two 1- D NumPy arrays element wise. Here the first array ‘arr’ elements act as a base, and the second array ‘arr1’ elements act as an exponent. This function will return an array with elements of the first array raised to exponents in the second array(element-wise)


# Get the exponents of two 1-Darray
arr = [2, 4, 6, 5, 3]
arr1 = [2, 3, 5, 4, 1]
arr2 = np.power(arr,arr1)
print(arr2)

# Output :
# [   4   64 7776  625    3]

5. Use Negative Exponent With Power() Function

The numpy.power() doesn’t work with negative exponents. If you try to use the negative power, it will throw ValueError Integers to negative integer powers are not allowed.


# Use negative exponent with numpy Power() function
arr = [2, 4, 6, 5, 3]
arr1 = [-2, 3, -5, 4, 1]
arr2 = np.power(arr,arr1)
print(arr2)

# Output
# ValueError: Integers to negative integer powers are not allowed.

6. Get The Power of 2-D NumPy Array

We can calculate the powers of two-Dimensional NumPy arrays element wise using power() function. The shape of two 2-D NumPy arrays must be having the same shape. Let’s check the process.


# Create 2-D array
# Get the power vales of 2-D array
arr = np.array([[4,2,3],[3,2,5]])
arr1 =np.array([[3,2,3],[1,2,3]])
arr2 = np.power(arr,arr1)
print(arr2)

# Output :
# [[ 64   4  27]
# [  3   4 125]]

Alternatively, we can also use ** operator and get the power values of an array of elements.


# Get the power values using ** operator
arr = np.array([[4,2,3],[3,2,5]])
arr1 =np.array([[3,2,3],[1,2,3]])
print(arr**arr1)

# Output :
# [[ 64   4  27]
# [  3   4 125]]

You can see that the difference between the first and second approaches is execution time. The ** approach is slightly faster than the np.power() method.

8. Conclusion

In this article, I have explained how to use the NumPy power() function and using how to get the power values of single-dimensional and multi-dimensional NumPy arrays. The function is used to compute the first array elements raised to powers from the second array elements, element-wise.

Happy Learning!!

You May Also Like

References

numpy power

Leave a Reply

You are currently viewing NumPy power() Function in Python