面膜基于索引numpy的数组 [英] Mask numpy array based on index
问题描述
我如何掩盖根据实际的指数值的数组?
How do I mask an array based on the actual index values?
也就是说,如果我有一个10×10×30矩阵,我想屏蔽阵列,当第一和第二索引相等彼此
That is, if I have a 10 x 10 x 30 matrix and I want to mask the array when the first and second index equal each other.
例如, [1,1,:]
应该被屏蔽,因为1和1相等对方,但 [1,2,:]
不应该因为他们没有。
For example, [1, 1 , :]
should be masked because 1 and 1 equal each other but [1, 2, :]
should not because they do not.
我的第三个层面,因为它类似于我目前存在的问题和可能的事情复杂化只要求这一点。但我的主要问题是,如何根据指标的值,以掩盖阵列?
I'm only asking this with the third dimension because it resembles my current problem and may complicate things. But my main question is, how to mask arrays based on the value of the indices?
推荐答案
在一般情况下,进入指数的值,可以使用 np.meshgrid
:
In general, to access the value of the indices, you can use np.meshgrid
:
i, j, k = np.meshgrid(*map(np.arange, m.shape), indexing='ij')
m.mask = (i == j)
这种方法的优点是,它适用于任意的布尔函数对 I
,Ĵ
和<$ ç$ C> K 。它比使用身份
特殊情况要慢一点。
The advantage of this method is that it works for arbitrary boolean functions on i
, j
, and k
. It is a bit slower than the use of the identity
special case.
In [56]: %%timeit
....: i, j, k = np.meshgrid(*map(np.arange, m.shape), indexing='ij')
....: i == j
10000 loops, best of 3: 96.8 µs per loop
由于@Jaime指出, meshgrid
支持稀疏
选项,它并不做这么多的重复,但需要多一点的关怀在某些情况下,因为他们没有播出。这将节省内存和速度的东西了一点。例如,
As @Jaime points out, meshgrid
supports a sparse
option, which doesn't do so much duplication, but requires a bit more care in some cases because they don't broadcast. It will save memory and speed things up a little. For example,
In [77]: x = np.arange(5)
In [78]: np.meshgrid(x, x)
Out[78]:
[array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]),
array([[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]])]
In [79]: np.meshgrid(x, x, sparse=True)
Out[79]:
[array([[0, 1, 2, 3, 4]]),
array([[0],
[1],
[2],
[3],
[4]])]
所以,你可以为他说用稀疏
版本,但你必须强制广播这样:
So, you can use the sparse
version as he says, but you must force the broadcasting as such:
i, j, k = np.meshgrid(*map(np.arange, m.shape), indexing='ij', sparse=True)
m.mask = np.repeat(i==j, k.size, axis=2)
和加速比:
In [84]: %%timeit
....: i, j, k = np.meshgrid(*map(np.arange, m.shape), indexing='ij', sparse=True)
....: np.repeat(i==j, k.size, axis=2)
10000 loops, best of 3: 73.9 µs per loop
这篇关于面膜基于索引numpy的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!