question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

awkward.argsort is wrong for arrays with dimensions > 2

See original GitHub issue

Results 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:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jpivarskicommented, Nov 2, 2020

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 → dtype or f ⊗ g: int → dtype. That g: int → int function 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 be g⁻¹ when it’s supposed to be g. They have the same int → int signature, after all.

That’s why we check to see what NumPy does, to be sure we pick the same conventions.

0reactions
yimuchencommented, Nov 2, 2020

@jpivarski Ah, thanks. It looks like I have misunderstood the meaning of the output: So basically, the argsort function 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found