Skip to content
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

numpy.argsort() bug #11678

Closed
JerryZhou199 opened this issue Aug 6, 2018 · 2 comments
Closed

numpy.argsort() bug #11678

JerryZhou199 opened this issue Aug 6, 2018 · 2 comments

Comments

@JerryZhou199
Copy link

the function argsort() returns a wrong indice that would sort an array.
for example:

from numpy import *
a=array([4,2,3,1])
argsort(a)
array([3, 1, 2, 0], dtype=int64)

so this is right, it returned the sorted index. but the follow example is wrong:

a=array([4,2,3])
argsort(a)
array([1, 2, 0], dtype=int64)

the right index should be array([2,0,1]),and the follow example is also wrong:

a=array([5,1,4,3,2])
argsort(a)
array([1, 4, 3, 2, 0], dtype=int64)

the right index should be array([4,0,3,2,1]),and the follow example is also wrong:

a=array([7,1,5,2,4,6,3])
argsort(a)
array([1, 3, 6, 4, 2, 5, 0], dtype=int64)

the right index should be array([6,0,4,1,3,5,2]),so I find a error rule that function argsort() will display the wrong index when an array that starts with a maximum and has an odd number of elements.

Reproducing code example:

>>>from numpy import *
>>> a=array([4,2,3,1])
>>> argsort(a)
array([3, 1, 2, 0], dtype=int64)
### so this is right, it returned the sorted index. but the follow example is wrong:
>>> a=array([4,2,3])
>>> argsort(a)
array([1, 2, 0], dtype=int64)
### the right index should be array([2,0,1]),and the follow example is also wrong:

>>> a=array([5,1,4,3,2])
>>> argsort(a)
array([1, 4, 3, 2, 0], dtype=int64)
### the right index should be array([4,0,3,2,1]),and the follow example is also wrong:
>>> a=array([7,1,5,2,4,6,3])
>>> argsort(a)
array([1, 3, 6, 4, 2, 5, 0], dtype=int64)

Error message:

Numpy/Python version information:

Numpy 1.13.3/Python 3.5.2

1.13.3 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]

@pv
Copy link
Member

pv commented Aug 6, 2018

The invariant satisfied by argsort is "a[np.argsort(a)] is sorted", so all of the above are fine.

@eric-wieser
Copy link
Member

Duplicate of #8757

@eric-wieser eric-wieser marked this as a duplicate of #8757 Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants