kyle chan
kyle chan

Reputation: 361

Numpy array indexing with indices

I wrote the following:

arr3=np.array([[[1,2,3],[1,2,3],[1,2,3],[1,2,3]],[[2,2,3],[4,2,3],[4,2,2],[2,2,2]],[[1,1,1],[1,1,1],[1,1,1],[1,1,1]]])

As I expected,
arr3[0:3,1] should return the same result as
arr3[0:3][1]:array([[2, 2, 3],[4, 2, 3],[4, 2, 2],[2, 2, 2]])

But it returns:array([[1, 2, 3],[4, 2, 3],[1, 1, 1]]).

BTW, I am using python3 in Jupyter notebook

Upvotes: 3

Views: 74

Answers (2)

Neil
Neil

Reputation: 14313

Let's look at our multidimensional numpy array:

import numpy as np
arr3=np.array([
    [
        [1,2,3],[1,2,3],[1,2,3],[1,2,3]
    ],[
        [2,2,3],[4,2,3],[4,2,2],[2,2,2]
    ],[
        [1,1,1],[1,1,1],[1,1,1],[1,1,1]
    ]
])
print(arr3[0:3,1])

That returns:

[[1 2 3]
 [4 2 3]
 [1 1 1]]

Which makes sense because we are fetching row numbers 1 through 3 and we are grabbing only the first column.

However, arr3[0:3][1] returns the array from row 0 to row 3 and then selects the second row (or row index 1).

Observe:

print(arr3[0:3])

Returns:

[[[1 2 3]
  [1 2 3]
  [1 2 3]
  [1 2 3]]

 [[2 2 3]
  [4 2 3]
  [4 2 2]
  [2 2 2]]

 [[1 1 1]
  [1 1 1]
  [1 1 1]
  [1 1 1]]] 

It returns the a new array (which happens to be the same as our current array because we just asked for all rows in the array). Then we ask for the second row:

print(arr3[0:3][1])

Returns:

[[2 2 3]
 [4 2 3]
 [4 2 2]
 [2 2 2]]

Upvotes: 1

Joe Iddon
Joe Iddon

Reputation: 20414

When doing arr3[0:3,1], you are taking element from 0:3 in the first axis and then for each of those, taking the first element.

This gives a different result to taking the 0:3 in the first axis with arr3[0:3] and then taking the first array from this axis.

So in this case, the 0:3 part does nothing in either case as the array's shape is (3, 4, 3) so taking the first 3 just gives you back the same array. This does absolutely nothing in the second case, but in the first case, it does serve as essentially a place holder so that you can access the second axis, but for that you should just use a colon so: [:, some_index].

See how its the same array?

>>> arr3[0:3]
array([[[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],

       [[2, 2, 3],
        [4, 2, 3],
        [4, 2, 2],
        [2, 2, 2]],

       [[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]]])

But then when you do arr3[:, 1] you are taking the second element from the second axis of the array so that will give you:

array([[1, 2, 3],
       [4, 2, 3],
       [1, 1, 1]])

whereas in the other case, you are taking the second element from the first axis of the array` so:

array([[2, 2, 3],
       [4, 2, 3],
       [4, 2, 2],
       [2, 2, 2]])

To read further about numpy indexing, take a look at this page on scipy.

Take note of this specific description which applies directly to your problem:

When there is at least one slice (:), ellipsis (...) or np.newaxis in the index (or the array has more dimensions than there are advanced indexes), then the behaviour can be more complicated. It is like concatenating the indexing result for each advanced index element

Upvotes: 3

Related Questions