Numpy MaskedArray.power()函数| Python
numpy.MaskedArray.power()
函数用于计算从第二个数组提升到幂的元素基础数组。它将 arr1 中的每个碱基提高到 arr2 中的位置对应幂。 arr1 和 arr2 必须可广播到相同的形状。请注意,整数类型的负整数幂将引发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.
代码#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]