Hello and welcome to this tutorial on Numpy amax. In this tutorial, we will be learning about the NumPy amax() method and also seeing a lot of examples regarding the same. So let us begin!
Also read:Â NumPy fmax(): Element-wise maximum of array elements
What is NumPy amax?
The amax()
method in NumPy is a function that returns the maximum of the array elements. It can be the maximum of all the array elements, the maximum of the array elements along the rows, or the maximum of the array elements along the columns.
In Python, NaN means Not a Number. If in the input data, any one element is NaN, then the maximum value will be NaN as well.
We will see the examples for each of these in the upcoming section of this tutorial.
Syntax of NumPy amax
numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
Parameter | Description | Required/Optional |
a (array_like) | Input data. | Required |
axis | Axis along which the maximum of the array is to be calculated. It can be axis=0 or axis=1 or axis=None which implies that the maximum of the entire array is to be returned. If the axis is a tuple of ints, the maximum is calculated on all of the axes specified in the tuple instead of a single axis or all the axes as before. | Optional |
out | An alternative output array in which to place the result. It must have the same shape as the expected output. | Optional |
keepdims (bool) | If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. | Optional |
initial | The minimum value of an output element. | Optional |
where | Elements to compare for finding the maximum. | Optional |
Returns:
The maximum of a. If a is a scalar, the result is also a scalar, otherwise, it is an array.
Examples of Using Numpy Amax Function
Let’s get right into the different examples of using numpy.amax() function.
Maximum of a 1-dimensional array
import numpy as np
arr = [3, 6, 22, 10, 84]
# using amax method to compute the maximum
ans = np.amax(arr)
print("arr =", arr)
print("Maximum value =", ans)
Output:
arr = [3, 6, 22, 10, 84]
Maximum value = 84
Maximum of a 2-dimensional array
import numpy as np
arr = [[10, 36], [4, 16]]
# using amax method to compute the maximum
ans = np.amax(arr)
print("arr =", arr)
print("Maximum value =", ans)
Output:
arr = [[10, 36], [4, 16]]
Maximum value = 36
Since this is a 2-d array, it is first flattened row-wise like this: [10, 36, 4, 16], and then the maximum is calculated.
Maximum along axis=0
import numpy as np
arr = [[10, 36], [4, 16]]
# using amax method to compute the maximum
ans = np.amax(arr, axis=0)
print("arr =", arr)
print("Maximum value =", ans)
Output:
arr = [[10, 36], [4, 16]]
Maximum value = [10 36]
Here, the maximum is computed along the columns as:
ans[0] = max(arr[0][0], arr[1][0]) = max(10, 4) = 10
ans[1] = max(arr[0][1], arr[1][1]) = max(36, 16) = 36
Maximum along axis=1
import numpy as np
arr = [[10, 36], [4, 16]]
# using amax method to compute the maximum
ans = np.amax(arr, axis=1)
print("arr =", arr)
print("Maximum value =", ans)
Output:
arr = [[10, 36], [4, 16]]
Maximum value = [36 16]
In this case, the maximum is computed along the rows as follows:
ans[0] = max(arr[0][0], arr[0][1]) = max(10, 36) = 36
ans[1] = max(arr[1][0], arr[1][1]) = max(4, 16) = 16
Maximum of an array containing NaN
import numpy as np
arr = [3, 6, np.nan, 22, np.nan, 10, 84]
# using amax method to compute the maximum
ans = np.amax(arr)
print("arr =", arr)
print("Maximum value =", ans)
Output:
arr = [3, 6, nan, 22, nan, 10, 84]
Maximum value = nan
As stated earlier in this tutorial, if an array contains NaN, then its maximum value is also said to be NaN as can be seen in the above example.
Maximum of an array given an initial value
import numpy as np
arr = [3, 6, 22, 10, 84]
# using amax method to compute the maximum
ans = np.amax(arr, initial=100)
print("arr =", arr)
print("Maximum value =", ans)
Output:
arr = [3, 6, 22, 10, 84]
Maximum value = 100
Here, we have mentioned an initial value of 100. This value is compared with all the elements in the array to find the maximum.
Here, since 100 is the highest value among all the values, it is returned.
Maximum of an array using only selected elements
To find the maximum of only some selective values in the array, we can pass the where argument to the numpy.amax() function.
import numpy as np
arr = [3, 6, 22, 10, 84]
# using amax method to compute the maximum
ans = np.amax(arr, where=[False, False, True, True, False], initial=-1)
print("arr =", arr)
print("Maximum value =", ans)
Output:
arr = [3, 6, 22, 10, 84]
Maximum value = 22
Here in the where list only index 2 and 3 are True, the rest are False. This implies that the amax() method has to find the maximum of only elements at index 2 and 3 in arr and ignore the remaining elements.
Hence, max(22, 10) = 10 which is returned as the answer.
Conclusion
That’s all! In this tutorial, we learned about the Numpy amax method and practiced different types of examples using the same.
If you want to learn more about NumPy, feel free to go through our NumPy tutorials.