NumPy float_power

NumPy Float Power Cover Image

Hello and welcome to this tutorial on Numpy float_power. In this tutorial, we will be learning about the NumPy float_power() method and also seeing a lot of examples regarding the same. So let us begin!

Also Read: NumPy Power – Raising another number to the power of


What is NumPy float_power?

The float_power() method in NumPy is a function that returns an array calculated by raising the elements in one array to the power corresponding to the value in the second array.

If x1 and x2 are two arrays, then float_power(x1, x2) computes the output element-wise, i.e. by raising each value in x1 to the value in x2 at the corresponding position. As the name of the function itself suggests, its default return type is float. We will see the examples for this function in the upcoming section of this tutorial.


Syntax of NumPy float_power

numpy.float_power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
ParameterDescriptionRequired/Optional
x1 (array_like)Base array.Required
x2 (array_like)Power/Exponent array.Required
outAn alternative output array in which to place the result. It must have the same shape as the expected output.Optional
whereTakes an array-like object. At locations where it is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.Optional

Returns: 
An array containing the result of x1 raised to x2, element-wise. If x1 and x2 are scalar, then the result is also a scalar.


Examples of Numpy float_power()

Lets now get right into the examples and understand how the float_power method actually works.

When both inputs are scalars

import numpy as np

a = 5
b = 3
# using the float_power method 
ans = np.float_power(a, b)

print("a = ", a, "\nb =", b)
print("Result = ", ans)

Output:

a =  5 
b = 3
Result =  125.0

A simple example where the result is calculated as

5^3 = 125

When one input is a scalar and the other a 1-dimensional array

import numpy as np

a = [0, -2, 4, -6, 8]
b = 3
# using the float_power method 
ans = np.float_power(a, b)

print("a = ", a, "\nb =", b)
print("Result = ", ans)

Output:

a =  [0, -2, 4, -6, 8] 
b = 3
Result =  [   0.   -8.   64. -216.  512.]

Here, each element in the array a is raised to the power b and the output is calculated as

ans[0] = a[0] ^ b = 0 ^ 3 = 0
ans[1] = a[1] ^ b = -2 ^ 3 = -8
ans[2] = a[2] ^ b = 4 ^ 3 = 64
ans[3] = a[3] ^ b = -6 ^ 3 = -216
ans[4] = a[4] ^ b = 8 ^ 3 = 512

From the output, we can see that the function also handles negative values.


When both the input arrays are 1-dimensional

import numpy as np

a = [3, 1, 4, 2.5]
b = [0, 2, 2.7, 4]
# using the float_power method 
ans = np.float_power(a, b)

print("a = ", a, "\nb =", b)
print("Result = ", ans)

Output:

a =  [3, 1, 4, 2.5] 
b = [0, 2, 2.7, 4]
Result =  [ 1.          1.         42.22425314 39.0625    ]

Here, each element in a is raised to the power of the corresponding element in b and the output is calculated as:

ans[0] = a[0] ^ b[0] = 3 ^ 0 = 1
ans[1] = a[1] ^ b[1] = 1 ^ 2 = 1
ans[2] = a[2] ^ b[2] = 4 ^ 2.7 = 42.22425314
ans[3] = a[3] ^ b[3] = 2.5 ^ 4 = 39.0625

Observe that even floating point numbers are handled by the float_power() method.


When both the input arrays are 2-dimensional

import numpy as np

a = [[1, 2], [6, 3]]
b = [[2, 5], [2, 3]]
# using the float_power method 
ans = np.float_power(a, b)

print("a = ", a, "\nb =", b)
print("Result = \n", ans)

Output:

a =  [[1, 2], [6, 3]] 
b = [[2, 5], [2, 3]]
Result = 
 [[ 1. 32.]
 [36. 27.]]

Similar to the above example,

ans[0][0] = a[0][0] ^ b[0][0] = 1 ^ 2 = 1
ans[0][1] = a[0][1] ^ b[0][1] = 2 ^ 5 = 32
 
ans[1][0] = a[1][0] ^ b[1][0] = 6 ^ 2 = 36
ans[1][1] = a[1][1] ^ b[1][1] = 3 ^ 3 = 27

Conclusion

That’s all! In this tutorial, we learned about the Numpy float_power method and practiced different types of examples using the same.  

If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference