Numpy MaskedArray.power () Function | python

numpy.MaskedArray.power() is used to compute the elementwise base array raised to the power from the second array. It raises each base in arr1 to the position-corresponding cardinality in arr2. arr1 and arr2 must translate to the same form. Note that an integer type raised to a negative integer power will raise a ValueError .

Syntax: numpy.ma.power (arr1, arr2, third = None)

Parameters:
arr1: [array_like] The base masked array.
arr2: [array_like] The exponents masked array.
third: [ndarray, optional] A location into which the result is stored.
 – & gt; If provided, it must have a shape that the inputs broadcast to.
 – & gt; If not provided or None, a freshly-allocated array is returned.

Return: [ndarray] The bases in arr1 raised to the exponents in arr2.

Code # 1:

# Python program explaining
# numpy.MaskedArray.power () method

 
# import numy as geek
# and numpy.ma module as ma

import numpy as geek 

import numpy.ma as ma 

 
# create base array

base_arr = geek.array ([< / code> 0 , 1 , 2 , 3 , 4 , 5 ]) 

print ( "Input base array:" , base_arr)

 
# We are now creating a base array.
# making one entry invalid.

base_mask_arr = ma.masked_array (base_arr, mask = [ 0 , 0 , 0 , 0 , 1 , 0 ]) 

print ( "Base Masked array:" , base_mask_arr) 

 
# create exponent array

exp_arr = geek.array ([ 0 , 2 , 1 , 4 , 2 , 3 ]) 

print ( " Input exponent array: " , exp_arr)

 
# We now create an array of the exponent mask.
# invalidating one entry.

exp_mask_arr = ma.masked_array (exp_arr, mask = [ 0 , 1 , 0 , 0 , 1 , 0 ] ) 

print ( "Exponent Masked array : " , exp_mask_arr) 

 
# applying the MaskedArray.power methods
# into the masked array

out_arr = ma.power (base_mask_arr, exp_mask_arr) 

print ( "Output masked array:" , out_arr)

Output:

 Input base array: [0 1 2 3 4 5] 
Base Masked array: [0 1 2 3 - 5]
Input exponent array: [0 2 1 4 2 3]
Exponent Masked array: [0 - 1 4 - 3 ]
Output masked array: [1 - 2 81 - 125]