4

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!!

3
  • 1
    The largest value of all values, or the array that has the largest value? You want just a maximum single value out of the 24 values given? Commented Mar 8, 2019 at 12:09
  • 2
    so what should be the desired output Commented Mar 8, 2019 at 12:10
  • 1
    @ 9769953 and @Jeril My desired output is the array which would contain the names of the array which has the maximum value in each position. following the example datasets, my desired output will be [predict_prob1,predict_prob1,predict_prob1,predict_prob1] since this array has the largest value among the three for all 4 rows. Commented Mar 8, 2019 at 12:26

4 Answers 4

3

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')
>>> 
Sign up to request clarification or add additional context in comments.

3 Comments

Could you help me how to interpret this result?
Python counts from zero, your variables are named counting from one, so that you have to add 1 to the final array to map the answer to your result. ፨ That said, assume that you want to know which is the input array that has the maximum value in position (2,1): the numbers are, respectively 0.02688541, 0.2927128 and 0.07654601, hence the array with the maximum value is the second one. Now look at the final result, its (2,1) element is 1 → add 1 because Python counts from zero and you know that the array with the maximum value is the second one.
Thank you for the answer and the wonderful explanation..Now I can understand.
2

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]]

2 Comments

This looks simple. Can you also help me to implement to get result as the name of the array whose value is largest? For example, in this case [predict_prob1,predict_prob1,predict_prob1,predict_prob1]
This doesn't answer the OP's question, they are asking about how to determine which array has the maximum value in each element, not to find the maximum across all the arrays for each element. The result should be an array that represents an index of the array where the result came from (array1, array2, or array3), not the values from the array.
1

Assuming you want, for each row, the index of the array with the highest probability for class 0:

which = 0

np.stack([predict_prob1, predict_prob2, predict_prob3], axis=2)[:, which, :].argmax(axis=1)

Output:

array([0, 0, 0, 0])

For class 1:

array([2, 1, 1, 1])

Comments

0

You could use the fact that operands > and < yield a boolean mask of your arrays.

import numpy as np

predict_prob1 =np.array([[0.95602106, 0.04397894],
   [0.93332366, 0.06667634],
   [0.97311459, 0.02688541],
   [0.97323962, 0.02676038]])

predict_prob2 =np.array([[0.70425144, 0.29574856],
   [0.69751251, 0.30248749],
   [0.7072872 , 0.2927128 ],
   [0.68683139, 0.31316861]])

predict_prob3 =np.array([[0.56551921, 0.43448079],
   [0.93321106, 0.06678894],
   [0.92345399, 0.07654601],
   [0.88396842, 0.11603158]])

predict_prob = (predict_prob1>predict_prob2)*predict_prob1 + (predict_prob1<predict_prob2)*predict_prob2
predict_prob = (predict_prob>predict_prob3)*predict_prob + (predict_prob<predict_prob3)*predict_prob3

print(predict_prob)

The result is:

[[0.95602106 0.43448079]
 [0.93332366 0.30248749]
 [0.97311459 0.2927128 ]
 [0.97323962 0.31316861]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.