1

What is the explanation of the following behavior:

import numpy as np    
arr = np.zeros((3, 3))
li = [1,2]
print('output1:', arr[:, li].shape)
print('output2:', arr[:][li].shape)

>>output1: (3, 2)
>>output2: (2, 3)

I would expect output2 to be equal to output1.

1
  • a[:] only copies the a. Commented Feb 2, 2021 at 11:14

3 Answers 3

1

Let's use a different array where it's easier to see the difference:

>>> arr = np.arange(9).reshape(3, 3)
>>> arr
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

The first case arr[:, li] will select all elements from the first dimension (in this case all the rows), then index the array with [1, 2], which means just leaving out the first column:

array([[1, 2],
       [4, 5],
       [7, 8]])

Hence, the shape of this is (3, 2).

The other case arr[:] will copy the original array, so it doesn't change the shape, therefore it's equvivalent to arr[li], hence the output shape is (2, 3). In general you should avoid double indexing an array, because that might create views twice, which is inefficient.

Sign up to request clarification or add additional context in comments.

Comments

1

You are getting the the correct output.

In first line

print('output1:', arr[:, li].shape)

You are printing 2nd and 3rd element of each subarray within arr, thus getting 3 elements each containing 2 values.

In second line

print('output2:', arr[:][li].shape)

You are selecting first the whole array, then from the whole array you select 2nd and 3rd element (each containing 3 elements themselves), thus getting 2 elements each containing 3 values.

Comments

1

The difference can be seen if you examine this code -

import numpy as np    
arr = np.arange(9).reshape(3, 3)
li = [1,2]
print('output1:', arr[:, li])
print('output2:', arr[:][li])

This gives -

[[1 2]
 [4 5]
 [7 8]]

and

[[3 4 5]
 [6 7 8]]

When you do arr[:, [1, 2]], what you are saying that you want to take all the rows of the array (: specifies this) and, from that, take column [1, 2].

On the other hand, when you do arr[:], you are referring to the full array first. Out of which you are again taking the first two rows.

Essentially, in the second case, [1 2] is referring to the row axis of the original array while in the first case, it's referring to the column.

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.