1

For the array:

import numpy as np
arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> arr2d
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> arr2d[2].shape
(3,)
>>> arr2d[2:,:].shape
(1, 3)

Why do I get different shapes when both statements return the 3rd row? and shouldn't the result be (1,3) in both cases since we are returning a single row with 3 columns?

2 Answers 2

5

Why do I get different shapes when both statements return the 3rd row?

Because with the first operation you are indexing the rows, and selecting just ONE element, which -as mentioned in the single-element indexing paragraph of a multidimensional array- returns an array with a lower dimension (a 1D array).

In the 2nd example, you are using a slice as evident by the colon. Slicing operations do not reduce the dimensions of an array. This is also logical, because imagine the array would not have 3 but 4 rows. Then arr2d[2:,:].shape would be (2,3). The developers of numpy made slicing operations consistent and therefor they (slices) never reduce the number of dimensions of the array.

and shouldn't the result be (1,3) in both cases since we are returning a single row with 3 columns?

No, just because of the previous reasons.

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

5 Comments

thanks, how do you interpret arr2d[2].shape result? Does (3,) mean an array with 3 values? and why have a ',' then?
Indeed, (3,) is the shape of a 1-dimensional array with just 3 elements. The reason it is being output like that, is to be consistent: any time someone asks for the shape of an n-dimension array, we know we will get back a tuple, even if that tuple only contains one element.
@BiRico, you're right, it was misleading, because it had nothing to do with fancy indexing. I've removed those two words from my answer and added the correct link.
@BiRico Thank you for the suggestion to improve and vote of confidence. ;-) By the way, I do think your analogy with the list of lists is helpful too, I wish you didn't delete it.
Sorry I took it down, the analogy is this. If you have a list L = ['a', 'b', 'c'], L[2] == 'c'. Indexing the list give one of the items in the list. Slicing on the other had produces a new list which is a sub-list of the original, for example L[2:] == ['c']. The result of slicing a list is always a list, even it has len 0 or 1. 2d numpy arrays behave a lot like lists of lists.
1

When doing arr2d[2], you are taking a row out of the array;

While when doing arr2d[2:, :], you are taking a subset of rows out of the array ('slicing'), in this case being the rows starting from the 3rd to the end, which is only the 3rd, but it didn't change that you are taking a subset, not an element.

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.