Shirink the numpy array with max

Question:

I have numpy array such as np.array([2,2])

[[1,9],
[7,3]]

I want to get the max of third demention and make this into one dimension.

then numpy.array should be like this [9,7]

I think I can do this with for loop and make another numpy.

However it looks ackword, is there any good way to do this ?

Asked By: whitebear

||

Answers:

amax function (alias is np.max)

import numpy as np

a = np.array([[1,9],
[7,3]])

np.amax(a, axis=1)  

# array([9, 7])
Answered By: svfat

Use max with specific axis. In this example axis is 1.

import numpy as np

arr = np.array([[1,9],
                [7,3]])

arr_max = np.max(arr, axis=1)
print(arr_max)

Output:

[9 7]
Answered By: Ali Ent

numpy.max is just an alias for numpy.amax. This function only works on a single input array and finds the value of maximum element in that entire array (returning a scalar). Alternatively, it takes an axis argument and will find the maximum value along an axis of the input array (returning a new array).

import numpy

np_array = numpy.array([[1,9],
                [7,3]])

max_array = numpy.max(np_array, axis=1)

print(max_array.shape)
print(max_array)

Output:

(2,)
[9 7]
Answered By: Rafat Hasan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.