我屏蔽我的数组,其中值是nodata(-9999),计算轴上的平均值= 0然后取消屏蔽我的数据数组,但后来我的nodata值变为0,但现在如何区分“计算意思是0“和”nodata 0“。请参阅以下代码示例:
In [1]: import numpy.ma as ma
...: x = [[0.,1.,-9999.,3.,4.],[0.,2.,-9999,4.,5.]]
...: x
Out[1]: [[0.0, 1.0, -9999.0, 3.0, 4.0], [0.0, 2.0, -9999, 4.0, 5.0]]
In [2]: mx = ma.masked_values(x, -9999.)
...: mx
Out[2]:
masked_array(data =
[[0.0 1.0 -- 3.0 4.0]
[0.0 2.0 -- 4.0 5.0]],
mask =
[[False False True False False]
[False False True False False]],
fill_value = -9999.0)
In [3]: mean = mx.mean(axis=0)
...: mean
Out[3]:
masked_array(data = [0.0 1.5 -- 3.5 4.5],
mask = [False False True False False],
fill_value = 1e+20)
In [4]: mean.mask = ma.nomask
...: mean
Out[4]:
masked_array(data = [0.0 1.5 0.0 3.5 4.5],
mask = [False False False False False],
fill_value = 1e+20)
但我希望输出类似于我的输入,其中nodata值为-9999。,如:
In [4]: mean.mask = ma.nomask
...: mean
Out[4]:
masked_array(data = [0.0 1.5 -9999. 3.5 4.5],
mask = [False False False False False],
fill_value = 1e+20)
答案 0 :(得分:5)
>>> mean = mx.mean(axis=0)
>>> mean[mean.mask] = mx.fill_value
>>> mean
masked_array(data = [0.0 1.5 -9999.0 3.5 4.5],
mask = [False False False False False],
fill_value = 1e+20)