2

I have a three dimensional numpy array. What is the fastest way to get a 3D array that has the largest item of each of final axis of the array without writing a loop.(I will later use CuPy with the same syntax, and loops would take away the GPU parallelism and the speed which is the most important factor here.)

Getting the indexes for largest items is easy:

>>> arr = np.array(
        [[[ 6, -2, -6, -5],
        [ 1, 12,  3,  9],
        [21,  7,  9,  8]],

       [[15, 12, 20, 12],
        [17, 15, 17, 23],
        [22, 18, 27, 32]]])
>>> indexes = arr.argmax(axis=2, keepdims=True)
>>> indexes
array([[[0],
        [1],
        [0]],

       [[2],
        [3],
        [3]]])

but how to use those indexes to get the chosen values from arr? All the ways I have tried either produce errors (such as arr[indexes]), or wrong results. What I would like to get in this example is

array([[[6],
        [12],
        [21]],

       [[20],
        [23],
        [32]]])
5
  • argmax docs suggest you see take_along_axis numpy.org/doc/stable/reference/generated/… Commented Aug 9, 2024 at 4:04
  • Thank you! I assumed there would be a simple answer but couldn't find it among so many numpy functions Commented Aug 9, 2024 at 7:54
  • Do you really need the intermediate step of getting the indices of the largest elements? If you are only interested in the largest elements themselves, you could use np.amax(), as proposed in the answer of "tax evader" below. Also you might want to give credit to the ones that helped you by (1) upvoting answers that you find helpful and (2) accepting the answer that solves your problem. Commented Aug 9, 2024 at 8:53
  • 1
    I will definitely upvote those who helped, when I have first acquired enough points to vote (=15 reputation points), currently I am at 11 points.... In my specific case I need the information of the indeces, yet without such need the np.amax() would indeed be better. I was about to make a comment on that after "tax evader"' answer, but then I read the guide that supposedly "Comments are used to ask for clarification or to point out problems in the post." This being my second post ever, I played safe and didn't make such other type of comment yet. Commented Aug 9, 2024 at 9:49
  • @Mikael Thanks for the clarification. Sorry I didn't remember that there was a minimum number of points to vote. In any case: welcome to Stack Overflow! Commented Aug 9, 2024 at 10:07

2 Answers 2

2

As pointed by hpaulj, you can use take_along_axis

>>>arr = np.array(
        [[[ 6, -2, -6, -5],
        [ 1, 12,  3,  9],
        [21,  7,  9,  8]],

       [[15, 12, 20, 12],
        [17, 15, 17, 23],
        [22, 18, 27, 32]]])
>>>indexes = arr.argmax(axis=2, keepdims=True)
>>>np.take_along_axis(arr , indexes , axis=2)

array([[[6],
        [12],
        [21]],

       [[20],
        [23],
        [32]]])
Sign up to request clarification or add additional context in comments.

Comments

2

I think you can use np.amax for that

import numpy as np

arr = np.array(
        [[[ 6, -2, -6, -5],
        [ 1, 12,  3,  9],
        [21,  7,  9,  8]],

       [[15, 12, 20, 12],
        [17, 15, 17, 23],
        [22, 18, 27, 32]]])

max_arr = np.amax(arr, axis=2, keepdims=True)
print(max_arr)

Output

[[[ 6]
  [12]
  [21]]

 [[20]
  [23]
  [32]]]

1 Comment

Thank you! If the information of what indeces were the largest is not needed, this answer would be ideal.

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.