How to use the np.float_power() function for 2D array

What is numpy.float_power()?

The numpy.float_power() method gives a result that raises each element of the first array x1 to the power of each element of the second array x2.

Note: In Python, a list of lists can be used to create a two-dimensional (2-D) array.

Syntax

numpy.float_power(x1, x2, dtype=None, out=None)

Parameters

  • x1: array-like, which represents the data input.
  • x2: array-like, which represents the data input.
  • dtype: This is an optional parameter. It represents the array’s return type.
  • out: This is an optional parameter. It denotes the alternate output array where the result will be stored.

Note: If x1 and x2 have different shapes, they must be broadcastable to a common output shape.

Return value

The method numpy.float power() returns an array with x1 elements raised to exponents in x2.

Example

The following code shows how to use the numpy.float_power() method for 2-D arrays.

# import numpy
import numpy as np
# create a two 2D arrays
x1 = np.array([[2,7,3,9],[6,2,5,8]])
x2 = np.array([[5,4,6,11],[2,9,10,1]])
# compute the power of the 2D arrays
# and store the result in res
res = np.float_power(x1, x2)
print(res)

Explanation

  • Line 2: We import the numpy library.
  • Line 4–5: We create the x1 and x2 2-D arrays.
  • Line 9: To compute the power of the arrays x1 and x2, we utilize the np.float_power() function. The outcome is saved in a new variable named res.
  • Line 11: We display the result.

Free Resources