I'm trying to find an elegant way to find the max value in a two-dimensional array. for example for this array:
[0, 0, 1, 0, 0, 1] [0, 1, 0, 2, 0, 0][0, 0, 2, 0, 0, 1][0, 1, 0, 3, 0, 0][0, 0, 0, 0, 4, 0]
I would like to extract the value '4'. I thought of doing a max within max but I'm struggling in executing it.
In line 5, we use the amax() method to find the maximum value in the array. Then, we print the maximum value in line 6. From lines 8 to 12, we define a numpy 2D array. In lines 14 and 15, we use the amax() method to find the maximum across the row and column respectively.
Find max values along the axis in 2D numpy array | max in rows or columns: If we pass axis=0 in numpy. amax() then it returns an array containing max value for each column i.e. If we pass axis = 1 in numpy.
Another way to solve this problem is by using function numpy.amax()
>>> import numpy as np >>> arr = [0, 0, 1, 0, 0, 1] , [0, 1, 0, 2, 0, 0] , [0, 0, 2, 0, 0, 1] , [0, 1, 0, 3, 0, 0] , [0, 0, 0, 0, 4, 0] >>> np.amax(arr)
Max of max numbers (map(max, numbers)
yields 1, 2, 2, 3, 4):
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0] >>> map(max, numbers) <map object at 0x0000018E8FA237F0> >>> list(map(max, numbers)) # max numbers from each sublist [1, 2, 2, 3, 4] >>> max(map(max, numbers)) # max of those max-numbers 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With