试图根据一列中的值遮罩2D numpy数组 [英] trying to mask 2D numpy arrays based on values in one column

查看:45
本文介绍了试图根据一列中的值遮罩2D numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

[[    6.           105.             2.             8.09841881]
[    6.           105.             4.             9.34220351]
[    6.           105.             6.             9.97663435]
[    6.          1001.             2.             9.57108242]
[    6.          1001.             4.            12.22355794]
[    6.          1001.             6.            13.57295942]
[   12.          1001.             2.            12.37474466]
[   12.          1001.             4.            17.45334004]
[   12.          1001.             6.            19.88499289]
[   18.          1007.             2.            16.09076561]
[   18.          1007.             4.            23.43742275]
[   18.          1007.             6.            27.73041646]]

我试图仅提取第一个元素为6个通孔的行

And I have tried to extract only the rows with the first element being a six via

print ma.MaskedArray(a, mask=(np.ones_like(a)*(a[:,0]==6.0)).T)

我从问题"中得到的

根据一列中的值屏蔽2D numpy数组".但是,我得到

which I got from the question " mask a 2D numpy array based on values in one column". However, I get

File "./Prova.py", line 170, in <module>
print ma.MaskedArray(a, mask=(np.ones_like(a)*(a[:,0]==6.0)).T)
ValueError: operands could not be broadcast together with shapes (12,4) (12)

您是否知道为什么这种方法不起作用?

do you have a clue of why this doesn't work?

这个问题可能很愚蠢,但是请耐心等待,因为我刚刚开始编写python. :-)

The question might be stupid, but please bear with me since I just started programming python. :-)

推荐答案

设置一些测试数据以进行处理:

Setting up some test data to work on:

>>> a = np.arange(12*4).reshape((12,4))

首先,我们为掩码数组分配"空间:

First, we "allocate" space for our mask array:

>>> mask = np.empty(a.shape,dtype=bool)

现在我们不能从a == 6的第一列直接分配它,因为它们的形状不正确:

Now we can't assign into it from the first column of a == 6 directly because they're not the proper shape:

>>> mask[:,:] = a[:,0] == 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (12,4) (12) 

但是我们可以通过简单地插入一个newaxis使其成为二维数组来广播a的第一列,直到其正确的形状:

But we can broadcast our first column of a up to the correct shape by simply inserting a newaxis so that it becomes a 2-D array:

>>> mask[:,:] = (a[:,0] == 6)[:,np.newaxis]

我们可以看到,我们的遮罩现在是正确的.

As we can see, our mask is now correct.

>>> mask
array([[ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False],
       [False, False, False, False]], dtype=bool)

现在,我们只需制作我们的遮罩数组,然后坐下来即可享受:).

Now we just make our masked array and sit back and enjoy :).

>>> ma.MaskedArray(a,mask=mask)
masked_array(data =
 [[-- -- -- --]
 [-- -- -- --]
 [-- -- -- --]
 [-- -- -- --]
 [-- -- -- --]
 [20 21 22 23]
 [24 25 26 27]
 [28 29 30 31]
 [32 33 34 35]
 [36 37 38 39]
 [40 41 42 43]
 [44 45 46 47]],
             mask =
 [[ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]],
       fill_value = 999999)

这篇关于试图根据一列中的值遮罩2D numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆