在numpy.argmax函数中,多个max元素之间的tie tie断开,以便返回第一个元素.是否有随机化打破平局的功能,以便所有最大数字都有相同的被选中机会?
以下是直接来自numpy.argmax文档的示例.
>>> b = np.arange(6)
>>> b[1] = 5
>>> b
array([0, 5, 2, 3, 4, 5])
>>> np.argmax(b) # Only the first occurrence is returned.
1
Run Code Online (Sandbox Code Playgroud)
我正在寻找方法,以便以相同的概率返回列表中的第1和第5个元素.
谢谢!
Div*_*kar 25
使用np.random.choice-
np.random.choice(np.flatnonzero(b == b.max()))
Run Code Online (Sandbox Code Playgroud)
让我们验证一个有三个最大候选者的数组 -
In [298]: b
Out[298]: array([0, 5, 2, 5, 4, 5])
In [299]: c=[np.random.choice(np.flatnonzero(b == b.max())) for i in range(100000)]
In [300]: np.bincount(c)
Out[300]: array([ 0, 33180, 0, 33611, 0, 33209])
Run Code Online (Sandbox Code Playgroud)
在多维数组的情况下,choice将无法正常工作.
另一种选择是
def randargmax(b,**kw):
""" a random tie-breaking argmax"""
return np.argmax(np.random.random(b.shape) * (b==b.max()), **kw)
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因生成随机浮点数比其他方法慢,random.random可以用其他方法替换.
由于答案可能并不明显,因此它的工作原理如下:
| 归档时间: |
|
| 查看次数: |
7511 次 |
| 最近记录: |