Numpy MaskedArray.power() function | Python
numpy.MaskedArray.power()
function is used to compute element-wise base array raised to power from second array. It raise each base in arr1 to the positionally-corresponding power in arr2. arr1 and arr2 must be broadcastable to the same shape. 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.
-> If provided, it must have a shape that the inputs broadcast to.
-> 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 # importing numpy as geek # and numpy.ma module as ma import numpy as geek import numpy.ma as ma # creating base array base_arr = geek.array([ 0 , 1 , 2 , 3 , 4 , 5 ]) print ( "Input base array : " , base_arr) # Now we are creating a base masked array. # by making one entry as invalid. base_mask_arr = ma.masked_array(base_arr, mask = [ 0 , 0 , 0 , 0 , 1 , 0 ]) print ( "Base Masked array : " , base_mask_arr) # creating exponent array exp_arr = geek.array([ 0 , 2 , 1 , 4 , 2 , 3 ]) print ( "Input exponent array : " , exp_arr) # Now we are creating a exponent masked array. # by making one entry as invalid. exp_mask_arr = ma.masked_array(exp_arr, mask = [ 0 , 1 , 0 , 0 , 1 , 0 ]) print ( "Exponent Masked array : " , exp_mask_arr) # applying MaskedArray.power methods # to masked array out_arr = ma.power(base_mask_arr, exp_mask_arr) print ( "Output masked array : " , out_arr) |
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]
Contact Us