[Numpy-discussion] Right way to do fancy indexing from argsort() result?

Benjamin Root ben.v.root at gmail.com
Mon Mar 26 14:24:27 EDT 2018


I seem to be losing my mind... I can't seem to get this to work right.

I have a (N, k) array `distances` (along with a bunch of other arrays of
the same shape). I need to resort the rows, so I do:

indexs = np.argsort(distances, axis=1)

How do I use this index array correctly to get back distances sorted along
rows? Note, telling me to use `np.sort()` isn't going to work because I
need to apply the same indexing to a couple of other arrays.

new_dists = distances[indexs]

gives me a (N, k, k) array, while

new_dists = np.take(indexs, axis=1)

gives me a (N, N, k) array.

What am I missing?

Thanks!
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180326/e94be523/attachment.html>


More information about the NumPy-Discussion mailing list