How to find the maximum and minimum value in NumPy 1d-array?

Let’s see the various ways to find the maximum and minimum value in NumPy 1d-array.

Method 1: Using numpy.amax() and numpy.amin() functions of NumPy library.

  • numpy.amax(): This function returns maximum of an array or maximum along axis(if mentioned). 
  • numpy.amin(): This function returns minimum of an array or minimum along axis(if mentioned). 

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import numpy as np
  
# create a numpy 1d-array
array = np.array([1, 2, 3
                  0, -1, -2])
  
# find max element in an array
max_ele = np.amax(array)
  
# find man element in an array
min_ele = np.amin(array)
  
  
# show the outputs
print("Given Array:", array)
  
print("Max Element:", max_ele)
  
print("Min Element:", min_ele)

chevron_right


Output:



Given Array: [ 1  2  3  0 -1 -2]
Max Element: 3
Min Element: -2

Method 2: Using numpy.argmax() and numpy.argmin() function of NumPy library.

  • numpy.argmax(): This function returns indices of the max element of the array in a particular axis(if mentioned).
  • numpy.argmin(): This function returns indices of the min element of the array in a particular axis(if mentioned).

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import numpy as np
  
# create a numpy 1d-array
array = np.array([1, 2, 3,
                  0, -1, -2])
  
# find index of max element
# in an array
max_ele_index = np.argmax(array)
  
# find max element in an array
max_ele = array[max_ele_index]
  
# find index of min element
# in an array
min_ele_index = np.argmin(array)
  
# find min element in an array
min_ele = array[min_ele_index]
  
# show the outputs
print("Given Array:", array)
  
print("Max Element:", max_ele)
  
print("Min Element:", min_ele)

chevron_right


Output:

Given Array: [ 1  2  3  0 -1 -2]
Max Element: 3
Min Element: -2

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.