Power Function of Python Numpy [7 Easy Code Examples]

Power Function of Python Numpy explanation with python code examples parameters explained in jupyter notebook

In this article, we’ll walk ourselves through the power function of Python Numpy. To properly understand the role and usage of this function, we’ll use multiple code examples with good explanations of the parameters of numpy.power() function.

Power Function of Python Numpy

The use of the Numpy power function is to raise the elements of the array passed to it as an argument to a given power. A new array is returned by the power() function that contains the values computed as the base element raised to the specified power.

Power Function of Python Numpy explanation with python code examples parameters explained in jupyter notebook

parameters of python numpy power function with code examples in python explanation

python numpy power function with code examples in python

Syntax of power()

numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters of numpy.power() Function

  1. The parameter(x1) can be an array or a scalar value that stores the base values and they will be raised to the specified power.
  2. The (x2) can also be an array or a scalar value and they specify the power to which the ‘x1‘ array’s elements will be raised.
  3. The special parameter(/) is used to separate the positional arguments(x1, x2) from the keyword arguments(all the other arguments). This parameter is called the ‘positional-only‘ marker.
  4. The optional parameter(out) is used to store the resulting array. Make sure this array’s shape got matched with the input arrays. If this parameter is not set, then a new array will be created.
  5. The optional parameter(where) is used to specify for which items should the operation be performed. It can take an array of Boolean values. True means to perform the operation and False means to not perform it. The (x1, x2) dimensions and (where) must be broadcastable to a shape that is common.
  6. The (casting) parameter is optional and its role is to handle the casting between different data types. It can take ‘no‘, ‘safe‘, or ‘equiv‘. The default value given to it is ‘same_kind‘ and it shows that only the ‘safe‘ casts are allowed. The ‘no‘ specifies no casting while the ‘equiv‘ represents the identical datatypes casting.
  7. The parameter (order) is optional and it sets the output array’s memory layout.  It can take ‘A‘ for ‘any’ memory layout, ‘C‘ for C-style contiguous, or ‘F‘ for Fortran-style contiguous. The default value provided to it is ‘K’ which represents keeping the input array’s memory layout.
  8. The optional parameter(dtype) is used to set the output array’s datatype. The datatype is taken from x1 and x2, if this parameter is not specified.
  9. The parameter(subok) also comes as an optional parameter. It takes a boolean value. Passing True to it will make the resulting array a subclass of the input array. While passing False to it will make the resulting array a base class array.

7 code Examples of Python numpy.power() Function

Example 1: 2 Arrays with power() Function

import numpy as np

a1 = np.array([3,6,3])
a2 = np.array([4, 2, 3])

result = np.power(a1, a2)
print(result)

Output

[81 36 27]

In this code, we passed 2 arrays to the power function. The first array will work as a base while the second one will be specified as power. For instance, if we take the first items of both these arrays(3 and 4) then they will be (3*3*3*3) as the item of the first array(a1) 3 has a power 4(a2).

Example 2: Applying (out) parameter

ar1 = np.array([2, 3, 4])
ar2 = np.array([3, 2, 1])

outputAr = np.empty_like(ar1)

np.power(ar1, ar2, out=outputAr)
print(outputAr)

Output

[8 9 4]

In this example, we created an empty array that will have the same shape as the first array. Then we passed it to the ‘out‘ parameter of the power function. We can see that the empty array now has the resulting value generated by the power() function.

Example 3: Parameter (where)

firstArray = np.array([2, 3, 4])
secondArray = np.array([3, 2, 6])
conditionsArray = np.array([True, False, False])

res = np.power(firstArray, secondArray, where=conditionsArray)
print(res)

Output

[8 2 6]

We specified an array of Boolean values and passed it to the parameter ‘where‘. During operation, when the value is True, only then the operation of power will perform, else it’ll return the item of the second array(second argument). In this example, the operation got executed only for the first items of both arrays as the first item of the Boolean array passed to the ‘where‘ parameter is True, while its False for other items.

Example 4: (casting) Parameter

arrr1 = np.array([4,6,2], dtype=np.int64)
arrr2 = np.array([2,1,5], dtype=np.float64)

resultAr = np.power(arrr1, arrr2, casting='safe')
print(resultAr)

Output

[16.  6. 32.]

In this Python code, we specified the ‘casting’ parameter to be ‘safe’ which means conversion of the datatype from one to another while ensuring that the data do not lose precision. We set the first array to be of type Int while the second array is set to be of type Float. The result is now in float but the precision is retained.

Example 5: (order) Parameter of numpy.power() applied on 2D Array

twoDAr1 = np.array([[4,7], [2,4]])
twoDAr2 = np.array([[1,3], [2,6]])

resultArrayF = np.power(twoDAr1, twoDAr2, order='F')
print(resultArrayF)

resultArrayC = np.power(twoDAr1, twoDAr2, order='C')
print(resultArrayC)

Output

[[   4  343]      # this is F order
 [   4 4096]]
[[   4  343]       # this is C order
 [   4 4096]]

We specified the ‘order’ parameter for both ‘C‘ and ‘F‘. Although, the result looks the same for both and the reason is that we are using the same shape. But the first array represents the ‘F‘ order while the second array shows the ‘C‘ order.

Example 6: Implementing (dtype) Parameter

b1 = np.array([1,2,4,6])
b2 = np.array([2,3,4,1])

resultA = np.power(b1, b2, dtype=np.float64)
print(resultA)

Output

[  1.   8. 256.   6.]

This example shows that we set the ‘dtype’ parameter to be of type float but the input arrays are of type integers. As a result, the resulting array values are in the float format. So, it means by using the ‘dtype‘ parameter, we can set the datatypes of the items of the output array.

Example 7: Using (subok) Parameter of Numpy Power Function

class MyArr(np.ndarray):
  pass

aa1 = np.array([2, 4, 5]).view(MyArr)
aa2 = np.array([2, 2, 3]).view(MyArr)

rAr = np.power(aa1, aa2, subok=True)
print(rAr)
print(type(rAr))

Output

[  4  16 125]
<class '__main__.MyArr'>

In this code:

  • we created a subclass of Python Numpy ‘ndarray’ class which is ‘MyArr‘.
  • We then created the two arrays(aa1, aa2) by making use of the ‘view()‘ method.
  • After that, we used the ‘subok’ parameter and set it to True. It recognizes that the input arrays (aa1, aa2) are custom ‘MyArr‘ class instances. It means that the output value of the power function will also be the same as ‘MyArr‘ subclass. The output can be seen above.

Conclusion

In conclusion, we learned how to use the Power function of Python Numpy. Multiple Python code examples with explanations were used to explain the parameters of power function.

Thank you for reading this article.

Leave a Comment

Your email address will not be published. Required fields are marked *