Python 如何将一个数组中的掩码应用到另一个数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16495298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to apply a mask from one array to another array?
提问by Balthasar
I've read the masked array documentation several times now, searched everywhere and feel thoroughly stupid. I can't figure out for the life in me how to apply a mask from one array to another.
我已经多次阅读掩码数组文档,到处搜索并感到非常愚蠢。我无法弄清楚如何将蒙版从一个阵列应用到另一个阵列。
Example:
例子:
import numpy as np
y = np.array([2,1,5,2]) # y axis
x = np.array([1,2,3,4]) # x axis
m = np.ma.masked_where(y>2, y) # filter out values larger than 5
print m
[2 1 -- 2]
print np.ma.compressed(m)
[2 1 2]
So this works fine.... but to plot this y axis, I need a matching x axis. How do I apply the mask from the y array to the x array? Something like this would make sense, but produces rubbish:
所以这很好用......但是要绘制这个 y 轴,我需要一个匹配的 x 轴。如何将 y 数组中的掩码应用于 x 数组?像这样的事情是有道理的,但会产生垃圾:
new_x = x[m.mask].copy()
new_x
array([5])
So, how on earth is that done (note the new x array needs to be a new array).
那么,到底是怎么做的(注意新的 x 数组需要是一个新的数组)。
Edit:
编辑:
Well, it seems one way to do this works like this:
好吧,似乎一种方法是这样工作的:
>>> import numpy as np
>>> x = np.array([1,2,3,4])
>>> y = np.array([2,1,5,2])
>>> m = np.ma.masked_where(y>2, y)
>>> new_x = np.ma.masked_array(x, m.mask)
>>> print np.ma.compressed(new_x)
[1 2 4]
But that's incredibly messy! I'm trying to find a solution as elegant as IDL...
但这实在是太乱了!我试图找到一个像 IDL 一样优雅的解决方案......
采纳答案by kiriloff
Why not simply
为什么不简单
import numpy as np
y = np.array([2,1,5,2]) # y axis
x = np.array([1,2,3,4]) # x axis
m = np.ma.masked_where(y>2, y) # filter out values larger than 5
print list(m)
print np.ma.compressed(m)
# mask x the same way
m_ = np.ma.masked_where(y>2, x) # filter out values larger than 5
# print here the list
print list(m_)
print np.ma.compressed(m_)
code is for Python 2.x
代码适用于 Python 2.x
Also, as proposed by joris, this do the work new_x = x[~m.mask].copy()giving an array
另外,正如 joris 所提出的,这可以完成new_x = x[~m.mask].copy()提供数组的工作
>>> new_x
array([1, 2, 4])
回答by red_tiger
I had a similar issue, but involving loads more masking commands and more arrays to apply them. My solution is that I do all the masking on one array and then use the finally masked array as the condition in the mask_wherecommand.
我有一个类似的问题,但涉及加载更多的屏蔽命令和更多的数组来应用它们。我的解决方案是我对一个数组进行所有屏蔽,然后使用最终屏蔽的数组作为mask_where命令中的条件。
For example:
例如:
y = np.array([2,1,5,2]) # y axis
x = np.array([1,2,3,4]) # x axis
m = np.ma.masked_where(y>5, y) # filter out values larger than 5
new_x = np.ma.masked_where(np.ma.getmask(m), x) # applies the mask of m on x
The nice thing is you can now apply this mask to many more arrays without going through the masking process for each of them.
好消息是您现在可以将此掩码应用于更多数组,而无需为每个数组都经过掩码过程。

