Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Indexing style leads to different output #16552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Faaziy opened this issue Jun 9, 2020 · 6 comments
Closed

Indexing style leads to different output #16552

Faaziy opened this issue Jun 9, 2020 · 6 comments
Labels
04 - Documentation 33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued

Comments

@Faaziy
Copy link

Faaziy commented Jun 9, 2020

import numpy as np
a = np.array( [[1,2,3] , [4,5,]] )
x = np.array( [1,0] )
y = np.array( [1,0] )
print(a[x][y])
print(a[x,y])

These two print statements give different outputs. In the numpy indexing document it states that the operations in the two print statements are the same.
Link of document: https://numpy.org/devdocs/user/basics.indexing.html

Numpy/Python version information:

1.18.1 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]

@seberg
Copy link
Member

seberg commented Jun 9, 2020

Can you say where the documentation is wrong or could be clarified. The case you are showing is explained in this section: https://numpy.org/devdocs/user/basics.indexing.html#indexing-multi-dimensional-arrays and while I am sure it can be improved it seems correct on first glance.

@Faaziy
Copy link
Author

Faaziy commented Jun 9, 2020

Can you say where the documentation is wrong or could be clarified. The case you are showing is explained in this section: https://numpy.org/devdocs/user/basics.indexing.html#indexing-multi-dimensional-arrays and while I am sure it can be improved it seems correct on first glance.

An example of the the case I presented can't be found at the indexing page.
Specifically the developers imply the following:
Annotation 2020-06-09 223950

So using an index for array A like A[x][y] or A[x,y] should lead to the same output according to the documentation. Which is the case for a 1 dimensional input. But the code I presented shows a different thing. The 2 print statements actually differin output!.

So one of the two things is the case:
The documentation is wrong about the equality of the 2 indexing possibilities. Or there is a structural bug in which leads to 2 different outputs. I hope I clarified the issue.

@mattip
Copy link
Member

mattip commented Jun 9, 2020

The documentation you refer to is under the heading Single element indexing. You are not using a single element,.

@Faaziy
Copy link
Author

Faaziy commented Jun 9, 2020

The documentation you refer to is under the heading Single element indexing. You are not using a single element,.

That doesn't change anything, even in the paragraph of multidimensional array indexing this issue/phenomenon doesn't appear.

@rossbar
Copy link
Contributor

rossbar commented Jun 10, 2020

There is an important distinction between two different kinds of indexing behavior that are both accessed with the [] syntax: "basic" and "advanced" indexing. The case you originally reference from the documentation (x[0][2] == x[0, 2]) holds for basic indexing, but in your case, x and y are arrays, which triggers advanced indexing, which has different behavior. Note that the indexing article in the reference guide is a bit more clear on this distinction than the one in the user guide linked above. Hopefully this helps - if you have ideas for improvements to the various documentation pages on indexing (see e.g. #14038), please feel free to propose changes!

@eric-wieser eric-wieser added 04 - Documentation 33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued labels Jun 17, 2020
@rossbar
Copy link
Contributor

rossbar commented Jul 10, 2020

I'm going to go ahead and close this for now - any ideas related to specific improvements to the indexing documentation are more than welcome!

@rossbar rossbar closed this as completed Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
04 - Documentation 33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued
Projects
None yet
Development

No branches or pull requests

5 participants