简体   繁体   中英

Substituting values in a numpy masked array

I'm trying to substitute some values in a numpy masked array , but my mask is being dropped:

import numpy as np
a = np.ma.array([1, 2, 3, -1, 5], mask=[0, 0, 0, 1, 0])
a[a < 2] = 999

The result is:

masked_array(data = [999 2 3 999 5],
mask = [False False False False False],
fill_value = 999999)

But what I want is:

masked_array(data = [999 2 3 -- 5],
mask = [False False False  True False],
fill_value = 999999)

What am I doing wrong? I'm using Python 2.7 and numpy 1.7.1 on Ubuntu 13.10

I think you are not doing the substitution correctly, try this:

>>> import numpy as np
>>> a = np.ma.array([1, 2, 3, -1, 5], mask=[0, 0, 0, 1, 0])
>>> a.data[a < 2] = 999
>>> a
 masked_array(data = [999 2 3 -- 5],
         mask = [False False False  True False],
   fill_value = 999999)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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