-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: Add a function that performs the indexing needed to map argsort to sort #8708
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
Comments
See #6078 for what I think is the same issue; that has also some code snippets from myself and @shoyer as well as a comment by @jaimefrio that maybe the best approach would be for the |
I think that changing So I'd be strongly in favor of either making a method for this, or re-purposing an existing one. I also suspect that going through an index tuple leads to slower indexing than writing a C function to directly handle the loop described above, since looking up an index in |
Is this a good candidate for a Cython function, to implement this as just a |
Personally doubt it, since its probably a plan to use the numpy iteration API, and I am not sure there is any convenience in doing that in cython. OTOH you can probably get half way (a bit like |
Can you elaborate on what you mean by that? |
I mean performance on larger arrays is probably not very good, just like np.take comparing to advanced indexing. Except in some cases (here probably small dimensions or so), which don't optimize well then doing the fancier npyiter. |
Should I just submit a pure-python PR for now then? |
If you like, didn't make up my mind what I think the best approach. |
@eric-wieser -- you're code is missing one aspect, that to be able to mimic Note that yet another place where one could add this to is My overall preference, though, would be to use |
This is the choice of Either way, this can be emulated with: >>> ai = np.expand_dims(a.argmin(axis=axis), axis=axis)
>>> take_along_axis(a, ai, axis=axis) This is a more powerful function than just taking |
|
Nothing beats actual code! I'm quite happy with |
Hmm. I just tried this
and got this error:
Oh, duh, never mind, the sample |
this should fix:
|
Using The PR for this suggestion already handles negative indices correctly |
Hello, any update on this? |
@crusaderky - there is a PR, see #8714. But your comment is a reminder to look at that and see what remains to be done -- it would still be good to have this functionality! |
This is the reduced version that does not allow any insertion of extra dimensions
This is the reduced version that does not allow any insertion of extra dimensions
fixed by #11105 |
Correct me if I'm missing something, but while what you say is true in case you want the behaviour of It seems to me it should be possible to modify |
Awesome! |
There've been two prs recently ( #8703, #8678 ) that have needed this function, and in general it's needed for any kind of vectorized function returning indices along an axis (
argsort
,argpartition
,argmin
,argmax
,count
, ...)A python implementation looks something like this:
Which works as intended:
As far as I can tell,
np.take
isn't suitable for this right now, as instead it computes:instead of
Perhaps
np.take(..., broadcast=True)
should b a thing that means the above?If not, there's perhaps enough similarity with
apply_along_axis
to justify the similar name:apply_along_axis
:out[a..., k..., b...] = f(arr[a..., :, b...])
take_along_axis
:out[a..., k..., b...] = arr[a..., inds[a..., k..., b...], b...]
The text was updated successfully, but these errors were encountered: