0

I'm looking for a variation on the answer for this question

Given multiple arrays (3 or more), i need to construct a new array where each element identifies the input array which had the maximum value.

eg

array0 = np.array([1, 2, 3])
array1 = np.array([0, 3, 4])
array2 = np.array([-1, 1, 1])

the resulting array should be array([0,1,1])

np.maximum.reduce does not seem to work in the case. I could only come up with a brute force for loop

d = []
for i in range(len(array0)):
  k = 0
  if array1[i] > array0[i]:
    k = 1
  if array2[i] > array1[i] and array2[i] > array0[i]:
    k = 2
  d.append(k)

Is there a more pythonic/numpy way to do this?

1 Answer 1

1

Build a new array from the three and take the argmax along the first axis:

np.array([array0, array1, array2]).argmax(0)
# array([0, 1, 1])
Sign up to request clarification or add additional context in comments.

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.