替换 numpy 掩码数组中的值 [英] Substituting values in a numpy masked array
本文介绍了替换 numpy 掩码数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试替换 numpy
masked array
中的一些值,但是我的掩码被丢弃了:
将 numpy 导入为 npa = np.ma.array([1, 2, 3, -1, 5], 掩码=[0, 0, 0, 1, 0])[a <2] = 999
结果是:
masked_array(data = [999 2 3 999 5],掩码 = [假假假假假假],填充值 = 999999)
但我想要的是:
masked_array(data = [999 2 3 -- 5],掩码 = [假假假假真假],填充值 = 999999)
我做错了什么?我在 Ubuntu 13.10 上使用 Python 2.7 和 numpy 1.7.1
解决方案
我认为你没有正确地进行替换,试试这个:
<预><代码>>>>将 numpy 导入为 np>>>a = np.ma.array([1, 2, 3, -1, 5], 掩码=[0, 0, 0, 1, 0])>>>a.数据[a <2] = 999>>>一种masked_array(data = [999 2 3 -- 5],掩码 = [假假假假真假],填充值 = 999999)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)
这篇关于替换 numpy 掩码数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文