np.argmin 的作用
时间: 2024-08-16 15:08:42 浏览: 61
`np.argmin()` 是 NumPy 库中的一个函数,它返回数组中最小元素的索引位置。当你有一个一维数组时,这个函数会查找并返回使得数组值最小的那个元素对应的整数索引。如果数组中有多个相同的最小值,`argmin` 将返回第一个找到的最小值的索引。
例如,如果你有这样一个数组 `arr = [4, 7, 1, 9, 3]`,`np.argmin(arr)` 将返回 3,因为数字 1 是数组中的最小值,它的索引是 3。
相关问题
python np.argmin
`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)
```
def order_points(pts): # 一共4个坐标点 rect = np.zeros((4, 2), dtype = "float32") # 按顺序找到对应坐标0123分别是 左上,右上,右下,左下 # 计算左上,右下 s = pts.sum(axis = 1) rect[0] = pts[np.argmin(s)] rect[2] = pts[np.argmax(s)] # 计算右上和左下 diff = np.diff(pts, axis = 1) rect[1] = pts[np.argmin(diff)] rect[3] = pts[np.argmax(diff)] return rect
这是一个Python函数,接受一个形如$[(x_1, y_1), (x_2, y_2), (x_3, y_3), (x_4, y_4)]$的四个点坐标列表pts,然后返回一个4行2列的二维数组rect,其中每一行表示一个坐标点的x和y坐标。具体实现是通过计算四个点的坐标和以及坐标差之和,找出这四个点的相对位置关系,从而得到它们对应到rect数组中的位置。其中,左上角的点对应rect数组的第一行,右上角的点对应第二行,右下角的点对应第三行,左下角的点对应第四行。
阅读全文