Skip to content
Related Articles

Related Articles

How to get the minimum and maximum value of a given NumPy array along the second axis?

Improve Article
Save Article
Like Article
  • Last Updated : 02 Sep, 2020

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 library
import numpy as np
  
# Create a numpy array
arr = np.array([[0, 1],
                [2, 3]])
  
print("Given Array:\n",
     arr)
  
# find row-wise max values 
rslt1 = np.amax(arr, 1)
print("\nMaximum Value:",
      rslt1)
  
# find row-wise min values
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 library
import numpy as np
  
# Create a numpy array
arr = np.array([[10, 34, 45],
                [22, -3, 46], 
                [33, 4, 6]])
  
print("Given array:\n",
     arr)
  
# find row-wise max values 
rslt1 = np.amax(arr, 1)
print("\nMaximum value along the second axis:",
      rslt1)
  
# find row-wise min values 
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]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!