我有一个像这个numpy数组的数组
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]]
我需要2个单独的数组dd[:,1] ==1
和dd[:,1] ==2
这些数组就是我追求的目标:
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]]
我试过np.where
确实有效
答案 0 :(得分:6)
您可以使用numpy花式索引:
[~/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]])
或者你可以使用列表理解:
[~/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]])
编辑
如何在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