Skip to main content

Python NumPy: How to Find the Maximum and Minimum Element in a NumPy Array in Python

NumPy is the go-to library for numerical computing in Python, offering powerful tools for creating and manipulating arrays. One of the most common operations when working with data is finding the maximum and minimum values within an array.

This guide covers how to use NumPy's built-in functions to find extremes in 1D arrays, 2D arrays, along specific rows or columns, and even across multiple arrays.

Prerequisites

NumPy is not included with Python's standard library, so you need to install it first:

pip install numpy

Then import it in your script. By convention, NumPy is imported with the alias np:

import numpy as np

Finding Max and Min in a 1D Array

The simplest case is finding the maximum and minimum values in a one-dimensional array. NumPy provides two straightforward functions for this:

  • np.max(arr) - returns the maximum value.
  • np.min(arr) - returns the minimum value.
import numpy as np

arr = np.array([1, 5, 4, 8, 3, 7])

max_element = np.max(arr)
min_element = np.min(arr)

print("Maximum element:", max_element)
print("Minimum element:", min_element)

Output:

Maximum element: 8
Minimum element: 1
note

np.max() and np.min() work only with numeric data types (integers and floats). Passing an array of strings will raise a TypeError or produce unexpected results.

Finding Max and Min in a 2D Array

When working with multidimensional arrays, np.max() and np.min() return the global maximum and minimum across all elements by default.

import numpy as np

arr = np.array([
[11, 2, 3],
[4, 5, 16],
[7, 81, 22]
])

max_element = np.max(arr)
min_element = np.min(arr)

print("Maximum element:", max_element)
print("Minimum element:", min_element)

Output:

Maximum element: 81
Minimum element: 2

Finding Max and Min Along Rows or Columns

In many data analysis tasks, you don't want the overall extreme - you need the maximum or minimum per row or per column. This is achieved using the axis parameter:

  • axis=0 - operates along rows (i.e., returns one result per column).
  • axis=1 - operates along columns (i.e., returns one result per row).
import numpy as np

arr = np.array([
[11, 2, 3],
[4, 5, 16],
[7, 81, 22]
])

# Per column (axis=0)
max_per_column = np.max(arr, axis=0)
min_per_column = np.min(arr, axis=0)

# Per row (axis=1)
max_per_row = np.max(arr, axis=1)
min_per_row = np.min(arr, axis=1)

print("Max per column:", max_per_column)
print("Min per column:", min_per_column)
print("Max per row:", max_per_row)
print("Min per row:", min_per_row)

Output:

Max per column: [11 81 22]
Min per column: [4 2 3]
Max per row: [11 16 81]
Min per row: [2 4 7]
Understanding the axis parameter

Think of axis as the dimension that gets collapsed:

  • axis=0 collapses rows → you get a result for each column.
  • axis=1 collapses columns → you get a result for each row.

A helpful way to remember: axis=0 moves vertically down the rows, axis=1 moves horizontally across columns.

Element-Wise Comparison Between Two Arrays

When you have two arrays of the same shape, you can find the element-wise maximum or minimum using np.maximum() and np.minimum(). These functions compare corresponding elements and return a new array.

import numpy as np

a = np.array([1, 4, 6, 8, 9])
b = np.array([5, 7, 3, 9, 22])

element_wise_max = np.maximum(a, b)
element_wise_min = np.minimum(a, b)

print("Element-wise max:", element_wise_max)
print("Element-wise min:", element_wise_min)

Output:

Element-wise max: [ 5  7  6  9 22]
Element-wise min: [1 4 3 8 9]
Don't confuse np.max() with np.maximum()

These two functions serve different purposes:

  • np.max(arr) - finds the single maximum value within one array.
  • np.maximum(a, b) - performs element-wise comparison between two arrays and returns an array of the same shape.
import numpy as np

a = np.array([1, 4, 6])
b = np.array([5, 2, 3])

# This returns a single value
print(np.max(a)) # Output: 6

# This returns an array
print(np.maximum(a, b)) # Output: [5 4 6]

Output:

6
[5 4 6]

The same distinction applies to np.min() vs. np.minimum().

Finding the Index of the Max or Min Value

Sometimes you don't just need the value - you need to know where it is. Use np.argmax() and np.argmin() to get the index of the maximum and minimum elements.

import numpy as np

arr = np.array([1, 5, 4, 8, 3, 7])

max_index = np.argmax(arr)
min_index = np.argmin(arr)

print(f"Max value {arr[max_index]} is at index {max_index}")
print(f"Min value {arr[min_index]} is at index {min_index}")

Output:

Max value 8 is at index 3
Min value 1 is at index 0

For 2D arrays, np.argmax() returns the index in the flattened array by default. Use the axis parameter or np.unravel_index() to get the row and column position:

import numpy as np

arr = np.array([
[11, 2, 3],
[4, 5, 16],
[7, 81, 22]
])

flat_index = np.argmax(arr)
row, col = np.unravel_index(flat_index, arr.shape)

print(f"Max value {arr[row, col]} is at position ({row}, {col})")

Output:

Max value 81 is at position (2, 1)

Quick Reference Summary

FunctionPurposeReturns
np.max(arr)Maximum value in an arraySingle value
np.min(arr)Minimum value in an arraySingle value
np.max(arr, axis=0)Max per columnArray
np.max(arr, axis=1)Max per rowArray
np.maximum(a, b)Element-wise max of two arraysArray
np.minimum(a, b)Element-wise min of two arraysArray
np.argmax(arr)Index of the maximum valueInteger
np.argmin(arr)Index of the minimum valueInteger

Conclusion

NumPy makes finding maximum and minimum values intuitive and efficient, whether you're working with simple 1D arrays or complex multidimensional data.

  • Use np.max() and np.min() for global extremes, add the axis parameter to operate along rows or columns
  • Use np.maximum() / np.minimum() for element-wise comparisons between arrays.
  • When you also need the position of an extreme value, np.argmax() and np.argmin() have you covered.