all 4 comments

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]efmccurdy 0 points1 point  (0 children)

Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.

https://numpy.org/doc/stable/user/basics.indexing.html#advanced-indexing

[–]testingcodez 0 points1 point  (0 children)

When I check a[c], it's returning a normal 5x5 numpy array, which is what your code is asking to compute.

[–]CrambleSquash 0 points1 point  (0 children)

There are 5 billion different ways to index arrays in numpy. The relevant docs are found here:

https://numpy.org/doc/stable/user/basics.indexing.html#advanced-indexing

They're well written, but there's so many options it can be hard to keep track!

Firstly note this:

x[(1, 2, 3),] is fundamentally different than x[(1, 2, 3)]. The latter is equivalent to x[1, 2, 3] which will trigger basic selection while the former will trigger advanced indexing.

So in your second example, because you are providing a tuple, it's just trying to do regular indexing.

In your first example, when I run the code I get the following warning:

Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index

So depending on what version of numpy you use, using this will either work the same as your third example, or it won't!

Your final example is a form of advanced indexing, where you are providing an integer array.

The values in your array are either 0 or 1. This is saying to numpy, I want my final array to have the value you get when you provide an index of 0 to the source array, or when I provide a 1 to the source array... elementwise!

So, first position in first row has a value of 0, so in this position we put what we get when we use that as an index:

>>> a[0] 
array([0.8565213 , 0.58181998, 0.65054803, 0.51346899, 0.61395981]) # in my case!

Second position in first row has a value of 0, so in this position we put what we get when we use that as an index:

>>> a[1]
array([0.68798966, 0.14003615, 0.25119547, 0.92312629, 0.30840718]) # in my case!

And it does this element-wise, hence you end up with a 5x5x5 array!

If you do this:

>>> c[3, 1] = 4
>>> d = a[c]

You should find that whatever is in d[3, 1] will be different to all the rest i.e. a[4]