Answer a question

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 indices of the minimum values along an axis.

I might assume that

np.argmin([5, 3, 2, 1, 1, 1, 6, 1])

will return an array of all indices: which will be [3, 4, 5, 7]

But instead of this it returns only 3. Where is the catch, or what should I do to get my result?

Answers

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())
Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐