Right way to do fancy indexing from argsort() result?
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
On Mon, Mar 26, 2018 at 11:24 AM, Benjamin Root
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?
Broadcasting! new_dists = distances[np.arange(N)[:, np.newaxis], indexs] -- Robert Kern
Ah, yes, I should have thought about that. Kind of seems like something
that we could make `np.take()` do, somehow, for something that is easier to
read.
Thank you!
Ben Root
On Mon, Mar 26, 2018 at 2:28 PM, Robert Kern
On Mon, Mar 26, 2018 at 11:24 AM, Benjamin Root
wrote: 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?
Broadcasting!
new_dists = distances[np.arange(N)[:, np.newaxis], indexs]
-- Robert Kern
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
https://github.com/numpy/numpy/issues/8708 is a proposal to add such a
function, with an implementation in https://github.com/numpy/numpy/pull/8714
Eric
On Mon, 26 Mar 2018 at 11:35 Benjamin Root
Ah, yes, I should have thought about that. Kind of seems like something that we could make `np.take()` do, somehow, for something that is easier to read.
Thank you! Ben Root
On Mon, Mar 26, 2018 at 2:28 PM, Robert Kern
wrote: On Mon, Mar 26, 2018 at 11:24 AM, Benjamin Root
wrote: 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?
Broadcasting!
new_dists = distances[np.arange(N)[:, np.newaxis], indexs]
-- Robert Kern
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
participants (3)
-
Benjamin Root
-
Eric Wieser
-
Robert Kern