3

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

Reti43
9,8273 gold badges30 silver badges48 bronze badges
asked Nov 18, 2017 at 7:56

2 Answers 2

3

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

answered Nov 18, 2017 at 8:05

3 Comments

@kylechan Please accept (grey tick next to answer) and upvote if you haven't already
done! my first time to use stackoverflow. thanks for being patient
@kylechan No problem, this was a well worded question for a new user :)
1

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]]
answered Nov 18, 2017 at 8:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.