Numpy power function – Python NumPy power() Function

Numpy power function: Numpy is a robust and powerful Python package for data science. The numpy power() method, for example, takes the first input array as a base and raises it to the power of the second input array’s corresponding component.

NumPy power() Function in Python:

Numpy power: The power() method of the NumPy module is used to calculate the elements of the first array raised to powers from the second array, element by element.

Syntax:

numpy.power(arry1, arry2, out=None)

Parameters:

arry1: This is required. It is an array or an object that has the base elements.

arry2: This is required. It is an array or an object that has exponential elements. They must be broadcastable to a common shape if arry1.shape!= arry2.shape (that becomes the output’s shape).

out:

Numpy exponent: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value: 

This power() function returns an array with the first array’s elements(bases) raised to the second array’s exponential element. The output will be in the form of an integer.

NumPy power() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function to
    create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array, some random exponent value as the arguments to the power()
    function of the numpy module to get the given array elements raised to the given exponent
    value and print the result.
  • Here we gave the exponent as 2 so it returns the squares of each element of the array
  • Pass the above given array, some random exponent value as the arguments to the power()
    function of the numpy module to get the given array elements raised to the given exponent value
    value and print the result.
  • Here we gave the exponent as 3 so it returns the cubes of each element of the array
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([5, 2, 6, 7, 3])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array, some random exponent value as the arguments to the power() 
# function of the numpy module to get the given array elements raised to the given exponent 
# value and print the result.
# Here we gave the exponent as 2 so it returns the squares of each element of array
print("The squares of each element of array:")
print(np.power(gvn_arry, 2))
# Pass the above given array, some random exponent value as the arguments to the power() 
# function of the numpy module to get the given array elements raised to the given exponent value
# value and print the result.
# Here we gave the exponent as 3 so it returns the cubes of each element of array
print("The cubes of each element of array:")
print(np.power(gvn_arry, 3))

Output:

The above given array is:
[5 2 6 7 3]
The squares of each element of array:
[25 4 36 49 9]
The cubes of each element of array:
[125 8 216 343 27]

Example2

Approach:

  • Import numpy module using the import keyword
  • Pass some random bases list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Pass some random exponents list as an argument to the array() function to create another array.
  • Store it in another variable.
  • Pass the above given bases array and the exponents array as the arguments to the power()
    function of the numpy module to get the given base array elements raised to the given exponent
    array elements and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random bases list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_basearry = np.array([2, 6, 5, 1])      
# Pass some random exponents list as an argument to the array() function to
# create another array. 
# Store it in a variable.
gvn_exponentarry = np.array([5, 2, 3, 8]) 
# Pass the above given bases array and the exponents array as the arguments to the power() 
# function of the numpy module to get the given base array elements raised to the given exponent 
# array elements and print the result.
print("The given base array elements raised to the given exponent array elements:")
print(np.power(gvn_basearry, gvn_exponentarry))

Output:

The given base array elements raised to the given exponent array elements:
[ 32 36 125 1]

 

Leave a Comment