[Numpy-discussion] Find indices of largest elements

Skipper Seabold jsseabold@gmail....
Wed Apr 14 10:48:26 CDT 2010


On Wed, Apr 14, 2010 at 11:16 AM, Nikolaus Rath <Nikolaus@rath.org> wrote:
> Hello,
>
> How do I best find out the indices of the largest x elements in an
> array?
>
> Example:
>
> a = [ [1,8,2], [2,1,3] ]
> magic_function(a, 2) == [ (0,1), (1,2) ]
>
> Since the largest 2 elements are at positions (0,1) and (1,2).

Something like this might be made to work, if you want the max
elements along all the rows.

In [3]: a = np.asarray(a)

In [4]: a[range(len(a)),np.argmax(a, axis=1)]
Out[4]: array([8, 3])

Skipper


More information about the NumPy-Discussion mailing list