我有一个二维的NumPy数组。我知道如何获得轴上的最大值:
>>> a = array([[1,2,3],[4,3,1]])
>>> amax(a,axis=0)
array([4, 3, 3])
如何获得最大元素的索引?我想把array([1,1,0])
作为输出。
发布于 2011-03-29 15:39:44
>>> a.argmax(axis=0)
array([1, 1, 0])
发布于 2012-11-24 04:52:54
>>> import numpy as np
>>> a = np.array([[1,2,3],[4,3,1]])
>>> i,j = np.unravel_index(a.argmax(), a.shape)
>>> a[i,j]
4
发布于 2014-05-09 06:58:16
argmax()
将只返回每行的第一个匹配项。http://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html
如果您需要对成形数组执行此操作,则此方法比unravel
更有效
import numpy as np
a = np.array([[1,2,3], [4,3,1]]) # Can be of any shape
indices = np.where(a == a.max())
您还可以更改您的条件:
indices = np.where(a >= 1.5)
上面给出了您所要求的表单的结果。或者,可以通过以下方式转换为x,y坐标列表:
x_y_coords = zip(indices[0], indices[1])
https://stackoverflow.com/questions/5469286
复制相似问题