[Numpy-discussion] argmax() indexes to value

Daniele Nicolodi daniele at grinta.net
Fri Nov 1 12:57:51 EDT 2019


On 31-10-2019 01:44, Elliot Hallmark wrote:
> 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,

It does not.

> 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.

Why do you think I am asking for advice on how to do the complicated
thing?  If a 2x increase in the run time would have not mattered, I
would not have bothered. Don't you think?

I appreciate the effort spent guiding inexperienced users toward
pragmatic solutions and not over complicate their code. However, it is
disappointing to have very precise questions dismissed as "that is
complicated, thus you don't really want to do it".

Best,
Dan

> On Thu, Oct 31, 2019 at 12:35 AM Daniele Nicolodi <daniele at grinta.net
> <mailto:daniele at grinta.net>> wrote:
> 
>     On 30/10/2019 22:42, Elliot Hallmark wrote:
>     > I wouldn't be surprised at all if calling max in addition to argmax
>     > wasn't as fast or faster than indexing the array using argmax.
>     > Regardless, just use that then profile when you're done with the
>     > whole thing and see if there's any gains to be made. Very likely
>     not here.
> 
>     Hi Elliot,
> 
>     how do you arrive at this conclusion? np.argmax() and np.max() are O(N)
>     while indexing is O(1) thus I don't see how you can conclude that
>     running both np.argmax() and np.max() on the input array is going to
>     incur in a small penalty compared to running np.argmax() and then
>     indexing.
> 
>     Cheers,
>     Dan
> 
> 
>     >
>     > -elliot
>     >
>     > On Wed, Oct 30, 2019, 10:32 PM Daniele Nicolodi
>     <daniele at grinta.net <mailto:daniele at grinta.net>
>     > <mailto:daniele at grinta.net <mailto:daniele at grinta.net>>> wrote:
>     >
>     >     On 30/10/2019 19:10, Neal Becker wrote:
>     >     > max(axis=1)?
>     >
>     >     Hi Neal,
>     >
>     >     I should have been more precise in stating the problem.
>     Getting the
>     >     values in the array for which I'm looking at the maxima is
>     only one step
>     >     in a more complex piece of code for which I need the indexes
>     along the
>     >     second axis of the array. I would like to avoid to have to
>     iterate the
>     >     array more than once.
>     >
>     >     Thank you!
>     >
>     >     Cheers,
>     >     Dan
>     >
>     >
>     >     > On Wed, Oct 30, 2019, 7:33 PM Daniele Nicolodi
>     <daniele at grinta.net <mailto:daniele at grinta.net>
>     >     <mailto:daniele at grinta.net <mailto:daniele at grinta.net>>
>     >     > <mailto:daniele at grinta.net <mailto:daniele at grinta.net>
>     <mailto:daniele at grinta.net <mailto:daniele at grinta.net>>>> wrote:
>     >     >
>     >     >     Hello,
>     >     >
>     >     >     this is a very basic question, but I cannot find a
>     satisfying
>     >     answer.
>     >     >     Assume a is a 2D array and that I get the index of the
>     maximum
>     >     value
>     >     >     along the second dimension:
>     >     >
>     >     >     i = a.argmax(axis=1)
>     >     >
>     >     >     Is there a better way to get the value of the maximum array
>     >     entries
>     >     >     along the second axis other than:
>     >     >
>     >     >     v = a[np.arange(len(a)), i]
>     >     >
>     >     >     ??
>     >     >
>     >     >     Thank you.
>     >     >
>     >     >     Cheers,
>     >     >     Daniele
>     >     >     _______________________________________________
>     >     >     NumPy-Discussion mailing list
>     >     >     NumPy-Discussion at python.org
>     <mailto:NumPy-Discussion at python.org>
>     >     <mailto:NumPy-Discussion at python.org
>     <mailto:NumPy-Discussion at python.org>>
>     >     <mailto:NumPy-Discussion at python.org
>     <mailto:NumPy-Discussion at python.org>
>     >     <mailto:NumPy-Discussion at python.org
>     <mailto:NumPy-Discussion at python.org>>>
>     >     >     https://mail.python.org/mailman/listinfo/numpy-discussion
>     >     >
>     >     >
>     >     > _______________________________________________
>     >     > NumPy-Discussion mailing list
>     >     > NumPy-Discussion at python.org
>     <mailto:NumPy-Discussion at python.org>
>     <mailto:NumPy-Discussion at python.org
>     <mailto:NumPy-Discussion at python.org>>
>     >     > https://mail.python.org/mailman/listinfo/numpy-discussion
>     >     >
>     >
>     >     _______________________________________________
>     >     NumPy-Discussion mailing list
>     >     NumPy-Discussion at python.org
>     <mailto:NumPy-Discussion at python.org>
>     <mailto:NumPy-Discussion at python.org
>     <mailto:NumPy-Discussion at python.org>>
>     >     https://mail.python.org/mailman/listinfo/numpy-discussion
>     >
>     >
>     > _______________________________________________
>     > NumPy-Discussion mailing list
>     > NumPy-Discussion at python.org <mailto:NumPy-Discussion at python.org>
>     > https://mail.python.org/mailman/listinfo/numpy-discussion
>     >
> 
>     _______________________________________________
>     NumPy-Discussion mailing list
>     NumPy-Discussion at python.org <mailto:NumPy-Discussion at python.org>
>     https://mail.python.org/mailman/listinfo/numpy-discussion
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
> 



More information about the NumPy-Discussion mailing list