Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将掩码从一个数组应用到另一个数组?_Python_Numpy - Fatal编程技术网

Python 如何将掩码从一个数组应用到另一个数组?

Python 如何将掩码从一个数组应用到另一个数组?,python,numpy,Python,Numpy,现在我已经读了好几遍蒙面数组文档,到处都找遍了,觉得自己非常愚蠢。我不知道如何将面具从一个阵列应用到另一个阵列 例如: 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 --

现在我已经读了好几遍蒙面数组文档,到处都找遍了,觉得自己非常愚蠢。我不知道如何将面具从一个阵列应用到另一个阵列

例如:

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]
所以这很好用。。。。但要绘制这个y轴,我需要一个匹配的x轴。如何将遮罩从y阵列应用到x阵列?这样做是有道理的,但会产生垃圾:

new_x = x[m.mask].copy()
new_x
array([5])
那么,这到底是怎么做到的(请注意,新的x阵列需要是一个新的阵列)

编辑:

看来有一种方法是这样的:

>>> 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]
但那真是一团糟!我正试图找到一个像IDL一样优雅的解决方案…

为什么不简单呢

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_)
代码是针对Python2.x的

此外,正如joris所建议的,这将完成以下工作:
new_x=x[~m.mask].copy()
给出一个数组

>>> new_x
array([1, 2, 4])

我有一个类似的问题,但涉及加载更多的屏蔽命令和更多的数组来应用它们。我的解决方案是在一个数组上执行所有屏蔽,然后在
mask\u where
命令中使用最终屏蔽的数组作为条件

例如:

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

很好的一点是,您现在可以将此掩码应用于更多阵列,而无需对每个阵列执行掩码过程。

这可能不是OP想要知道的100%, 但这是我一直在使用的一段可爱的小代码- 如果要以相同的方式屏蔽多个数组,可以使用此通用函数一次屏蔽动态数量的numpy数组:

def apply_mask_to_all(mask, *arrays):
assert all([arr.shape == mask.shape for arr in arrays]), "All Arrays need to have the same shape as the mask"
return tuple([arr[mask] for arr in arrays])
请参见此示例用法:

    # init 4 equally shaped arrays
x1 = np.random.rand(3,4)
x2 = np.random.rand(3,4)
x3 = np.random.rand(3,4)
x4 = np.random.rand(3,4)

# create a mask
mask = x1 > 0.8

# apply the mask to all arrays at once
x1, x2, x3, x4 = apply_mask_to_all(m, x1, x2, x3, x4)

你不能像
plot(x,m)
那样绘图而不创建新的\ux吗?它是
new\ux=x[~m.mask].copy()
。请注意
~
,因为掩码在值被掩码的地方是真的。请注意,在
new\u x=x[~m.mask].copy()中不需要
.copy()
。使用布尔数组进行索引总是会产生一个副本,因此这可以是
new\u x=x[~m.mask]
。您真的需要在这里使用mask吗?布尔数组上的索引将为您提供所需的内容
new_y=y[y<5]
new_x=x[y<5]
。如果要重用它,可以将其分配给变量:
mask=y<5;m=y[遮罩];新建_x=x[掩码]
。这样就避免了使用掩码数组,并使其更简单、更直观。谢谢,这也是一个好东西。很明显,我是Python新手,正在经历磨难……这可能有助于你的答案避免使用“简单”、“公正”等词。如果
x
y
有不同的维数,而我只想在轴0上遮罩会怎么样?