Skip to content
Advertisement

How to find maximums’ indexes by row with Numpy?

I have np 2d array and want to get indexes of max by row.

For example:

JavaScript

Maximum by row is [3, 4, 2]. Indexes are [(0,0) (0,3) (1,1) (1,2) (2,4)]

I tried smth like this

buf = np.apply_along_axis(lambda x: zip(np.where(x == np.max(x))), axis=1, arr=B)

or this

buf = np.where(B == np.max(B,axis = 1))

But it doesnt work.

Also I can’t use loops (otherwise I wouldn’t use numpy either)

Advertisement

Answer

After finding max you need set each max as one row then searching in base array like below:

JavaScript

If you want output like as tuple in your question you can try this:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement