2

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 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

the first example (fancy indexing) is about twice as fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.