awkward.argsort is wrong for arrays with dimensions > 2
See original GitHub issueResults for awkard.argsort is wrong for arrays with dimensions greater than 2. Example output from awkward1 version 0.3.1
Code
import awkward1
x = awkward1.Array([[[0.01539855, 0.45536142, 0.9510973 , 0.17593855, 0.30485241],
[0.57157337, 0.2281758 , 0.1437675 , 0.80287155, 0.11580015],],
[[0.77897828, 0.75505657, 0.61302232, 0.56694878, 0.99761142],
[0.38639971, 0.69037858, 0.61298759, 0.6602239 , 0.93297311],],]) # 2x2x5 array generated from numpy.random
print(awkward1.to_numpy(awkward1.argsort(x,axis=-1)))
Resulting output:
[[[0 3 4 1 2]
[4 2 1 0 3]]
[[3 2 1 0 4]
[0 2 3 1 4]]]
Expected output:
[[[0 3 4 1 2]
[3 2 1 4 0]]
[[3 2 1 0 4]
[0 3 1 2 4]]]
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Weird / Wrong outpout of np.argsort() - Stack Overflow
I think the issue is with what you think argsort is outputting. Let's focus on a simpler 1D example: arr = np.array([5, 10,...
Read more >argsort gives wrong results · Issue #8757 · numpy ... - GitHub
The argsort function seems to be broken. Looking at the code provided, the argsort for rows [0, 1] is correct but it's messed...
Read more >ak.argsort — Awkward Array documentation - Read the Docs
array – Data for which to get a sorting index, possibly within nested lists. axis (int) – The dimension at which this operation...
Read more >Scikit-HEP/awkward-array - Gitter
The first is a simplified version of yours: your example creates arrays from Python data, which internally invoke the ArrayBuilder. The second makes...
Read more >Tentative_NumPy_Tutorial - SciPy wiki dump
In Numpy dimensions are called axes. The number of axes is rank. For example, the coordinates of a point in 3D space [1,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Aha! You thought it was the inverse, a natural assumption to make.
That is, if selecting an item from an array is a function,
f: int → dtype, then slicing by an array,g: int → int, is composition:f ⊗ g: int → int ⊗ int → dtypeorf ⊗ g: int → dtype. Thatg: int → intfunction can have an inverse,g⁻¹, (depending on how general we take it to be, but from argsort, it’s always a permutation), and it’s natural to guess that it’s supposed to beg⁻¹when it’s supposed to beg. They have the sameint → intsignature, after all.That’s why we check to see what NumPy does, to be sure we pick the same conventions.
@jpivarski Ah, thanks. It looks like I have misunderstood the meaning of the output: So basically, the
argsortfunction outputs the indices to retrieve the objects to place the numbers in sorted order. While the output I was expecting was the position at which the numbers will end up. Thanks for the clarification! (I guess it was unfortunate that the first row of the example outputs has two answers coincided, so I as confused as to why the output is ‘sometimes right’ when it as I that was wrong all along)So yes this issue is closed.