NumPy amax() function
In this tutorial, we will cover the numpy.amax()
function of the Numpy library.
This is a statistical function of the NumPy library that is used to return the maximum element of an array or maximum element along an axis.
To find the minimum value of an array we use the numpy.amin() function.
Syntax of numpy.amax()
:
The syntax required to use this function is as follows:
numpy.amax(a, axis, out, keepdims=<no value>, initial=<no value>, where=<no value>)
Parameters:
Now it's time to discuss the parameters of this function and these are as follows:
-
a
This parameter indicates the input data that is in the form of an array.
-
axis
It is an optional parameter indicating the Axis or axes along which to operate. The value of this parameter can be int
or a tuple of int
values and also has default value as None.
-
out
This optional parameter is used to indicate an alternative output array in which the result gets stored. The value of this parameter is in the form of an ndarray.
-
keepdims
With the help of this optional parameter(having boolean value), the result will broadcast correctly against the input array. If this option is set to True, the axes which are reduced are left in the result as dimensions with size one.
-
initial
This is a scalar and optional parameter used to indicate the maximum value of an output element.
-
where
This is an optional parameter used to indicate the elements to compare for the maximum value
Returned Values:
This function is used to return the maximum value of an array. If the axis is None, the result is a scalar value. If the axis is given, the result is an array of dimension a.ndim - 1
.
Example 1:
We will now cover a basic example using this function:
import numpy as np
a = np.array([[2,15,20],[80,43,31],[22,43,10]])
print("The original array:\n")
print(a)
print("\nThe maximum element among the array:",np.amax(a))
print("\nThe maximum element among the rows of array",np.amax(a,0))
print("\nThe maximum element among the columns of array",np.amax(a,1))
Output:
Example 2:
Another code example is as follows:
a = np.arange(9).reshape((3,3))
print("The Array is :")
print(a)
print("Maximum element in the array is:",np.amax(a))
print("Maximum element along the first axis of array is:",np.amax(a, axis=0))
print("Maximum element along the second axis of array is:",np.amax(a, axis=1))
Output:
Summary
In this tutorial, we covered the numpy.amax()
statistical function of Numpy library with its syntax, parameters, and returned values, along with a few code examples.