In this article, we’ll learn the Numpy float_power() function. Its syntax, explanation of parameters, and implementation with code examples will be included in this article.
numpy.float_power() Function of Numpy
We’ve learned about the simple Numpy power function. But in the numpy.float_power function, the calculation of element-wise power on the input array1(a1) elements raised to the power of corresponding array2(a2) items is performed by making use of the float power function. What it specifies is that the array1(a1) each element will be raised to the power specified by the array2(a2) corresponding element.
Images of Coding Examples
float_power() function’s Syntax
numpy.float_power(a1, a2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
Parameters
- The array ‘a1‘ is the base array while the array ‘a2‘ is the exponent array.
- Separation of the positional and keyword arguments is done by this special symbol(/).
- The output can be stored in a specific array using the ‘out‘ parameter.
- Whether to perform the operation on specific elements of arrays is specified using the ‘where‘ parameter.
- The casting rules can be set using the ‘casting‘ parameter.
- The memory layout of the result array is set by the ‘order‘ parameter.
- The data type of the output array is set using the parameter ‘dtype‘.
- The parameter ‘subok‘ specifies whether the output should be a subclass of the same class as the input arrays or not.
All the parameters except(a1 and a2) are optional.
7 Examples explaining numpy.float_power Function
Example 1: Simple 1D Arrays
import numpy as np xAr1 = np.array([5,3,6,2]) xAr2 = np.array([3,2,1,2]) resulA = np.float_power(xAr1, xAr2) print(resulA)
Output
[125. 9. 6. 4.]
The output shows that passing two arrays to it results in an array returned that has the result in float format. To give you an idea, this is how it’s working. The first item of both arrays is xAr1(5) and xAr2(3). The operation is performed like this (5*5*5).
Example 2: Parameter ‘out’ and ‘casting’
f1 = np.array([2,5,2,6,4]) f2 = np.array([4,6,3,8,2]) result_arr = np.zeros_like(f1) np.float_power(f1, f2, out=result_arr,casting='unsafe') print(result_arr)
Output
[ 16 15625 8 1679616 16]
In this code, we made an array of zeros that will be of the same shape as the first array(f1). Then we passed it to the ‘out‘ parameter to store the result. If we pass it as it is then it’ll raise an exception. See below:
UFuncTypeError: Cannot cast ufunc 'float_power' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
In order to solve it, we can use the ‘casting’ parameter and pass it the ‘unsafe‘. The default is ‘same_kind‘. So this is how we can solve this UFuncTypeError. Or you can set the type of the array passed to the ‘out’ parameter like this.
result_arr = np.zeros_like(f1,dtype=np.float64)
Now the output will be:
[1.600000e+01 1.562500e+04 8.000000e+00 1.679616e+06 1.600000e+01]
Example 3: ‘where’ parameter
aa1 = np.array([2, 3, 4, 5]) aa2 = np.array([2, 2, 3, 2]) condAr= np.array([True,True,False,False]) resultAA = np.float_power(aa1, aa2, where = condAr) print(resultAA)
Output
[4. 9. 6. 4.]
We specified an array(Booleans) and passed it to the ‘where’ parameter and as you can see that the output shows different because some items were not used in the operation where False was present.
Example 4: ‘casting’ Parameter
x1 = np.array([2, 3, 4, 5], dtype=np.float32) x2 = np.array([3, 2, 4, 1], dtype=np.int32) r = np.float_power(x1, x2, casting='safe') print(r)
Output
[ 8. 9. 256. 5.]
Passing ‘safe’ to the ‘casting’ parameter will use the datatype which can hold the result safely.
Example 5: ‘order’ Parameter
aa1 = np.array([[2, 5], [3, 6]]) aa2 = np.array([[3,4], [2,1]]) resultF = np.float_power(aa1,aa2, order='F') resultC = np.float_power(aa1,aa2, order='C') print(resultF) print(resultC)
Output
[[ 8. 625.] [ 9. 6.]] [[ 8. 625.] [ 9. 6.]]
Passing ‘C'(C style) or ‘F'(Fortran style) specifies which memory layout should be used for the result.
Example 6: ‘dtype’ parameter of float_power
array1 = np.array([3,2,4,1], dtype=np.int32) array2 = np.array([4,3,2,3], dtype=np.int32) resultingA = np.float_power(array1, array2, dtype=np.float64) print(resultingA)
Output
[81. 8. 16. 1.]
We can see that by using the ‘dtype’ parameter, we can change the data type of the resulting array, even if the input arrays are of different types from the result. In this code, we have input arrays of type int while the output items are of type float.
Example 7: Parameter ‘subok’
# Subclass of ndarray class MyArray(np.ndarray): pass ar1 = np.array([1,2,3,4]).view(MyArray) ar2 = np.array([4,5,2,3]).view(MyArray) resultArrr = np.float_power(ar1, ar2, subok=True) print(resultArrr) print(type(resultArrr))
Output
Conclusion
In conclusion, we learned the numpy.float_power() function of Python Numpy with the help of multiple code examples. The parameters of this function are explained practically with easy explanations.
Thank you for reading it.