Numpy MaskedArray masked_outside()函数| Python
numpy.MaskedArray.masked_outside()
函数用于屏蔽给定间隔之外的数组。此函数是 masked_where 的快捷方式,其中对于区间[v1, v2] (arr
之外的 arr,条件为 True。边界 v1 和 v2 可以按任意顺序给出。
Syntax : numpy.ma.masked_outside(arr, v1, v2, copy=True)
Parameters:
arr : [ndarray] Input array which we want to mask.
v1, v2 : [int] Lower and upper range.
copy : [bool] If True (default) make a copy of arr in the result. If False modify arr in place and return a view.
Return : [ MaskedArray] The resultant array after masking.
代码#1:
# Python program explaining
# numpy.MaskedArray.masked_outside() method
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
# creating input array
in_arr = geek.array([1, 2, 3, -1, 2])
print ("Input array : ", in_arr)
# applying MaskedArray.masked_outside methods
mask_arr = ma.masked_outside(in_arr, -1, 1)
print ("Masked array : ", mask_arr)
输出:
Input array : [ 1 2 3 -1 2]
Masked array : [1 -- -- -1 --]
代码#2:
# Python program explaining
# numpy.MaskedArray.masked_outside() method
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
# creating input array
in_arr = geek.array([5e8, 3e-5, -45.0, 4e4, 5e2])
print ("Input array : ", in_arr)
# applying MaskedArray.masked_outside methods
mask_arr = ma.masked_outside(in_arr, 5e2, 5e8)
print ("Masked array : ", mask_arr)
输出:
Input array : [ 5.0e+08 3.0e-05 -4.5e+01 4.0e+04 5.0e+02]
Masked array : [500000000.0 -- -- 40000.0 500.0]