[Numpy-discussion] maximum value and corresponding index

Hans Meine meine@informatik.uni-hamburg...
Thu Aug 6 11:05:36 CDT 2009


On Thursday 06 August 2009 17:27:51 Robert Kern wrote:
> 2009/8/6 Hans Meine <meine@informatik.uni-hamburg.de>:
> > On Wednesday 05 August 2009 22:06:03 David Goldsmith wrote:
> >> But you can "cheat" and put them on one line (if that's all you're 
after):
> >> >>> x = np.array([1, 2, 3])
> >> >>> maxi = x.argmax(); maxv = x[maxi]
> >
> > Is there any reason not to put this as a convenience function into numpy?
> > It is needed so frequently, and it's a shame that the shortest solution
> > traverses the array twice [...]
>
> The array is only traversed once. Indexing is O(1).

Yes, but I wrote "the shortest solution", which is to call .argmax() and 
.max(), since..

> > the above is usually longer due to variable names, and/or >1 dimensions

which requires unravel_index() (note that flattening is also inefficient when 
the array is strided).

Never mind,
  Hans


More information about the NumPy-Discussion mailing list