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

BUG: np.argsort not returning correct order #20574

Closed
RodrigoVillatoro opened this issue Dec 13, 2021 · 3 comments
Closed

BUG: np.argsort not returning correct order #20574

RodrigoVillatoro opened this issue Dec 13, 2021 · 3 comments
Labels
33 - Question Question about NumPy usage or development 50 - Duplicate 57 - Close? Issues which may be closable unless discussion continued

Comments

@RodrigoVillatoro
Copy link

Describe the issue:

The resulting order of np.argsort appears to be incorrect and does not match with the order returned by x[argsort_idx].

Specifically, when sorting in descending order, the highlighted number should be in position 11, not 9.
Screenshot 2021-12-13 at 10 49 06

Strangely enough, this results in the correct ordering of the values:
Screenshot 2021-12-13 at 11 15 27

Not entirely sure what can be going on here. Thanks!

Reproduce the code example:

import numpy as np

x = np.array([ 26.929932  ,  17.864126  ,   5.439558  ,  -0.10773016,
        -9.52839   ,  -7.620389  , -11.919527  , -15.451256  ,
       -16.36096   , -18.313873  , -19.791018  , -18.288052  ,
       -21.068373  , -24.030613  , -23.9124    , -25.630302  ,
       -29.34889   , -29.329416  , -29.221819  , -26.609049  ,
       -29.375376  , -28.213442  , -28.636854  , -28.140215  ,
       -27.100985  ])

# Returns incorrect order of indices
list(zip(x, np.argsort(-1*x)))

# Returns correct order of list
x[np.argsort(-1*x)]

Error message:

No response

NumPy/Python version information:

numpy 1.18.1
python 3.7.10

@seberg seberg added 33 - Question Question about NumPy usage or development 00 - Bug and removed 00 - Bug 33 - Question Question about NumPy usage or development labels Dec 13, 2021
@seberg
Copy link
Member

seberg commented Dec 13, 2021

Argsort does not return ranks, it returns sorting indices to be applied to the input. So the zip() does not make any sense, because the order of the argsort result is sorted and not the original data order.

@seberg seberg added the 57 - Close? Issues which may be closable unless discussion continued label Dec 13, 2021
@RodrigoVillatoro
Copy link
Author

You are completely right. Thank you!

@eric-wieser
Copy link
Member

Duplicate of #8757

@eric-wieser eric-wieser marked this as a duplicate of #8757 Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
33 - Question Question about NumPy usage or development 50 - Duplicate 57 - Close? Issues which may be closable unless discussion continued
Projects
None yet
Development

No branches or pull requests

3 participants