Re: [Numpy-discussion] argmax() indexes to value

2019-11-01 Thread CJ Carey
You could move some of the cost to index-creation time by converting the per-row indices into flattened indices: In [1]: a = np.random.random((5, 6)) In [2]: i = a.argmax(axis=1) In [3]: a[np.arange(len(a)), i] Out[3]: array([0.95774465, 0.90940106, 0.98025448, 0.97836906, 0.80483784]) In [4

Re: [Numpy-discussion] argmax() indexes to value

2019-11-01 Thread Daniele Nicolodi
On 01-11-2019 09:51, Allan Haldane wrote: > my thought was to try `take` or `take_along_axis`: > >ind = np.argmin(a, axis=1) >np.take_along_axis(a, ind[:,None], axis=1) > > But those functions tend to simply fall back to fancy indexing, and are > pretty slow. On my system plain fancy inde

Re: [Numpy-discussion] argmax() indexes to value

2019-11-01 Thread Daniele Nicolodi
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

Re: [Numpy-discussion] argmax() indexes to value

2019-11-01 Thread Eric Wieser
> On my system plain fancy indexing is fastest Hardly surprising, since take_along_axis is doing that under the hood, after constructing the index for you :) https://github.com/numpy/numpy/blob/v1.17.0/numpy/lib/shape_base.py#L58-L172 I deliberately didn't expose the internal function that const

Re: [Numpy-discussion] argmax() indexes to value

2019-11-01 Thread Allan Haldane
my thought was to try `take` or `take_along_axis`: ind = np.argmin(a, axis=1) np.take_along_axis(a, ind[:,None], axis=1) But those functions tend to simply fall back to fancy indexing, and are pretty slow. On my system plain fancy indexing is fastest: >>> %timeit a[np.arange(N),ind] 1.58 µ

Re: [Numpy-discussion] argmax() indexes to value

2019-10-31 Thread Elliot Hallmark
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% o

Re: [Numpy-discussion] argmax() indexes to value

2019-10-30 Thread Daniele Nicolodi
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

Re: [Numpy-discussion] argmax() indexes to value

2019-10-30 Thread Elliot Hallmark
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. -elliot On Wed, Oct 30, 20

Re: [Numpy-discussion] argmax() indexes to value

2019-10-30 Thread Daniele Nicolodi
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 ar

Re: [Numpy-discussion] argmax() indexes to value

2019-10-30 Thread Neal Becker
max(axis=1)? On Wed, Oct 30, 2019, 7:33 PM Daniele Nicolodi 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 bette

[Numpy-discussion] argmax() indexes to value

2019-10-30 Thread Daniele Nicolodi
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: