October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Python Program to Find the Smallest Element in a NumPy Array

A concise NumPy example for finding the global minimum, getting per-row or per-column minima, and choosing the right function for indices and NaNs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use np.min(array) to get the smallest value across a NumPy array. By default, NumPy reduces the full array to one value; add an axis only when you want a result for each row or column.

Find the smallest value in an array

Import NumPy, create an array, and call np.min():

import numpy as np

arr = np.array([8, 3, 12, -2, 5])
smallest = np.min(arr)
print(smallest)  # -2

The equivalent array method is arr.min(). With the default axis=None, NumPy reduces across the whole input and returns one value. See the NumPy minimum documentation.

Get a minimum for each row or column

For a two-dimensional array, omit axis for one global minimum. Set axis=0 to reduce the rows at each column position, or axis=1 to reduce the columns within each row:

matrix = np.array([[8, 3, 12], [4, -2, 5]])

print(np.min(matrix))          # -2
print(np.min(matrix, axis=0))  # [ 4 -2  5]
print(np.min(matrix, axis=1))  # [ 3 -2]

Get the position of the minimum instead

np.argmin(arr) returns an index, not the minimum value. Use it when you need the position, then index the array to retrieve the value:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
arr = np.array([8, 3, 12, -2, 5])

index = np.argmin(arr)
value = arr[index]
print(index)  # 3
print(value)  # -2

NumPy documents argmin as returning indices of minimum values. Use min or np.min when the requested result is the value itself.

Handle NaNs, infinities, and empty arrays

NaN values

np.min() propagates NaNs: a reduction slice containing a NaN can produce NaN. If the intended behavior is to ignore NaN values, use np.nanmin() instead:

arr = np.array([8.0, np.nan, -2.0])

print(np.min(arr))     # nan
print(np.nanmin(arr))  # -2.0

np.nanmin() ignores NaNs, not infinities. If a slice contains only NaNs, NumPy returns NaN and raises a RuntimeWarning. See the NumPy nanmin documentation.

Positive and negative infinity

NumPy follows IEEE floating-point ordering: positive infinity behaves like a large value and negative infinity like a small one. Therefore, -np.inf can be the minimum. The NaN-ignoring behavior of np.nanmin() does not remove infinities.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Empty arrays

An empty array has no ordinary minimum. If a meaningful candidate value exists for your problem, the initial parameter allows reduction of an empty slice. That initial value also participates in the comparison for nonempty input, so it can become the result if it is smaller than every array value. Use it only when that behavior matches your intended rule. Otherwise, check that the array is nonempty before calling min. Details are in the NumPy 2.0 minimum documentation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.