在(行、列)格式中的Numpy最小值

时间:2021-05-08 20:36:20

How can I know the (row, column) index of the minimum of a numpy array/matrix?

如何知道numpy数组/矩阵的最小索引(行、列)?

For example, if A = array([[1, 2], [3, 0]]), I want to get (1, 1)

例如,如果A =数组([1,2],[3,0]),我想要得到(1,1)

Thanks!

谢谢!

2 个解决方案

#1


56  

Use unravel_index:

使用unravel_index:

numpy.unravel_index(A.argmin(), A.shape)

#2


1  

Another simple solution is

另一个简单的解决方案是

ri, ci = A.argmin()//A.shape[0], A.argmin()%A.shape[1]

As numpy.argmin returns the index reading in row-major order

numpy。argmin以行主顺序返回索引读取。

#1


56  

Use unravel_index:

使用unravel_index:

numpy.unravel_index(A.argmin(), A.shape)

#2


1  

Another simple solution is

另一个简单的解决方案是

ri, ci = A.argmin()//A.shape[0], A.argmin()%A.shape[1]

As numpy.argmin returns the index reading in row-major order

numpy。argmin以行主顺序返回索引读取。