如何以numpy返回所有最小索引 [英] How to return all the minimum indices in numpy

查看:111
本文介绍了如何以numpy返回所有最小索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 argmin函数的文档有点困惑在numpy中. 看起来应该可以完成工作:

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

阅读此书

返回沿轴的最小值的索引.

Return the indices of the minimum values along an axis.

我可能会认为

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

将返回所有索引的数组:将为[3, 4, 5, 7]

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

但是,它只返回3而不是它.渔获物在哪里,或者我应该怎么做才能得到结果?

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

推荐答案

当您考虑多维数组时,该文档更有意义.

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])

在指定轴的情况下,argmin沿给定轴获取一维子数组,并返回每个子数组最小值的第一个索引.它不会返回单个最小值的所有索引.

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

这篇关于如何以numpy返回所有最小索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆