How slice Numpy array by column value

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I have an array like this numpy array

 dd =[[0.567 2 0.611]       [0.469 1 0.479]       [0.220 2 0.269]       [0.480 1 0.508]       [0.324 1 0.324]] 

I need 2 seperate array dd[:,1] ==1 and dd[:,1] ==2

These array are what I am after:

 na =[[0.469 1 0.479]       [0.480 1 0.508]       [0.324 1 0.324]]   na2 =[[0.567 2 0.611]        [0.220 2 0.269]] 

I have tried np.where did really work

回答1:

You could use numpy fancy indexing:

[~/repo/py] |32>dd[dd[:,1] == 1] [32]  array([[ 0.469,  1.   ,  0.479],        [ 0.48 ,  1.   ,  0.508],        [ 0.324,  1.   ,  0.324]])  [~/repo/py] |33>dd[dd[:,1] == 2] [33]  array([[ 0.567,  2.   ,  0.611],        [ 0.22 ,  2.   ,  0.269]]) 

Alternatively you could use a list comprehension:

[~/repo/py] |21>np.array([row for row in dd if row[1] == 1]) [21]  array([[ 0.469,  1.   ,  0.479],        [ 0.48 ,  1.   ,  0.508],        [ 0.324,  1.   ,  0.324]])  [~/repo/py] |22>np.array([row for row in dd if row[1] == 2]) [22]  array([[ 0.567,  2.   ,  0.611],        [ 0.22 ,  2.   ,  0.269]]) 

edit:

how to time these things in ipython:

[~/repo/py] |36>timeit dd[dd[:,1] == 1] 100000 loops, best of 3: 6 us per loop  [~/repo/py] |37>timeit np.array([row for row in dd if row[1] == 1]) 100000 loops, best of 3: 11.5 us per loop 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!