Get Minima & Maxima of NumPy Array in Python (3 Examples)

This post illustrates how to find the maximum and minimum of a NumPy array in the Python programming language.

Preparing the Examples

import numpy                         # Load numpy
x = np.array([[8, 2, 1, 7, 7, 5],    # Constructing a NumPy array in Python
              [4, 10, 5, 9, 1, 4]])
print(x)
# [[ 8  2  1  7  7  5]
#  [ 4 10  5  9  1  4]]

Example 1: Calculate Minimum & Maximum of All Array Values in Python

print(np.min(x))                     # Computing the minimum of all values
# 1
print(np.max(x))                     # Computing the maximum of all values
# 10

Example 2: Calculate Minimum & Maximum by Array Rows in Python

print(np.min(x, axis = 1))           # Computing the minimum by rows
# [1 1]
print(np.max(x, axis = 1))           # Computing the maximum by rows
# [ 8 10]

Example 3: Calculate Minimum & Maximum by Array Columns in Python

print(np.min(x, axis = 0))           # Computing the minimum by columns
# [4 2 1 7 1 4]
print(np.max(x, axis = 0))           # Computing the maximum by columns
# [ 8 10  5  9  7  5]

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top