Index of maximum value of each row of an array in Python

Question:

I have an array Pr. I want to print index of maximum value of each row. For example, for row 0, the maximum value is 1.72731864e+003 and it occurs at index 0. I present the expected output.

import numpy as np 
Pr = np.array([[1.72731864e+003, 0.00000000e+000],
       [0.00000000e+000, 1.24439020e+003]])

MaxPr=Pr.max(axis=1)

The expected output is

indices=[0,1]
Asked By: isaacmodi123

||

Answers:

try using np.argmax function. it is used to find the index of the maximum value along the specified axis, which in this case is axis 1 (i.e., the rows). The resulting array of indices is then printed to the console. so indices = np.argmax(Pr, axis=1) print(indices)

Answered By: lavascone
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.