AppDividend
Latest Code Tutorials

Numpy float_power: How to Use np float_power() Function

Python NumPy power() is an inbuilt NumPy function that is used to get one array that contains elements of the first array raised to the power element of the second array. Means, if we have two arrays ( must be of same size ) arr1 and arr2 then numpy.power() will calculate arr1i^arr2i for each  i=0 to size_of_arrary-1. But this function is a little bit different from numpy.power(). In this function, no matter whether the input data is integer or float, we get the result as a float data type where minimum precision is float64 such that the result is always inexact.

Also, in this function, if we have negative numbers in arr2, it will not work through a ValueError; instead it returns a usable result and for positive power seldom overflows.

Python NumPy float_power()

NumPy float_power() function differs from the power function in those integers, float16, and float32 are promoted to floats with the minimum precision of float64 such that result is always inexact. The float_power() function will return a good result for negative powers and seldom overflow for +ve powers.

Syntax

numpy.float_power(arr1, arr2, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) 

Parameters

  1. arr1: This is the first array whose elements work as a base.
  2. arr2: This is the second array whose elements work as an exponent.
  3. out: This is a ndarray and an optional field. A place the result will be stored in. If given, the shape to which the inputs broadcast must be in. When a freshly-allocated array is returned unless received or None. A tuple (possible as a keyword argument only) must have a length equal to the outputs.
  4. where: This state is transmitted over data. The out array will be set to the ufunc result in locations where the condition is True. The out array will be maintaining its original interest elsewhere. Notice that if the default out = None produces an uninitialized out list, positions within it where the condition is False will remain uninitialized.

Return Value

The float_power() function returns an array with elements of the first array raised to the power element of the second array. The results will be in the float data type.

Program to show the working of float_power()

See the following code.

#Importing numpy
import numpy as np

#Declaring arr1 and arr2 whose elements are integer
arr1 = [2, 2, 1, 3, 5]
arr2 = [4, 0, 5, 2, 3]

#Now we will check working of power()
print("Elements of arr1 raised to the power elements of arr2 are: ")
print(np.float_power(arr1, arr2))

#Declaring arr1 and arr2 whose elements are float
arr1 = [2.5, 2.1, 1.4, 3, 5]
arr2 = [4, 0, 5, 2, 3]

#Now we will check working of power()
print("Elements of arr1 raised to the power elements of arr2 are: ")
print(np.float_power(arr1, arr2))

Output

Elements of arr1 raised to the power elements of arr2 are:
[ 16.   1.   1.   9. 125.]
Elements of arr1 raised to the power elements of arr2 are:
[ 39.0625    1.        5.37824   9.      125.     ]

Explanation

In the first case, we have first declared two arrays of the same shape, and all are integers, as you can see.  Then we have called numpy.float_power() to calculate arr1i^arr2i.

So, according to the formula, we got our desired output but in float data type.

In the second case, we have first declared two arrays of the same shape, and elements of arr1 are float data type, as you can see.  Then we have called numpy.float_power() to calculate arr1i^arr2i.

So, according to the formula, we got our desired output but in float data type.

Program to show when arr2 elements are negative

#Program when elements of arr2 are negative

#Importing numpy
import numpy as np

#Declaring arr1 and arr2 whose elements are integer
arr1 = [2, 2, 1, 3, 5]
arr2 = [-4, 0, 5, -2, 3]

#Now we will check working of power()
print("Elements of arr1 raised to the power elements of arr2 are: ")
print(np.float_power(arr1, arr2))

#Declaring arr1 and arr2 whose elements are float
arr1 = [2.5, 2.1, 1.4, 3, 5]
arr2 = [-4, 0, -5, 2, 3]

#Now we will check working of power()
print("Elements of arr1 raised to the power elements of arr2 are: ")
print(np.float_power(arr1, arr2))

Output

Elements of arr1 raised to the power elements of arr2 are:
[6.25000000e-02 1.00000000e+00 1.00000000e+00 1.11111111e-01
 1.25000000e+02]
Elements of arr1 raised to the power elements of arr2 are:
[2.56000000e-02 1.00000000e+00 1.85934432e-01 9.00000000e+00
 1.25000000e+02]

Explanation

In the first case, we have first declared two arrays of the same shape, and all are integers, as you can see, but in the second array (arr2), some elements are negative.  Then we have called numpy.float_power() to calculate arr1i^arr2i. As said earlier, this function returned a usable result.

In the second case, we have first declared two arrays of the same shape, and elements of arr1 are float data type as you can see, but in the second array (arr2), some elements are negative.  Then we have called float_power() to calculate arr1i^arr2i. As said earlier, this function returned a usable result.

See also

Numpy eye()

Numpy npv()

Numpy fv()

Numpy pv()

Numpy power()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.