python np.argmin
时间: 2024-04-21 20:04:52 浏览: 178
`np.argmin` is a NumPy function that returns the indices of the minimum values along an axis of an array.
Syntax: `np.argmin(a, axis=None, out=None)`
Parameters:
- `a`: input array
- `axis`: the axis along which to find the minimum value. If not specified, the entire array will be flattened and the index of the minimum value will be returned.
- `out`: optional output array in which to place the result. If provided, the shape must be identical to the output shape.
Returns:
- If `axis` is None, a single integer indicating the index of the minimum value in the flattened array is returned.
- If `axis` is specified, an array of indices is returned, with the same shape as `a` except for the specified axis, which is replaced by a scalar index.
Example:
```python
import numpy as np
arr = np.array([[4, 2, 1],
[3, 5, 6]])
# Returns the index of the minimum value in the flattened array
print(np.argmin(arr)) # Output: 2 (index of value 1)
# Returns the index of the minimum value along axis 0 (column-wise)
print(np.argmin(arr, axis=0)) # Output: [1 0 0] (indices of minimum values in each column)
# Returns the index of the minimum value along axis 1 (row-wise)
print(np.argmin(arr, axis=1)) # Output: [2 0] (indices of minimum values in each row)
```
阅读全文