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

argsort does not work for multidimensional arrays #4724

Closed
healther opened this issue May 19, 2014 · 12 comments
Closed

argsort does not work for multidimensional arrays #4724

healther opened this issue May 19, 2014 · 12 comments

Comments

@healther
Copy link

consider the following code:
a = np.random.random([5,5])
ind = np.argsort(a, axis=1)
a_sorted = a[ind]
np.sort(a, axis=1)

now a_sorted and a should be both sorted along the 1-axis. However not even the shapes are identical anymore. a is still of (5,5) while a_sorted is (5,5,5). If this is the intended behaviour, can someone tell me why?

@rkern
Copy link
Member

rkern commented May 19, 2014

argsort() is working fine, but indexing doesn't work the way that you are expecting it to.

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing

Try this:

a = np.random.random([5,5])
i = np.arange(len(a))[:, np.newaxis]
j = np.argsort(a, axis=1)
a_sorted = a[i, j]
np.sort(a, axis=1)

@pv pv closed this as completed May 19, 2014
@lzkelley
Copy link
Contributor

I was confused in the same way. The documentation specifically states,

Returns
index_array : ndarray, int
Array of indices that sort a along the specified axis.
In other words, a[index_array] yields a sorted a.

Which only applies to a 1D array. Should the documentation be extended to specify that this is not the case for ND arrays?

@shoyer
Copy link
Member

shoyer commented Oct 15, 2015

@lzkelley Yes, a patch to improve the documentation would be very welcome.

@lzkelley
Copy link
Contributor

@shoyer cool, I'll make a PR for it and update

charris added a commit that referenced this issue Oct 23, 2015
DOC: clarify usage of 'argparse' return value (see #4724).
jaimefrio pushed a commit to jaimefrio/numpy that referenced this issue Mar 22, 2016
In response to Ticket numpy#4724, explain that the 'index_array' returned by
'argparse' can only be used to (directly) sort a one-dimensional input
array.
@endolith
Copy link
Contributor

It would be good if there were an example of how to actually use the output to sort the N-D array.

It would also be good if there were a more readable solution than a[np.arange(np.shape(a)[0])[:,np.newaxis], np.argsort(a)]

@charris
Copy link
Member

charris commented Dec 18, 2016

Maybe @seberg could make it one of his indexing function additions.

@seberg
Copy link
Member

seberg commented Dec 18, 2016

I don't think it fits indexing very obviously, a pick function or so is probably an easier match.

@shoyer
Copy link
Member

shoyer commented Dec 18, 2016

+1 for a new function.

@lzkelley
Copy link
Contributor

Isn't the entire purpose of argsort (as apposed to sort) to be used for indexing?

@endolith
Copy link
Contributor

argsort supplies the indexes to sort along an axis, but actually sorting along that axis using the indexes seems needlessly verbose. Unless there's a simpler way I'm not aware of.

@eric-wieser
Copy link
Member

a pick function or so is probably an easier match.

This is #8708

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants