根据两列的值选择numpy ndarray中的行 [英] selecting rows in numpy ndarray based on the value of two columns

查看:388
本文介绍了根据两列的值选择numpy ndarray中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的np.ndarray (3600000,3)HUEVALUE和关联的CLASS号.我想使用此数组查找每对HUEVALUE对应的Class号.我是Python的初学者,很难做到这一点.你知道这样做的方法吗?

I have a big np.ndarray (3600000,3), the HUE, the VALUE, and an associated CLASS number. For each pairs of HUE and VALUE I would like to find, using this array the corresponding Class number. I'm a very beginner in Python and have a hard time doing it. Do you know a way to do it?

提前谢谢!

推荐答案

我假设您的数组看起来像:

I assume your array looks like:

       |(HUE)(VALUE)(CLASS)
row/col|   0     1     2
-------+-----------------
0      |   0     1     2
1      |   3     4     5
2      |   6     7     8
.      |   .     .     .
.      |   .     .     .
3599999|   .     .     .

这是示例代码.为简单起见,我将大小为3600000更改为5.

And here is the sample code. For simplicity I changed the size 3600000 to 5.

a = np.array(xrange(5 * 3))
a.shape = (5, 3)

现在的数组a看起来像这样:

Now array a look like this:

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])

如果要与HUE=9排在一起,请执行以下操作:

If you want row with HUE=9, do like this:

a[np.where(a[:,0] == 9)]
#array([[ 9, 10, 11]])

如果要与VALUE=4排在一起,请执行以下操作:

If you want row with VALUE=4, do like this:

a[np.where(a[:,1] == 4)]
#array([[3, 4, 5]])

如果要使用HUE=0VALUE=1行,请执行以下操作:

If you want row with HUE=0 and VALUE=1, do like this:

a[np.where((a[:,0] == 0) * (a[:,1] == 1))]
#array([[0, 1, 2]])

这篇关于根据两列的值选择numpy ndarray中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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