2017年5月6日 星期六

[ Python 常見問題 ] Numpy - Selecting rows in numpy ndarray based on the value of two columns

Source From Here 
Question 
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. 

How-To 
I assume your array looks like: 

And here is the sample code. For simplicity I changed the size 3600000 to 5: 
>>> import numpy as np
>>> a = np.array(xrange(5 * 3))
>>> a.shape
(15,)
>>> a = a.reshape([5,3])
>>> a
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])

>>> a.shape
(5, 3)

If you want row with HUE=9, do like this: 
>>> np.where(a[:,0] == 9)
(array([3]),)
>>> a[np.where(a[:,0] == 9)]
array([[ 9, 10, 11]])

If you want row with VALUE=4, do like this: 
>>> a[np.where(a[:,1] == 4)]
array([[3, 4, 5]])

If you want row with HUE=0 and VALUE=1, do like this: 
>>> a[:,0] == 0
array([ True, False, False, False, False], dtype=bool)
>>> a[:,1] == 1
array([ True, False, False, False, False], dtype=bool)
>>> (a[:,0] == 0) * (a[:,1] == 1)
array([ True, False, False, False, False], dtype=bool)
>>> a[np.where((a[:,0] == 0) * (a[:,1] == 1))]
array([[0, 1, 2]])

For more, please refer to Numpy Indexing routines.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...