> 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 constructs the slice, since leaving it private frees us to move those functions to c or in the distant future gufuncs. On Fri, Nov 1, 2019, 15:54 Allan Haldane <allanhald...@gmail.com> 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 indexing is fastest: > > >>> %timeit a[np.arange(N),ind] > 1.58 µs ± 18.1 ns per loop > >>> %timeit np.take_along_axis(a, ind[:,None], axis=1) > 6.49 µs ± 57.3 ns per loop > >>> %timeit np.min(a, axis=1) > 9.51 µs ± 64.1 ns per loop > > Probably `take_along_axis` was designed with uses like yours in mind, > but it is not very optimized. > > (I think numpy is lacking a category of efficient > indexing/search/reduction functions, like 'findfirst', 'groupby', > short-circuiting any_*/all_*/nonzero, the proposed oindex/vindex, better > gufunc broadcasting. There is slow but gradual infrastructure work > towards these, potentially). > > Cheers, > Allan > > On 10/30/19 11:31 PM, Daniele Nicolodi 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 <dani...@grinta.net > >> <mailto:dani...@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@python.org <mailto:NumPy-Discussion@python.org> > >> https://mail.python.org/mailman/listinfo/numpy-discussion > >> > >> > >> _______________________________________________ > >> NumPy-Discussion mailing list > >> NumPy-Discussion@python.org > >> https://mail.python.org/mailman/listinfo/numpy-discussion > >> > > > > _______________________________________________ > > NumPy-Discussion mailing list > > NumPy-Discussion@python.org > > https://mail.python.org/mailman/listinfo/numpy-discussion > > > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion >
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion