-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Make np.argmax, np.argmin to return indexes and values at the same time #15623
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
You can make the latter case work as you expect by indexing
Get the indices of the maxima along the last axis:
Now pull out the corresponding maximum values from
|
If you really want to do this in a single function call, over in ufunclab I have implemented an assortment of NumPy ufuncs and gufuncs, including Here's your example, using
|
We now have |
Ah, right, and there is even an example that uses For the example above:
(It would be nice if |
I am wondering if we should make an "endorsed" repo for such ufuncs, it could also include things like Julians old ufuncs for composed instructions (add 4 floats, fused multiply add). For most functions it would just be a home to put things with a bit less hesitation, for some it could maybe be an entry point into NumPy proper. |
@WarrenWeckesser, @seberg, I think Warren' functions would be a great addition because my search on internet shows me that people usually use another function to get values and indexes, even in some cases they get values first and indexes second (because of the question on stackoverflow) but I think it damages speed more than get indexes first and values second because the comparations over elements using values (O(n) in worst case) when indexing is direct (O(1)). @WarrenWeckesser, you have something similar (one function) in order to get top K maximum/minimum values and indexes at the same time?. I'm asking something similar to #15128 but returning values and indexes at the same time but only of the top K (to not use just |
Actually, it is C, not Cython.
No, I haven't looked into such a function. I'd probably use |
My fault confussing languages, still is fast than python wrapper
Nevermind, you help me a lot, thanks. I close this issue because was solved with |
You could also do: [ind1[ind2] for ind1, ind2 in zip(data, indexes)] |
Actually, np.argmax returns indexes of maximum values along some axis. On the other side, np.amax returns maximum values along some axis but if you want to get indexes of maximum values with those maximum values it is necessary to go through the array again. For example:
Even with np.arange, go through the array again consumes time (specially on huge arrays). So, np.argmax and np.argmin could return indexes and values at the same time on one look (maybe with an extra parameter in order to make compatible previous versions). For 1D just indexing would be fine, but that not work for arrays with 2 or more axis because it gets the rows elements:
Numpy version: '1.17.5'
Python version: '3.6.9'
The text was updated successfully, but these errors were encountered: