The numpy arrays contain prediction probabilities which looks like this:
predict_prob1 =([[0.95602106, 0.04397894],
[0.93332366, 0.06667634],
[0.97311459, 0.02688541],
[0.97323962, 0.02676038]])
predict_prob2 =([[0.70425144, 0.29574856],
[0.69751251, 0.30248749],
[0.7072872 , 0.2927128 ],
[0.68683139, 0.31316861]])
predict_prob3 =([[0.56551921, 0.43448079],
[0.93321106, 0.06678894],
[0.92345399, 0.07654601],
[0.88396842, 0.11603158]])
I want to compare these three numpy.ndarray elementwise and find out which array has the maximum probability as a result. Three of the arrays are of the same length. I have tried to implement something like this which is not correct.
for i in range(len(predict_prob1)):
if(predict_prob1[i] > predict_prob2[i])
c = predict_prob1[i]
else
c = predict_prob2[i]
if(c > predict_prob3[i])
result = c
else
result = array[i]
Please help!!
For me, it's not completely clear what you're asking — If your desired result is a 4x2 array that indexes which of the three arrays has the max value in position i,j then you want to use np.argmax
>>> import numpy as np
>>> predict_prob1 =([[0.95602106, 0.04397894],
[0.93332366, 0.06667634],
[0.97311459, 0.02688541],
[0.97323962, 0.02676038]])
>>> predict_prob2 =([[0.70425144, 0.29574856],
[0.69751251, 0.30248749],
[0.7072872 , 0.2927128 ],
[0.68683139, 0.31316861]])
>>> predict_prob3 =([[0.56551921, 0.43448079],
[0.93321106, 0.06678894],
[0.92345399, 0.07654601],
[0.88396842, 0.11603158]])
>>> np.argmax((predict_prob1,predict_prob2,predict_prob3), 0)
array([[0, 2],
[0, 1],
[0, 1],
[0, 1]])
>>>
Addendum
Having read a comment of the OP I add the following to my answer
>>> names = np.array(['predict_prob%d'%(i+1) for i in range(3)])
>>> names[np.argmax((predict_prob1,predict_prob2,predict_prob3),0)]
array([['predict_prob1', 'predict_prob3'],
['predict_prob1', 'predict_prob2'],
['predict_prob1', 'predict_prob2'],
['predict_prob1', 'predict_prob2']], dtype='<U13')
>>>
You could do with np.maximum.reduce:
np.maximum.reduce([A, B, C])
where A, B, C are numpy.ndarray
For your example it results:
[[0.95602106 0.43448079]
[0.93332366 0.30248749]
[0.97311459 0.2927128 ]
[0.97323962 0.31316861]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With