özgür Sanli
özgür Sanli

Reputation: 81

Numpy array chosing second largest number and assign it to a list in python

i have an array:probs=

[[0.01727184 0.62906768 0.35366048]
 [0.         0.34376546 0.65623454]
 [0.         0.37798286 0.62201714]
 [0.         0.44000258 0.55999742]
 [0.         0.44882893 0.55117107]
 [0.         0.44350829 0.55649171]
 [0.         0.53882745 0.46117255]
 [0.         0.51644612 0.48355388]
 [0.         0.61091    0.38909   ]
 [0.         0.62865738 0.37134262]
 [0.         0.71264312 0.28735688]
 [0.         1.         0.        ]]

I need to select the biggest second value and append a list according to INDEX.Like first row the second biggest number is 0.3536 than the list first number will 2 because of index.Also if there is no second biggest number than the list will assigned with the biggest number like the last array.According to this array our list will be:

List1=[2,1,1,1,1,1,2,2,2,2,2,1]

Thank for everyone!

Upvotes: 0

Views: 753

Answers (1)

mozway
mozway

Reputation: 262149

You can use argsort:

List1 = np.argsort(a)[:,-2].tolist()

Upvotes: 2

Related Questions