Using numpy.max() function we can find the maximum element along a specified axis in a NumPy array. It is useful in many situations such as whenever we are doing error detection or quality control we can identify outliers in a dataset by finding the maximum value in a dataset. Also in the statistical analysis for calculating percentiles, finding the maximum value might play an important role. Another example could be when we compare two arrays element-wise for performing operations such as element-wise maximum we need to find the maximum value.
In this article, we’ll look at three ways to find the maximum element from the array using numpy.max() function. Let’s get started.
Introducing numpy.max() Function
The numpy.max() function in Python’s NumPy library is used to find the maximum value in a NumPy array. It returns the maximum value among all the elements in the array.
Syntax:
numpy.max(a, axis=None, out=None, keepdims=False, initial=<no value>, where=True)
Parameters:
- a: The input array from which we want to find the maximum value.
- axis (optional): If specified, it allows us to find the maximum value along a particular axis of the input array, resulting in a reduced dimension array.
- out (optional): An alternative output array in which to place the result.
- keepdims (optional): If True, the dimensions of the output array will match those of the input array with reduced dimensions set to 1.
- initial (optional): The initial value from which the maximum is determined.
- where (optional): A boolean array that specifies where to compute the maximum.
Calculate Maximum of Array in NumPy
There are three ways through which we can find the maximum element from the array by using numpy.max() function:
- Maximum element from the entire array
- Maximum element from array rows
- Maximum element from array columns
1. Maximum Element from the Entire Array
Here by using the numpy.max() function we will find the maximum element from the whole array.
Example 1:
import numpy as np
arr1 = np.array([21,22,23,24])
print(np.max(arr1))
In the code above to create an array and to use the max() function on an array, we first imported the Numpy library as np then we created a 1D array of values 21,22,23,24 and saved it into a variable called arr1. Then, at last, we applied the numpy.max() function on arr1.
Output:
In the output above we have seen that after applying the numpy.max() function on the entire array arr1 we got the result 24 which is the maximum value.
Example 2:
import numpy as np
arr2 = np.array([[21,22,23],[24,25,26]])
print(np.max(arr2))
In this example, we created a 2D array and applied the numpy.max() function on it.
Output:
2. Maximum Element from Array Rows
Here we will provide axis=1 as a parameter in the numpy.max() function for getting the maximum element from the array rows.
Example :
import numpy as np
arr2 = np.array([[21,22,23],[24,25,26]])
print(np.max(arr2,axis=1))
In the above code, we have passed axis=1 as an additional argument to the numpy.max() function to get the maximum element row-wise from arr2.
Output:
In the output above we got maximum elements row-wise in the 2D array arr2. The first row contains values 21,22,23 so we got 23 as the maximum and the second row contains values 24,25,26 so we got 26 as the maximum from the second row.
3. Maximum Element from Array Columns
Here we will provide axis=0 as a parameter in the numpy.max() function for getting the maximum element from the array columns.
Example :
import numpy as np
arr2 = np.array([[21,22,23],[24,25,26]])
print(np.max(arr2,axis=0))
In the above code, we have passed axis=0 as an additional argument to the numpy.max() function to get the maximum element column-wise from arr2.
Output:
In the output above we got maximum elements column-wise in the 2D array arr2.The first column contains the value 21,24 so we got 24 as the maximum, the second column contains the value 22,25 so we got 25 as the maximum and in last the third column contains the value 23,26 so we got 26 as the maximum.
Summary
The numpy.max() function is a versatile tool for finding the maximum value in arrays. In this tutorial, we have discussed numpy.max() function provided by Python’s NumPy library and also explored three ways to find the maximum element in the array using numpy.max() function with examples. After reading this tutorial, we hope you can easily find the maximum element from the array in Python.
Reference
https://stackoverflow.com/questions/26810112/what-does-numpy-max-function-do