index of N highest elements from a list of numpy array

Message Passing

I need a function that returns the index (row and column) of the N highest element from a matrix/list of numpy array.

Suppose that I have the following matrix:

a = [[4,2,3], 
     [5,0,3]]

I would like to get a list of the indexes (row,column) of the N highest elements. For example, if I want the index of the 4 highest element, the function should return

[(1,0), (0,0), (0,2), (1,2)]

I have tried implementing this as following, but it returns a list of (value, row number), which is not want I need

for i, sub_list in enumerate(a):
    max_list.append((max(sub_list), i))
user3483203

I would flatten, argsort, and unravel_index


f = a.ravel().argsort()
[*zip(*np.unravel_index(f[-n:], a.shape))]

[(0, 2), (1, 2), (0, 0), (1, 0)]

As yatu points out, if you have a larger array, and a small-ish n, you can replace argsort with np.argpartition(a.ravel(), -n).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a pythonic way to sample N consecutive elements from a list or numpy array

Accessing NumPy array elements not in a given index list

Advanced slicing: Given a list of index, pick different elements from a numpy array

Extract elements from numpy array, that are not in list of indexes

Retrieve array elements from a list of index

Numpy 2d array - take N elements from specified index

sum every N elements from numpy array

Finding index of highest element from a list of list

Find all array with second highest elements in a list

Delete elements from python array with given index of elements as list

How to take n elements from numpy array and add them to separate list?

Get n elements of list having the highest property

Numpy create array from list of integers specifying the array elements

Get indices of N highest values in numpy array

Getting index of elements in a numpy array

NumPy - index of highest average

Numpy: get the lowest N elements of an array X, considering only elements whose index is not an element in another array Y

Select elements from numpy array based on a list of lengths

extract elements from numpy array of arrays using list

Remove elements from 2d Numpy array based on a list

PHP: Find highest index of numeric array that has missing elements

Build an Array of StringBuffer with x elements from a List of n elements

Increase numpy array elements using array as index

Finding the index of a numpy array in a list

Find index of numpy array in list

(Numpy) Index list to boolean array

How to get randomly select n elements from a list using in numpy?

Slicing n elements with stride m from numpy array

replace array elements with elements of a list in NumPy