<div dir="ltr">Depends on how big your array is.  Numpy C code is 150x+ faster than python overhead. Fancy indexing can be expensive in my experience.  Without trying I'd guess arr[:, argmax(arr, axis=1)] does what you want, but even if it is, try profiling the two and see.  I highly doubt such would be even 1% of your run time, but it depends on what your doing.  Part of python with numpy is slightly not caring about big O because trying to be clever is rarely worth it in my experience.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 31, 2019 at 12:35 AM Daniele Nicolodi <<a href="mailto:daniele@grinta.net">daniele@grinta.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 30/10/2019 22:42, Elliot Hallmark wrote:<br>
> I wouldn't be surprised at all if calling max in addition to argmax<br>
> wasn't as fast or faster than indexing the array using argmax.<br>
> Regardless, just use that then profile when you're done with the<br>
> whole thing and see if there's any gains to be made. Very likely not here.<br>
<br>
Hi Elliot,<br>
<br>
how do you arrive at this conclusion? np.argmax() and np.max() are O(N)<br>
while indexing is O(1) thus I don't see how you can conclude that<br>
running both np.argmax() and np.max() on the input array is going to<br>
incur in a small penalty compared to running np.argmax() and then indexing.<br>
<br>
Cheers,<br>
Dan<br>
<br>
<br>
> <br>
> -elliot<br>
> <br>
> On Wed, Oct 30, 2019, 10:32 PM Daniele Nicolodi <<a href="mailto:daniele@grinta.net" target="_blank">daniele@grinta.net</a><br>
> <mailto:<a href="mailto:daniele@grinta.net" target="_blank">daniele@grinta.net</a>>> wrote:<br>
> <br>
>     On 30/10/2019 19:10, Neal Becker wrote:<br>
>     > max(axis=1)?<br>
> <br>
>     Hi Neal,<br>
> <br>
>     I should have been more precise in stating the problem. Getting the<br>
>     values in the array for which I'm looking at the maxima is only one step<br>
>     in a more complex piece of code for which I need the indexes along the<br>
>     second axis of the array. I would like to avoid to have to iterate the<br>
>     array more than once.<br>
> <br>
>     Thank you!<br>
> <br>
>     Cheers,<br>
>     Dan<br>
> <br>
> <br>
>     > On Wed, Oct 30, 2019, 7:33 PM Daniele Nicolodi <<a href="mailto:daniele@grinta.net" target="_blank">daniele@grinta.net</a><br>
>     <mailto:<a href="mailto:daniele@grinta.net" target="_blank">daniele@grinta.net</a>><br>
>     > <mailto:<a href="mailto:daniele@grinta.net" target="_blank">daniele@grinta.net</a> <mailto:<a href="mailto:daniele@grinta.net" target="_blank">daniele@grinta.net</a>>>> wrote:<br>
>     ><br>
>     >     Hello,<br>
>     ><br>
>     >     this is a very basic question, but I cannot find a satisfying<br>
>     answer.<br>
>     >     Assume a is a 2D array and that I get the index of the maximum<br>
>     value<br>
>     >     along the second dimension:<br>
>     ><br>
>     >     i = a.argmax(axis=1)<br>
>     ><br>
>     >     Is there a better way to get the value of the maximum array<br>
>     entries<br>
>     >     along the second axis other than:<br>
>     ><br>
>     >     v = a[np.arange(len(a)), i]<br>
>     ><br>
>     >     ??<br>
>     ><br>
>     >     Thank you.<br>
>     ><br>
>     >     Cheers,<br>
>     >     Daniele<br>
>     >     _______________________________________________<br>
>     >     NumPy-Discussion mailing list<br>
>     >     <a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
>     <mailto:<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a>><br>
>     <mailto:<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
>     <mailto:<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a>>><br>
>     >     <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
>     ><br>
>     ><br>
>     > _______________________________________________<br>
>     > NumPy-Discussion mailing list<br>
>     > <a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a> <mailto:<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a>><br>
>     > <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
>     ><br>
> <br>
>     _______________________________________________<br>
>     NumPy-Discussion mailing list<br>
>     <a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a> <mailto:<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a>><br>
>     <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
> <br>
> <br>
> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
> <br>
<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>