1

I have such an array:

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).reshape(2,2,4)

array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]]])

And I want to get to get the max for each of these sub-arrays:

arr[:,:,-1]

array([[ 4,  8],
       [12, 16]])

So I would want each of these sub-arrays converted to their max value. As result:

array([8, 16)]

How would I be able to do that without iterating?

2 Answers 2

1

Use max in numpy:

arr[:,:,-1].max(-1)

-1 is your last dimension.

printing it:

  [8, 16]
Sign up to request clarification or add additional context in comments.

5 Comments

The question is, how to get the max aggregation out of these arrays. I.e. to end up with [8, 16]
It is a bit confusing. are you looking for arr[:,:,-1].max(-1)? It will give you [8,16]. but if you mean something else by aggregation, could you please explain more how you get the output? Thank you
Another possible thing that results in the same output is arr.max(-1).max(-1) if you were looking for this.
Sorry just realized myself that my question originally wasn't clear. Specified it. But your comment gave me the answer I was looking for. Thanks.
I will edit the post. Please feel free to accept it. Thank you.
1

IIUC you want the output to be [8,16] which is a max of the sub-arrays arr[:,:,-1] that you have computed. Try this -


arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).reshape(2,2,4)

np.max(arr[:,:,-1],-1)
array([ 8, 16])

3 Comments

hmm. Isn’t this an alias of posted answer?
As i see it, your answer was edited after mine to reflect the righr answer. Previous answer posted by you was not the complete solution.
The question was not clear to me originally. However, I also posted it in my comments beforehand. Nonetheless, it could be good to have the function version of it for learners too.

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.