np min in Python: get the minimum value from a NumPy array

np min (numpy.min()) returns the smallest value in a NumPy array, either across the whole array or along one axis.

Syntax and parameters of np min

Syntax:

numpy.min(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)

Parameters:

  • a: the input array or anything array-like (a list, a tuple, a nested list) that NumPy can convert.
  • axis (optional): the axis or tuple of axes to reduce over. Leave it as None and NumPy flattens the array first.
  • out (optional): an existing array to write the result into instead of allocating a new one.
  • keepdims (optional): keep the reduced axes in the output as dimensions of size one, so the result still broadcasts against the original array.
  • initial (optional): a starting value the minimum is compared against. Required if you want to call np min on an array that might be empty.
  • where (optional): a boolean array marking which values to include in the comparison.

Returns a scalar when axis is None, and an ndarray with one fewer dimension otherwise.

How do I get the minimum value from a NumPy array using np min?

Pass any array-like object into np.min() and it returns the smallest value as a single scalar, flattening the array first if it has more than one dimension.

import numpy as np

arr1 = np.array([1, 2, 3, 4])
print(np.min(arr1))
# Output: 1

arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(np.min(arr2))
# Output: 1

Minimum along rows and columns with axis

Pass axis=1 for the minimum of each row, and axis=0 for the minimum of each column.

import numpy as np

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

print(np.min(arr2, axis=1))
# Output: [1 4]

print(np.min(arr2, axis=0))
# Output: [1 2 3]

axis=1 collapses each row down to one number: the first row (1, 2, 3) has 1 as its lowest value, the second row (4, 5, 6) has 4. axis=0 does the same down each column instead. A reshape call built from np.arange is a quick way to generate test arrays for this without typing numbers by hand.

Keeping dimensions with keepdims

Reducing along an axis drops a dimension by default. Set keepdims=True to keep the result the same rank as the input, which matters when subtracting the minimum back from the original array.

import numpy as np

arr2 = np.array([[10, 17, 25], [15, 11, 22]])

print(np.min(arr2, axis=1, keepdims=True))
# Output: [[10]
#          [11]]

Without keepdims, the result has shape (2,), which will not broadcast against the original (2, 3) array. With keepdims=True, it has shape (2, 1), which subtracts cleanly.

How do I use initial and where to control the np min comparison?

initial sets a value the array’s minimum gets compared against, and it is the only way to call np min on an array that could be empty. where filters which values get looked at in the first place.

import numpy as np

array1 = np.array([10, 25, 17, 16, 14])
print(np.min(array1, initial=16))
# Output: 10

empty = np.array([])
print(np.min(empty, initial=5))
# Output: 5.0

initial does not behave like Python’s built-in min(), which only uses its default for genuinely empty inputs. In np min, initial is treated as one of the values being compared, so it can override a real minimum if set too high.

import numpy as np

print(np.min([6], initial=5))
# Output: 5
print(min([6], default=5))
# Output: 6

where narrows down which values get compared. Combine it with initial, since where needs a fallback for positions it skips.

import numpy as np

arr = np.array([[12, 25, 32], [47, 50, 36]])

print(np.min(arr, initial=50, where=(arr % 2 == 1)))
# Output: 25

print(np.min(arr, initial=50, where=(arr > 30)))
# Output: 32

A common pairing for this is np.where, useful for building the boolean mask itself when the condition is more involved than a single comparison.

Does np min ignore NaN values in a NumPy array?

No. NumPy treats NaN as contagious. If even one value in the array is NaN, np min returns NaN, no matter how small the rest of the values are.

import numpy as np

b = np.arange(5, dtype=float)
b[2] = np.nan

print(np.min(b))
# Output: nan

Two ways around it: filter with where, or switch to a dedicated function.

import numpy as np

print(np.min(b, where=~np.isnan(b), initial=10))
# Output: 0.0

print(np.nanmin(b))
# Output: 0.0

np.nanmin is the more direct option for datasets with occasional NaN gaps, and it takes the same axis argument as np min.

What is the difference between np min, np amin, np minimum and np fmin?

  • np.min() and np.amin(): identical functions. np.amin() is the older name, np.min() is the one most current code uses.
  • np.min(): reduces one array down to its smallest value, whole array or along an axis.
  • np.minimum(): compares two arrays element by element and returns the smaller value at each position. Takes two arrays, never an axis.
  • np.fmin(): does the same job as np.minimum() but ignores NaN instead of propagating it.

Everything here has a mirror image on the np max side, with np.max, np.amax, np.maximum and np.fmax following the identical pattern in reverse.

import numpy as np

a1 = np.array([2, 8, 125])
a2 = np.array([3, 3, 15])

print(np.minimum(a1, a2))
# Output: [2 3 15]

np.minimum() also broadcasts, so a smaller array or scalar can compare against a larger array as long as the shapes are compatible, the same broadcasting rule behind every element-wise NumPy operation.

import numpy as np

data = np.array([15, 35, 60, 25])
clipped = np.minimum(data, 30)
print(clipped)
# Output: [15 30 30 25]

How do I store the np min result in an existing array using out?

Pass an existing array to out and NumPy writes the result into it instead of allocating a new one, useful inside a loop that calls np min repeatedly on arrays of the same shape.

import numpy as np

array1 = np.array([[10, 17, 25], [15, 11, 22], [11, 19, 20]])
array2 = np.array([0, 0, 0])

np.min(array1, axis=0, out=array2)
print(array2)
# Output: [10 11 20]

out must already match the shape and dtype the result would otherwise get, or NumPy raises an error instead of quietly reshaping it.

How do I get the minimum and maximum values together in NumPy?

There is no single NumPy call that returns both in one pass over the array. Calling np.min() and np.max() separately walks the array twice.

import numpy as np

data = np.array([15, 35, 60, 25])

lowest = data.min()
highest = data.max()
spread = np.ptp(data)
print(lowest, highest, spread)
# Output: 15 60 45

np.ptp() (peak to peak) gives the difference between max and min in one call, useful when only the range matters. For a genuine single-pass min and max on large arrays, the third-party numpy-minmax package or a small Numba-compiled loop are the usual routes. For most scripts, two plain calls are simpler and fast enough.

Why does np min raise a ValueError on an empty array?

Calling np min on an empty array without initial raises a ValueError, since there is nothing to compare.

import numpy as np

empty = np.array([])
print(np.min(empty))
# Output: ValueError: zero-size array to reduction operation minimum which has no identity

Pass initial with a fallback value whenever the array might legitimately be empty, for example after filtering rows out of a dataset.

Why does np minimum raise a broadcasting error?

np.minimum() needs its two inputs to be the same shape or broadcastable to a common one. Shapes that cannot broadcast raise a ValueError instead of silently comparing the wrong values.

import numpy as np

array1 = np.array([1, 2, 3])
array2 = np.array([[4, 5, 6], [7, 8, 9]])

try:
    result = np.minimum(array1, array2)
except ValueError as e:
    print("Error:", e)
# Output: Error: operands could not be broadcast together with shapes (3,) (2,3)

array1 has shape (3,), array2 has shape (2, 3), and NumPy cannot line up 3 values against 2 rows of 3. Wrapping array1 as np.array([[1, 2, 3]]) gives it shape (1, 3), which broadcasts against (2, 3) cleanly.

How do I use np min in a practical data example?

A small dataset shows the same syntax at work outside an abstract array of integers.

import numpy as np

temperatures = np.array([23, 25, 19, 30, 21, 18, 24])
coldest_day = np.min(temperatures)
print("Coldest temperature this week:", coldest_day)
# Output: Coldest temperature this week: 18

Swap the hardcoded array for one loaded from a CSV of daily readings and the same call still works, since np min does not care where the array’s values came from.

Aditya Gupta
Aditya Gupta

Aditya Gupta is a founding member and editor at CodeForGeek. He first found his way into tech by reading articles, and now writes approachable guides to Node.js security, authentication, AI tools, coding agents, and web scraping.

Articles: 529