How to return all the minimum indices in numpy

后端 未结 4 451
深忆病人
深忆病人 2020-12-03 00:51

I am a little bit confused reading the documentation of argmin function in numpy. It looks like it should do the job:

Reading this

Return the

4条回答
  •  死守一世寂寞
    2020-12-03 01:38

    That documentation makes more sense when you think about multidimensional arrays.

    >>> x = numpy.array([[0, 1],
    ...                  [3, 2]])
    >>> x.argmin(axis=0)
    array([0, 0])
    >>> x.argmin(axis=1)
    array([0, 1])
    

    With an axis specified, argmin takes one-dimensional subarrays along the given axis and returns the first index of each subarray's minimum value. It doesn't return all indices of a single minimum value.

    To get all indices of the minimum value, you could do

    numpy.where(x == x.min())
    

提交回复
热议问题