NumPy amin(): Find Minimum Values Along an Axis

Quick answer: np.amin reduces an array to its minimum value, either across the flattened input or along selected axes. The axis, keepdims, output dtype, and NaN policy determine whether the result broadcasts correctly and represents the data you intend to measure.

Python Pool infographic showing NumPy amin reducing an array globally or along selected axes with keepdims
amin reduces an array to minimum values; axis and keepdims determine the output shape, while NaN behavior should be chosen explicitly.

numpy.amin() returns the minimum value in an array, or the minimum values along a selected axis. The official NumPy amin documentation states that amin is an alias of numpy.min, so the same parameters and behavior apply.

Use np.amin() when you want a reduction: many values become one value, or one axis is reduced away. Use np.minimum() when you need element-wise comparison between two arrays.

The most important arguments are axis, keepdims, initial, and where. If axis is omitted, NumPy reduces the flattened array and returns one scalar. If an axis is provided, NumPy reduces only along that direction.

Before using the result in later calculations, confirm the output shape. Shape mistakes are more common than numeric mistakes when minimum reductions are added to a larger NumPy pipeline.

Also decide how missing or excluded values should behave before writing the reduction. A minimum value can drive thresholds, alerts, clipping, or normalization, so silent NaN propagation or an accidental empty slice can change downstream results.

Find The Minimum Of A Whole Array

With no axis argument, np.amin() looks across all elements and returns a scalar.

import numpy as np

values = np.array([[8, 3, 1], [6, 4, 9]])

result = np.amin(values)

print(result)

The minimum is 1. This is the same result you would get from np.min(values).

This mode is useful for quick checks such as the smallest error, lowest sensor reading, or minimum value in a generated array. amin returns the minimum value; NumPy argmin Guide: Find Minimum Indexes returns the index of that value and explains axes, ties, and keepdims.

When the array has more than one dimension, this global reduction ignores row and column structure. That is fine for a single overall minimum, but not for per-row or per-column summaries.

Use axis For Column Or Row Minimums

The axis argument controls the direction of the reduction. For a two-dimensional array, axis=0 reduces rows and returns one result per column. axis=1 reduces columns and returns one result per row.

import numpy as np

values = np.array([[8, 3, 1], [6, 4, 9]])

print(np.amin(values, axis=0))
print(np.amin(values, axis=1))

The first result gives column minimums. The second gives row minimums. If this feels backwards, print the array shape first and decide which dimension should disappear.

For reshaping arrays before a reduction, see the NumPy reshape guide.

For higher-dimensional arrays, the same idea applies. The selected axis is removed from the output unless keepdims is enabled.

Python Pool infographic showing a NumPy array, rows, columns, values, and a minimum reduction
Array values: A NumPy array, rows, columns, values, and a minimum reduction.

Keep Reduced Dimensions

keepdims=True leaves reduced axes in the result with length one. This helps when the result must broadcast back against the original array.

import numpy as np

values = np.array([[8, 3, 1], [6, 4, 9]])

row_min = np.amin(values, axis=1, keepdims=True)

print(row_min)
print(values - row_min)

Here each row is shifted by its own minimum. Keeping the reduced dimension makes the subtraction broadcast cleanly.

Without keepdims, you may need to reshape the result before combining it with the original array.

This is especially helpful in normalization code, where each row or column is adjusted by its own minimum and broadcasting must line up predictably.

Handle Empty Slices With initial

The initial argument supplies a starting value. NumPy documents it as required for empty-slice style reductions where no compared element may exist.

import numpy as np

values = np.array([], dtype=int)

safe_min = np.amin(values, initial=100)

print(safe_min)

This avoids an error for an empty input, but the chosen initial value becomes part of the calculation. Pick a value that is valid for the problem.

For normal non-empty arrays, avoid adding initial unless you deliberately want it included in the comparison.

A common safe pattern is to use initial only when the code path may receive empty input after filtering. Otherwise, let the data itself define the minimum.

Filter Values With where

where lets you select which elements participate in the reduction. Use it with initial so the operation still has a value to compare when all elements are excluded.

import numpy as np

values = np.array([8, 3, 1, 6, 4])
mask = values > 3

result = np.amin(values, where=mask, initial=10)

print(result)

The mask keeps values greater than three, so the result is 4. This is useful when invalid or out-of-scope values should not affect the minimum.

If the mask expresses missing data, also consider storing data in a form that makes missing values explicit before the reduction.

The mask must be broadcastable to the array shape. If the mask shape is wrong, fix that first instead of forcing the reduction with a reshaped mask you do not understand.

Python Pool infographic mapping NumPy amin through axis zero, axis one, and global minimum
Reduce axis: NumPy amin through axis zero, axis one, and global minimum.

NaN Values And nanmin

Regular np.amin() propagates NaN values. If at least one compared item is NaN, the result can become NaN. Use np.nanmin() when NaN values should be ignored.

import numpy as np

values = np.array([5.0, np.nan, 2.0])

print(np.amin(values))
print(np.nanmin(values))

Choose this behavior intentionally. Propagating NaN can reveal data problems, while ignoring NaN can be correct for datasets where missing values are expected. For locating positions of minimum values, use np.argmin(); for counting conditions before a reduction, see the NumPy count_nonzero documentation.

In short, use np.amin() or np.min() for reductions, choose axis based on the dimension you want to reduce, keep dimensions when broadcasting matters, and switch to np.nanmin() only when ignoring NaN values is part of the intended analysis.

Start With The Shape

axis=None reduces every element to one scalar. An integer axis reduces that dimension, while a tuple of axes reduces several dimensions. Inspect the input shape before deciding what the result should mean.

Python Pool infographic comparing keepdims, initial, where, out, dtype, and output shape
Reduction options: Keepdims, initial, where, out, dtype, and output shape.

Use keepdims For Broadcasting

keepdims=True leaves reduced axes as dimensions of length one. That often makes the minimum usable in arithmetic against the original array without manually reshaping the result.

Distinguish amin And minimum

amin is a reduction over an array. minimum is an element-wise operation between values. Choose the function based on whether you want one minimum per group or pairwise comparisons.

Handle Missing Values

A regular reduction can propagate NaN values. If NaN represents missing observations and should be excluded, use nanmin deliberately and verify that an all-NaN slice has a defined policy.

Python Pool infographic testing empty arrays, NaN, masked values, ties, and integer dtype
Minimum checks: Empty arrays, NaN, masked values, ties, and integer dtype.

Use out Carefully

The out array can avoid an allocation when its shape and dtype are compatible with the expected result. Treat it as a destination buffer and check the shape for every axis choice.

Test Reduction Contracts

Test flattened, row-wise, column-wise, multi-axis, empty, integer, floating-point, and NaN-containing inputs. Assert both values and result shape so a later axis change cannot silently pass.

The official NumPy amin reference documents the reduction parameters. Related Python Pool references include arrays and tests.

For related NumPy reductions, compare array shapes, axis tests, and median calculations when choosing a minimum operation.

Frequently Asked Questions

What does NumPy amin do?

np.amin returns the minimum of an array or the minimum values along one or more selected axes.

What is the difference between axis=None and an axis value?

axis=None reduces the flattened array to one value, while an axis value preserves the other dimensions in the result.

When should I use keepdims=True?

Use keepdims=True when the reduced result must retain singleton dimensions so it broadcasts cleanly against the original array.

How does amin handle NaN values?

amin can propagate NaN values; use a NaN-aware reduction such as np.nanmin when missing values should be ignored after you have confirmed that behavior is appropriate.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted