简体   繁体   English

替换蒙版的numpy数组中的值不起作用

[英]Replace values in masked numpy array not working

I have the foll. 我有傻瓜。 masked array in numpy called arr with shape (50, 360, 720): numpy中的蒙版数组,称为arr,形状为(50,360,720):

masked_array(data =
 [[-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 ..., 
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]],
             mask =
 [[ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 ..., 
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]],
       fill_value = 1e+20)

It has the foll. 有愚蠢。 data in arr[0]: 数据在arr [0]中:

arr[0].data

array([[-999., -999., -999., ..., -999., -999., -999.],
       [-999., -999., -999., ..., -999., -999., -999.],
       [-999., -999., -999., ..., -999., -999., -999.],
       ..., 
       [-999., -999., -999., ..., -999., -999., -999.],
       [-999., -999., -999., ..., -999., -999., -999.],
       [-999., -999., -999., ..., -999., -999., -999.]])

-999. -999。 is the missing_value and I want to replace it by 0.0. 是missing_value,我想将其替换为0.0。 I do this: 我这样做:

arr[arr == -999.] = 0.0

However, arr remains the same even after this operation. 但是,即使执行此操作,arr仍然相同。 How to fix this? 如何解决这个问题?

Maybe you want filled . 也许你想filled I'll illustrate: 我将说明:

In [702]: x=np.arange(10)    
In [703]: xm=np.ma.masked_greater(x,5)

In [704]: xm
Out[704]: 
masked_array(data = [0 1 2 3 4 5 -- -- -- --],
             mask = [False False False False False False  True  True  True  True],
       fill_value = 999999)

In [705]: xm.filled(10)
Out[705]: array([ 0,  1,  2,  3,  4,  5, 10, 10, 10, 10])

In this case filled replaces all masked values with a fill value. 在这种情况下, filled将所有掩码值替换为填充值。 Without an argument it would use the fill_value . 如果没有参数,它将使用fill_value

np.ma uses this approach to perform many of its calculations. np.ma使用这种方法来执行许多计算。 For example its sum is the same as if I filled all masked values with 0. prod would replace them with 1. 例如,它的sum与我用0填充所有掩码值时相同prod会将它们替换为1。

In [707]: xm.sum()
Out[707]: 15
In [709]: xm.filled(0).sum()
Out[709]: 15

The result of filled is a regular array, since all masked values have been replaced with something 'normal'. 结果filled是一个普通的数组,因为所有的屏蔽值已被替换的东西“正常”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM