3

I have this arrays:

arr = np.array([[[ -1.,  -1.,  -1.,  0.,   0.,   0.],
                [ 0.1,  0.1,  0.1,  2.,   3.,   4.]], # <-- this one

               [[ -1.,  -1.,  -1.,  0.,   0.,  -1.],
                [ 0.1,  0.1,  0.1, 16.,  17.,  0.1]], # <-- and this one

               [[ -1.,  -1.,  -1.,  0.,   0.,   0.],
                [ 0.1,  0.1,  0.1,  4.,   5.,   6.]], # <-- and this one

               [[  0.,   0.,   0., -1.,   0.,   0.],
                [  1.,   2.,   3., 0.1,   1.,   2.]], # <-- and this one

               [[ -1.,  -1.,   0.,  0.,   0.,   0.],
                [ 0.1,  0.1,   1.,  9.,  10.,  11.]]]) # <-- and the last one

I want to extract the 2nd array in each array, and the result should be as follows:

res = [[ 0.1,  0.1,  0.1,  2.,   3.,   4.],
       [ 0.1,  0.1,  0.1, 16.,  17.,  0.1],
       [ 0.1,  0.1,  0.1,  4.,   5.,   6.],
       [  1.,   2.,   3., 0.1,   1.,   2.],
       [ 0.1,  0.1,   1.,  9.,  10.,  11.]]

I want to get the res in one line of code, I tried this but it didn't work

arr[:][1] # select the element 1 in each array
# I got
array([[ -1. ,  -1. ,  -1. ,   0. ,   0. ,  -1. ],
       [  0.1,   0.1,   0.1,  16. ,  17. ,   0.1]])

Can anyone explain why?

The only solution that I found is to explicitly indicate each index (arr[0][1]...), which I didn't like.

1
  • res = [x[1] for x in arr]? Commented Apr 26, 2017 at 12:42

2 Answers 2

7

That's a 3D array and you are trying to select the second element of the second axis and extracting all elements along the rest of the axes. So, its as simple as -

arr[:,1,:]

We can skip listing the : for the trailing axes, so it further simplifies to -

arr[:,1]

Sample run -

In [360]: arr
Out[360]: 
array([[[ -1. ,  -1. ,  -1. ,   0. ,   0. ,   0. ],
        [  0.1,   0.1,   0.1,   2. ,   3. ,   4. ]],

       [[ -1. ,  -1. ,  -1. ,   0. ,   0. ,  -1. ],
        [  0.1,   0.1,   0.1,  16. ,  17. ,   0.1]],

       [[ -1. ,  -1. ,  -1. ,   0. ,   0. ,   0. ],
        [  0.1,   0.1,   0.1,   4. ,   5. ,   6. ]],

       [[  0. ,   0. ,   0. ,  -1. ,   0. ,   0. ],
        [  1. ,   2. ,   3. ,   0.1,   1. ,   2. ]],

       [[ -1. ,  -1. ,   0. ,   0. ,   0. ,   0. ],
        [  0.1,   0.1,   1. ,   9. ,  10. ,  11. ]]])

In [361]: arr[:,1]
Out[361]: 
array([[  0.1,   0.1,   0.1,   2. ,   3. ,   4. ],
       [  0.1,   0.1,   0.1,  16. ,  17. ,   0.1],
       [  0.1,   0.1,   0.1,   4. ,   5. ,   6. ],
       [  1. ,   2. ,   3. ,   0.1,   1. ,   2. ],
       [  0.1,   0.1,   1. ,   9. ,  10. ,  11. ]])
Sign up to request clarification or add additional context in comments.

1 Comment

Congratulations!
2

I don't know anything about numpy, so there might be an easier way of doing it. But a simple list comprehension would work:

[a[1] for a in arr]

Comments

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.