[英]Handling masked numpy array
I have masked numpy array. 我已经屏蔽了numpy数组。 While doing processing for each of the element, I need to first check whether the particular element is masked or not, if masked then I need to skip those element.
在对每个元素进行处理时,我需要首先检查特定元素是否被遮罩,如果被遮罩,则需要跳过那些元素。
I have tried like this : 我已经这样尝试过:
from netCDF4 import Dataset
data=Dataset('test.nc')
dim_size=len(data.dimensions[nc_dims[0]])
model_dry_tropo_corr=data.variables['model_dry_tropo_corr'][:]
solid_earth_tide=data.variables['solid_earth_tide'][:]
for i in range(0,dim_size)
try :
model_dry_tropo_corr[i].mask=True
continue
except :
Pass
try:
solid_earth_tide[i].mask=True
continue
except:
Pass
correction=model_dry_tropo_corr[i]/2+solid_earth_tide[i]
Is there other efficient way to do this, please do let me know. 还有其他有效的方法可以做到这一点,请告诉我。 Your suggestion or comments are highly appreciated.
非常感谢您的建议或意见。
Instead of a loop you could use 可以使用循环代替循环
correction = model_dry_tropo_corr/2 + solid_earth_tide
This will create a new masked array that will have your answers and masks. 这将创建一个新的蒙版数组,其中包含您的答案和蒙版。 You could then access unmasked values from new array.
然后,您可以从新数组访问未屏蔽的值。
I'm puzzled about this code 我对此代码感到困惑
try :
model_dry_tropo_corr[i].mask=True
continue
except :
Pass
I don't have netCDF4
installed, but it appears from the documentation that your variable will look like, maybe even be a numpy.ma
masked array. 我没有安装
netCDF4
,但是从文档中可以看到您的变量看起来像,甚至可能是numpy.ma
掩码数组。
It would be helpful if you printed all or part of this variable, with attributes like shape and dtype. 如果您打印此变量的全部或一部分,并且具有shape和dtype之类的属性,将很有帮助。
I can make a masked array with an expression like: 我可以使用以下表达式制作蒙版数组:
In [746]: M=np.ma.masked_where(np.arange(10)%3==0,np.arange(10))
In [747]: M
Out[747]:
masked_array(data = [-- 1 2 -- 4 5 -- 7 8 --],
mask = [ True False False True False False True False False True],
fill_value = 999999)
I can test whether mask for a given element if True/False with: 我可以使用以下命令测试给定元素的掩码是否为True / False:
In [748]: M.mask[2]
Out[748]: False
In [749]: M.mask[3]
Out[749]: True
But if I index first, 但是如果我先索引
In [754]: M[2]
Out[754]: 2
In [755]: M[3]
Out[755]: masked
In [756]: M[2].mask=True
...
AttributeError: 'numpy.int32' object has no attribute 'mask'
In [757]: M[3].mask=True
So yes, your try/except will skip the elements that have the mask set True. 因此,可以,您的try / except将跳过掩码设置为True的元素。
But I think it would be clear to do: 但是我认为这样做很明显:
if model_dry_tropo_corr.mask[i]:
continue
But that is still iterative. 但这仍然是迭代的。
But as @user3404344
showed, you could perform the math with the variables. 但是,正如
@user3404344
所示,您可以使用变量执行数学运算。 Masking will carry over. 遮罩将继续。 That could though be a problem if masked values are 'bad' and cause errors in the calculation.
如果被屏蔽的值是“错误的”并导致计算错误,那可能是个问题。
If I define another masked array 如果我定义另一个蒙版数组
In [764]: N=np.ma.masked_where(np.arange(10)%4==0,np.arange(10))
In [765]: N+M
Out[765]:
masked_array(data = [-- 2 4 -- -- 10 -- 14 -- --],
mask = [ True False False True True False True False True True],
fill_value = 999999)
you can see how elements that were masked in either M
or N
are masked in the result 您可以看到在结果中如何掩盖了
M
或N
被掩盖的元素
I can used the compressed
method to give only the valid elements 我可以使用
compressed
方法仅给出有效元素
In [766]: (N+M).compressed()
Out[766]: array([ 2, 4, 10, 14])
filling can also be handy when doing math with masked arrays: 使用掩码数组进行数学运算时,填充也很方便:
In [779]: N.filled(0)+M.filled(0)
Out[779]: array([ 0, 2, 4, 3, 4, 10, 6, 14, 8, 9])
I could use filled
to neutralize problem calculations, and still mask those values 我可以使用
filled
来中和问题计算,并仍然掩盖这些值
In [785]: z=np.ma.masked_array(N.filled(0)+M.filled(0),mask=N.mask|M.mask)
In [786]: z
Out[786]:
masked_array(data = [-- 2 4 -- -- 10 -- 14 -- --],
mask = [ True False False True True False True False True True],
fill_value = 999999)
Oops, I don't need to worry about the masked values messing the calculation. 糟糕,我不必担心掩码值会使计算混乱。 The masked addition is doing the filling for me
蒙面的添加物正在为我做馅
In [787]: (N+M).data
Out[787]: array([ 0, 2, 4, 3, 4, 10, 6, 14, 8, 9])
In [788]: N.data+M.data # raw unmasked addition
Out[788]: array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
In [789]: z.data # same as the (N+M).data
Out[789]: array([ 0, 2, 4, 3, 4, 10, 6, 14, 8, 9])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.