二维数组python上的最小值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44431412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Minimum value on a 2d array python
提问by cd123
I have an array of the following structure which is simplified for this question:
我有以下结构的数组,针对这个问题进行了简化:
8 2 3 4 5 6
3 6 6 7 2 6
3 8 5 1 2 9
6 4 2 7 8 3
I wish to find the minimum value in this 2D array however using the inbuilt min function returns a value error:
我希望在这个二维数组中找到最小值,但是使用内置的 min 函数返回一个值错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ValueError:包含多个元素的数组的真值不明确。使用 a.any() 或 a.all()
I have looked into the alternative of using np.argmin:
我研究了使用 np.argmin 的替代方法:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmin.html
https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmin.html
However it only evaluates along a single axis and returns the index of the minimum value along a single row/column whereas I wish to evaluate the whole array and return the lowest value not the indices.
但是,它仅沿单个轴计算并返回沿单个行/列的最小值的索引,而我希望计算整个数组并返回最小值而不是索引。
If it is possible to return the index values of the lowest item in the array then that would be preferable also as from that the lowest value can easily be found.
如果可以返回数组中最低项的索引值,那么这也是可取的,因为可以轻松找到最低值。
EDIT: Thanks to the comments below np.min
is the solution I was looking for and I was not aware of it existing so my answer is solved.
编辑:感谢下面的评论np.min
是我正在寻找的解决方案,我不知道它存在,所以我的答案得到了解决。
回答by alkasm
However it only evaluates along a single axis and returns the index of the minimum value along a single row/column whereas I wish to evaluate the whole array and return the lowest value not the indices.
但是,它仅沿单个轴计算并返回沿单个行/列的最小值的索引,而我希望计算整个数组并返回最小值而不是索引。
numpy.argmin
does not by default evaluate along a single axis, the default is to evaluate along the flattened matrix and it returns the linear index in the flattened array; from the numpy
docs that you linked:
numpy.argmin
默认情况下不沿单个轴计算,默认值是沿展平矩阵计算,并返回展平数组中的线性索引;从numpy
您链接的文档中:
By default, the index is into the flattened array, otherwise along the specified axis.
默认情况下,索引位于扁平数组中,否则沿指定轴。
Either way, use numpy.amin
or numpy.min
to return the minimum value, or equivalently for an array arrname
use arrname.min()
. As you mentioned, numpy.argmin
returns the indexof the minimum value (of course, you can then use this index to return the minimum value by indexing your array with it). You could also flatten into a single dimension array with arrname.flatten()
and pass that into the built-in min
function.
无论哪种方式,使用numpy.amin
或numpy.min
回的最小值,或等效为一个阵列arrname
使用arrname.min()
。正如您所提到的,numpy.argmin
返回最小值的索引(当然,您可以使用该索引通过索引数组来返回最小值)。您还可以将其展平为一维数组arrname.flatten()
并将其传递给内置min
函数。
The four following methods produce what you want.
以下四种方法产生你想要的。
import numpy as np
values = np.array([
[8,2,3,4,5,6],
[3,6,6,7,2,6],
[3,8,5,1,2,9],
[6,4,2,7,8,3]])
values.min() # = 1
np.min(values) # = 1
np.amin(values) # = 1
min(values.flatten()) # = 1
回答by Nick
Alternatively for a non-numpy solution:
或者对于非 numpy 解决方案:
>>> a = [[8,2,3,4,5,6],
... [3,6,6,7,2,6],
... [3,8,5,1,2,9],
... [6,4,2,7,8,3]]
>>> mymin = min([min(r) for r in a])
>>> mymin
1
回答by akash karothiya
You can use np.min()
您可以使用 np.min()
>>> arr = np.array([[8,2,3,4,5,6],
[3,6,6,7,2,6],
[3,8,5,1,2,9],
[6,4,2,7,8,3]])
>>> arr.min()
1