Home > Mobile >  How to get 8 maximum / highest values in each row of 2D array with their indices
How to get 8 maximum / highest values in each row of 2D array with their indices

Time:12-19

I'm working on a Python script. I'm trying to achieve a list of 8 highest values and their respective indices of every single row of my 2D array in Python. The shape of my array has 4148 rows and 167 columns. What I essentially want is that for every row it should give me the 8 highest values (in descending order) present in that row with their indices.

I'm relatively new to Python, and I've tried to implement this below, however it gives me the overall 8 maximum values and their indices in the whole array.

a = predicting[:]
indices =  np.argpartition(a.flatten(), -8)[-8:]
np.vstack(np.unravel_index(indices, a.shape)).T

CodePudding user response:

If you can put the row data in a dictionary, swap the key and value and put it in to a list, then sort it in reverse. Then take the first 8 values of the list for each row.

put the row data in a dictionary called dictionaryRow, swap key and value and put into a list, sort in reverse)

x = sorted( [ (value, key) for key, value in dictionaryRow.items() ] ), reverse=True)

Take the top 8 from the list

x[:8]

CodePudding user response:

You can refer to my exampl. You will get a 2D sequence containing the index of the 8 largest numbers and a 2D sequence containing the 8 largest numbers.:

a = np.random.randint(0,12,size=(12,12))
indice =  np.argsort(-a)
indice = indice[:,:8]
b = np.sort(-a.copy())*-1
maximum_8 = b[:,:8]

One output: a

array([[ 1, 10, 11,  8,  8,  2,  4, 11,  2,  5,  3,  6],
   [11,  2,  2,  7,  3,  3,  9,  0,  0,  0, 10,  4],
   [ 8, 10,  8, 10,  5,  9,  6,  7,  3,  5,  2,  8],
   [ 4,  8,  8,  2,  6,  2,  0,  7,  1, 10, 10,  6],
   [ 9,  1,  5,  0,  6,  4,  3,  6,  7,  0,  7,  7],
   [ 3,  7,  8,  0, 11, 10, 10,  8,  2,  7,  2,  7],
   [ 6,  7,  5, 11,  6,  5,  4,  3,  0,  0,  8,  2],
   [ 7, 11,  7,  9, 11, 11,  8, 11,  4, 11,  6, 11],
   [11,  3,  9,  7, 11,  8, 11,  3,  8,  9,  0,  3],
   [ 4,  7,  6,  9, 11,  3,  8,  0,  5, 11,  6,  5],
   [ 9, 11,  8,  2,  5,  4,  4,  4,  9,  4,  7,  9],
   [ 5,  5,  3,  6,  4,  8,  4,  9,  4,  1,  8,  9]])

indice

array([[ 2,  7,  1,  3,  4, 11,  9,  6],
   [ 0, 10,  6,  3, 11,  4,  5,  1],
   [ 1,  3,  5,  0,  2, 11,  7,  6],
   [ 9, 10,  1,  2,  7,  4, 11,  0],
   [ 0,  8, 10, 11,  4,  7,  2,  5],
   [ 4,  5,  6,  2,  7,  1,  9, 11],
   [ 3, 10,  1,  0,  4,  2,  5,  6],
   [ 1,  4,  5,  7,  9, 11,  3,  6],
   [ 0,  4,  6,  2,  9,  5,  8,  3],
   [ 4,  9,  3,  6,  1,  2, 10,  8],
   [ 1,  0,  8, 11,  2, 10,  4,  5],
   [ 7, 11,  5, 10,  3,  0,  1,  4]], dtype=int64)

maximum_8

array([[11, 11, 10,  8,  8,  6,  5,  4],
   [11, 10,  9,  7,  4,  3,  3,  2],
   [10, 10,  9,  8,  8,  8,  7,  6],
   [10, 10,  8,  8,  7,  6,  6,  4],
   [ 9,  7,  7,  7,  6,  6,  5,  4],
   [11, 10, 10,  8,  8,  7,  7,  7],
   [11,  8,  7,  6,  6,  5,  5,  4],
   [11, 11, 11, 11, 11, 11,  9,  8],
   [11, 11, 11,  9,  9,  8,  8,  7],
   [11, 11,  9,  8,  7,  6,  6,  5],
   [11,  9,  9,  9,  8,  7,  5,  4],
   [ 9,  9,  8,  8,  6,  5,  5,  4]])
  • Related