Issue
I have a list of arrays like:
array([[ 0.39703756, 0.60296244],
[ 0.47432672, 0.52567328],
[ 0.9785825 , 0.0214175 ],
...,
[ 0.98861711, 0.01138289],
[ 0.98769643, 0.01230357],
[ 0.99783641, 0.99783641]])
I use the below code to get the max of each row and turn it in to a list:
scores=scores.max(axis=1).astype(float)
scores=scores.tolist()
I get the maximum of each row and am able to turn it to a list. additionally, I want to know which column is the maximum in. i.e. for the first row max is 0.60 and it should say 1 and so on and so forth. However, I wasn't able to do this how can this be done?
Solution
Use numpy.argmax
function. This outputs the indices of the maximum rather than the maximum values themselves. Since this output indices, they will be integers and you don't need the .astype(float)
import numpy as np
scores=np.array([[ 0.39703756, 0.60296244],
[ 0.47432672, 0.52567328],
[ 0.9785825 , 0.0214175 ],
[ 0.98861711, 0.01138289],
[ 0.98769643, 0.01230357],
[ 0.99783641, 0.99783641]])
scores_max=scores.max(axis=1).astype(float)
scores_arg=scores.argmax(axis=1)
scores_arg=scores_arg.tolist()
print(scores_arg)
Answered By - Vivek Kalyanarangan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.