0

I have a 2D list. I want to find the max value and its index of each row. Here is the list

q_table = [[0.16,  0.40,  0.61,  0.48,  0.20],
           [0.42,  0.79,  0.64,  0.54,  0.52],
           [0.64,  0.64,  0.24,  0.93,  0.43],
           [0.33,  0.54,  0.61,  0.43,  0.29],
           [0.25,  0.56,  0.42,  0.69,  0.62]]

Output:

0.61 2
0.79 1
0.93 3
0.61 2
0.69 3

# I'm using python 3.8

Thank You in Advance

Hirusha Fernando
  • 1,156
  • 10
  • 29
Ayon134
  • 21
  • 5
  • 1
    `np.max(q_table, axis=1)`? – Quang Hoang Dec 11 '20 at 04:18
  • Using for loops – bkakilli Dec 11 '20 at 04:20
  • `max`, `argmax`, then column_stack the result – cs95 Dec 11 '20 at 04:20
  • @QuangHoang That gives me a list of max values. This is nice also, but I need index as well. – Ayon134 Dec 11 '20 at 04:21
  • @Ayon134 `argmax` as cs95 suggested will do that – ssp Dec 11 '20 at 04:23
  • @cs95 i used that, for x in range(4): row_max = np.argmax(q_table[x,:]) print(row_max) but this error came, TypeError: list indices must be integers or slices, not tuple – Ayon134 Dec 11 '20 at 04:24
  • Does this answer your question? [How to get the index of a maximum element in a NumPy array along one axis](https://stackoverflow.com/questions/5469286/how-to-get-the-index-of-a-maximum-element-in-a-numpy-array-along-one-axis) – satvik Dec 11 '20 at 04:25
  • @satvik I followed ur link and did this code ``` s = np.argmax(q_table) print(s) ``` it results 13, which is fine, but it requers me to do additional calc to find exact location in 2d list. Is there any btr way – Ayon134 Dec 11 '20 at 04:33
  • what type of calculation? – satvik Dec 11 '20 at 04:37
  • You can use `for loop` and `max()` function. – Hirusha Fernando Dec 11 '20 at 04:43
  • `s = np.argmax(q_table) rows = len(q_table) i = int(s / rows) j = s % rows print('The max value ', q_table[i][j])` but @satvik it gives me max in the whole list, not in single row. – Ayon134 Dec 11 '20 at 04:45
  • @newbieprogrammer this gives max value index in the whole list, I need each row max value index – Ayon134 Dec 11 '20 at 04:51

2 Answers2

2

as suggested in comments you can use max to get the max values from your list and argmax for getting the position.

np.argmax(q_table, axis=1) #returns list of position of max value in each list.
np.max(q_table, axis=1)  # return list of max value in each  list.

you can then use zip function to iterate both the list together and store the output in list of list

import numpy as np
max_list_with_position=[ [x,y] for x,y in zip(np.argmax(q_table, axis=1),np.max(q_table, axis=1))]
print(max_list_with_position)

output:

[[2, 0.61], [1, 0.79], [3, 0.93], [2, 0.61], [3, 0.69]]
Amit Kumar
  • 613
  • 3
  • 15
0
q_table = [[0.16,  0.40,  0.61,  0.48,  0.20],
           [0.42,  0.79,  0.64,  0.54,  0.52],
           [0.64,  0.64,  0.24,  0.93,  0.43],
           [0.33,  0.54,  0.61,  0.43,  0.29],
           [0.25,  0.56,  0.42,  0.69,  0.62]]

rows_count = len(q_table) # to count number of rows
for i in range(rows_count):
    a_row = q_table[i] # taking each row in a variable
    max_value = max(a_row) # find mad value in a single row
    index = a_row.index(max_value) # find the max value's index of a single row
    print("The max value ",max_value, " and Index in ",index)

If there is a better way do suggest.

and here is the output,

The max value  0.61  and Index in  2
The max value  0.79  and Index in  1
The max value  0.93  and Index in  3
The max value  0.61  and Index in  2
The max value  0.69  and Index in  3
Ayon134
  • 21
  • 5