NumPy min()
The NumPy `min()` function is a part of Array Computation & Analysis tools, which is used to find the smallest value in an array. It is an efficient method for determining minimum values across various dimensions of a NumPy array.
Usage
The `min()` function is used when you need to identify the minimum value within an array or along a specified axis. It is particularly useful in data analysis and scientific computations to quickly assess data ranges.
numpy.min(array, axis=None, out=None, keepdims=<no value>)
In this syntax, `array` is the input data, `axis` specifies the dimension to reduce, `out` is an optional output array that should match the expected shape of the result, and `keepdims` retains reduced dimensions as size one.
Examples
1. Basic Minimum Value
import numpy as np
arr = np.array([3, 1, 4, 1, 5])
min_value = np.min(arr)
print(min_value)
This example finds the minimum value `1` in a one-dimensional array.
2. Minimum Value Along an Axis
import numpy as np
matrix = np.array([[3, 7, 5], [8, 4, 2], [6, 1, 9]])
min_by_row = np.min(matrix, axis=1)
print(min_by_row)
Here, the `min()` function computes the minimum value along each row, resulting in `[3, 2, 1]`.
3. Minimum with Output Array
import numpy as np
arr = np.array([[10, 20, 30], [40, 5, 60], [70, 80, 90]])
out_arr = np.empty((3,))
np.min(arr, axis=1, out=out_arr)
print(out_arr)
This example calculates the minimum along each row and stores the result in `out_arr`, yielding `[10, 5, 70]`.
4. Minimum with `keepdims`
import numpy as np
matrix = np.array([[3, 7, 5], [8, 4, 2], [6, 1, 9]])
min_by_column = np.min(matrix, axis=0, keepdims=True)
print(min_by_column)
This example demonstrates the use of `keepdims=True`, outputting `[[3, 1, 2]]` and retaining the dimensions of the result.
Tips and Best Practices
- Specify the axis wisely. Use the `axis` parameter to control which dimension to reduce, enhancing clarity and performance.
- Use `keepdims` for consistent array shapes. When necessary, use `keepdims=True` to maintain the original dimensions, which aids in subsequent computations.
- Predefine output arrays. For large datasets, predefining an output array with `out` can save memory allocations and optimize performance.
- Handle edge cases. Consider scenarios like empty arrays or arrays with `NaN` values. Note that `numpy.min()` will return `NaN` if any `NaN` values are present. Use `numpy.nanmin()` for robust handling by ignoring `NaN` values.
- Consider performance with large datasets. The function is optimized for large and multi-dimensional arrays, but understanding the data size and structure can help in choosing the best practices for performance efficiency.
Additional Notes
- Comparison with `numpy.amin()`. The function `numpy.amin()` is functionally equivalent to `numpy.min()`, providing the same results. The choice between them can depend on user preference or readability within your code.