0

I have the following code :

import numpy as np
a = np.array([[ 1,  2,  3,  4,  5,  6],
              [ 7,  8,  9, 10, 11, 12]])
a[:, 2:3] #get [[[3],[9]]
a[:,[2]] # get [[3],[9]]
a[:, 2, None] # get [[3],[9]]
a[:, 2] #get [3, 9]

Why a[:, 2] get [3, 9] ?

p.s. saw a few posts talking about slicing 1 column out of a 2D array (as in the example above) get a 1D array, but no explanation on why.

p.p.s This question is not about how-to do it, but why so.

ngBeginner
  • 137
  • 8
  • @jpp, the thread you posted is about how-to, I already know the how-to (as noted in the p.s.) but asking about why. The 2 threads are not the same question. – ngBeginner Dec 01 '18 at 21:53
  • @jpp, Feel free to provide a better answer, I already clearly asked why several times in my post. I can change my acceptance. Regardless, linking the 2 threads as duplicates is incorrect as the 2 aren't talking about the same thing. – ngBeginner Dec 01 '18 at 21:58
  • The answer [here](https://stackoverflow.com/a/2640168/9209546) gives the true answer... it's [documented behaviour](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer-array-indexing). – jpp Dec 01 '18 at 22:03
  • I don't think so, @jpp. It is talking about `how-to` as well. Why don't you challenge @ShlomiF about his statement `They're both "mathematically" one-dimensional, but they have different numpy shapes.` you don't like, instead of forcing me to accept answers I don't find adequately address my question ? – ngBeginner Dec 01 '18 at 22:10
  • This is a silly argument. The person _asking_ the question defines what he meant. Not someone with a "better notion" regarding his meaning. And there's nothing misleading in my answer, when addressing the term 1D, as @ngBeginner used in his question. The premise of the question was partially based on the intuitive side of things, and the answer was addressing that as well. Have a good day! – ShlomiF Dec 01 '18 at 22:30

2 Answers2

1

Numpy is dropping the singleton dimensions in the second example. You can preserve the shape and get the equivalent of the first example with the following, if desired.

a[:, [2]] # get [[3],[9]]
Matt
  • 1,342
  • 5
  • 12
-1

I think you're using the term 1D array in a slightly ill-defined way.
The one option returns an array of shape (2, 1), and the other returns a list-like array of shape (2, ).
They're both "mathematically" one-dimensional, but they have different numpy shapes.
So what you're aiming for is to get a more matrix-like array, of shape (2, 1), and that's done by slicing the wanted indexes over all dimensions, as opposed to choosing a specific index.

Here's a more intuitive case to consider, taking things to an extreme by slicing or choosing over both dimensions:

import numpy as np
a = np.array([[ 1,  2,  3,  4,  5,  6],
             [ 7,  8,  9, 10, 11, 12]])

specific_index_value = a[0, 0]
print(specific_index_value)
print(type(specific_index_value))
print(str(specific_index_value) + ' is a scalar, not a 1X1 matrix')
>> 1
>> <class 'numpy.int32'>
>> 1 is a scalar, not a 1X1 matrix

sliced_index_value = a[:1, :1]
print(sliced_index_value)
print(type(sliced_index_value))
print(str(sliced_index_value) + ' is a matrix, with shape {}'.format(sliced_index_value.shape))

>> [[1]]
>> <class 'numpy.ndarray'>
>> [[1]] is a matrix, with shape (1, 1)

Does that make sense? Good luck!

ShlomiF
  • 2,686
  • 1
  • 14
  • 19
  • Your explanation helps some, but not the example. Sorry, you are dealing with a green dumbo. :( Thanks anyway. – ngBeginner Dec 01 '18 at 20:36
  • I'll clarify the example – ShlomiF Dec 01 '18 at 21:05
  • another poster (see above) doesn't agree with a statement in your post. Instead of asking you about it, he wants me to accept other answers which I don't find sufficiently address my question. Geeze ! never know asking a simple question on here can be such a hassle. – ngBeginner Dec 01 '18 at 22:24
  • You're free to accept whatever addresses the reason you originally asked. I'm not going to fight anyone about it. And sometimes hand-waving is the best explanation, even if not the most "precise". Good luck! – ShlomiF Dec 01 '18 at 22:27
  • This answer doesn't explain *why* (however you interpret *why*). [This answer does](https://stackoverflow.com/a/2640168/9209546). – jpp Dec 01 '18 at 23:05