How to get the minimum and maximum value of a given NumPy array along the second axis?
Let’s see How to get the minimum and maximum value of a given NumPy array along the second axis. Here, the Second axis means row-wise.
We are using numpy.amax() and numpy.amin() function of NumPy to get the minimum and maximum values of an array along the second axis respectively.
numpy.amax(): This function returns maximum of an array or maximum along axis(if mentioned).
Syntax: numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
numpy.amin(): This function returns minimum of an array or minimum along axis(if mentioned).
Syntax: numpy.amin(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
Now, Let’s see an example:
Example 1:
Python3
import numpy as np
arr = np.array([[ 0 , 1 ],
[ 2 , 3 ]])
print ( "Given Array:\n" ,
arr)
rslt1 = np.amax(arr, 1 )
print ( "\nMaximum Value:" ,
rslt1)
rslt2 = np.amin(arr, 1 )
print ( "\nMinimum Value:" ,
rslt2)
|
Output:
Given Array:
[[0 1]
[2 3]]
Maximum Value: [1 3]
Minimum Value: [0 2]
Example 2:
Python3
import numpy as np
arr = np.array([[ 10 , 34 , 45 ],
[ 22 , - 3 , 46 ],
[ 33 , 4 , 6 ]])
print ( "Given array:\n" ,
arr)
rslt1 = np.amax(arr, 1 )
print ( "\nMaximum value along the second axis:" ,
rslt1)
rslt2 = np.amin(arr, 1 )
print ( "\nMinimum value along the second axis:" ,
rslt2)
|
Output:
Given array:
[[10 34 45]
[22 -3 46]
[33 4 6]]
Maximum value: [45 46 33]
Minimum value: [10 -3 4]
Last Updated :
02 Sep, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...