Break_tie_randomly
np.argmax()返回 arr 中第一个最大元素的下标,但是有时候需要在最大元素中随机选择一个.
1 | np.random.choice(np.flatnonzero(b == b.max())) |
b==b.max()will return an array of booleans, with values oftruewhere items are max and values offalsefor other items.flatnonzero()will do two things: ignore thefalsevalues (nonzero part) then return indices oftruevalues. In other words, you get an array with indices of items matching the max value.- Finally, you pick a random index from the array.
发现还是很慢,不如手写版本
1 | def random_argmax(value_list): |