numpy.float_power() in Python: Get Element-wise Power of Array

The numpy.float_power() function in Python is part of the NumPy library that is used to compute the element-wise power of array elements. It allows us to raise each element of an array to a specified power, providing flexibility and efficiency for handling power operations on arrays.

In this article, we will understand Python numpy.float_power() function, its syntax, and demonstrate it with various examples. Let’s get started.

Syntax of numpy.float_power() Function

Let us get started by understanding the fundamental constituents of the numpy.float_power() function with the help of its syntax given below.

Syntax:

numpy.float_power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None)

Parameters:

  • x1: This is the base array representing the base values.
  • x2: This is the exponent array representing the exponent values.
  • out (optional): This is an optional parameter that specifies the location where the result should be stored. If not provided, a new array will be created.
  • where (optional): This parameter is used for conditional execution. If provided, only the elements where the condition is True will be included in the result.
  • casting (optional): This parameter controls what kind of data casting may occur. The default is ‘same_kind’, which means that only casting between similar kinds (e.g., float to float) is allowed.
  • order (optional): Specifies the memory layout of the result. The default is ‘K’ which means the order is determined by the input array’s memory layout.
  • dtype (optional): This parameter allows us to set the data type of the output array. If not specified, it is determined by the data types of the input arrays.
  • subok (optional): If True, then sub-classes will be passed through. If False, the returned array will always be a base-class array.

Return:

An array with the same shape as the input arrays is returned by the numpy.float_power() method, where each element is calculated as x1^x2.

Examples of numpy.float_power() Function

Let us now look at some examples to demonstrate the use of numpy.float_power() function in Python.

1. Get the Scalar Power Value

In this example, we will compute the element-wise power by passing scalar inputs into the numpy.float_power() function.

Example:

import numpy as np
sc1 = 2
sc2 = 4
result = np.float_power(sc1, sc2)
print(result)

Here we imported the NumPy library as np then we took two scalar values: sc1 and sc2 and passed them into the numpy.float_power() function which will compute the power of sc1 raised to the power of sc2, then we saved the output into the result variable. In last, we printed the result variable.

Output:

Get the Scalar Power Value

In the output, we can see that we got 16.0 as 2^4 = 16. Since the numpy.float_power() function returns the result with a default data type of float that’s why we got 16.0.

2. Get the Array Power Value

In this example, we will take both the inputs as a one-dimensional array and pass them to the numpy.float_power() function which will return us the array with the same shape by raising each element of the first array to the power of the corresponding element present in the second array.

Example:

import numpy as np
ar1 = np.array([12, 1, 2])
ar2 = np.array([2, 3, 2])
result = np.float_power(ar1, ar2)
print(result)

Here by using the NumPy library, we created arrays of one-dimension and passed them into the numpy.float_power() function which will calculate the element-wise power, then we saved the output in the result variable. At last, we printed it.

Output:

Get the Array Power Value

In the output, we can see that we got an array where the first element is 144.0 because 12 raises to the power of 2 is 144.0. In the same way, the second element in the output array is 1^3=1.0 and the third element in the output array is 4 as 2^2 = 4.0.

3. Get the Array Power Value with a Scalar Base

In this example, we will compute the scalar value raising to the power of each element from the one-dimensional array using numpy.float_power() function.

Example:

import numpy as np
sc = 4
ar = np.array([1, 2, 3])
result = np.float_power(sc, ar)
print(result)

Output:

Get the Array Power Value with a Scalar Base

In the output, we can see that the first element in the output array is 4 because 4^1=4.0. In the same manner for the second element 4^2=16.0 and for the third element we got 4^3 = 64.0.

4. Passing the ‘order’ Parameter

In this example, we will change the memory layout of the resulting array. By default, it’s in ‘k’ order, but we’ll switch it to ‘F’ order, which is like a Fortran-style memory layout.

Example:

ar1 = np.array([[4, 5], [2, 7]])
ar2 = np.array([[4,2], [3,1]])
result = np.float_power(ar1,ar2, order='F')
print(result)

Output:

Passing the 'order' Parameter

In the output, we can see that numpy.float_power() function calculated the element-wise power of the base array ar1 with the exponent array ar2 resulting in an array whose memory layout is of Fortran style as we passed the order as ‘F’ in numpy.float_power() function.

Summary

Now that we have reached the end of this article, hope it has elaborated on the different ways to find the element-wise power of scalar value as well as the arrays by using the numpy.float_power() function from the NumPy library. For individuals who want to advance in Python, CodeforGeek has a ton of other entertaining and equally educational articles that could be quite beneficial.

Reference

https://numpy.org/doc/stable/reference/generated/numpy.float_power.html

Priyanshu Singh
Priyanshu Singh
Articles: 44