How do I randomly equalize unequal values?

偶尔善良 提交于 2019-12-02 06:59:32

Well, there is a technique called Inverse Weights, where you sample items inverse proportional to their previous appearance. Each time we sample a, b, c, d or e, we update their appearance numbers and recalculate probabilities. Simple python code, I sample numbers [0...4] as a, b, c, d, e and start with what you listed as appearances. After 100,000 samples they looks to be equidistributed

import numpy as np

n = np.array([100, 140, 200, 2, 1000])

for k in range(1, 100000):

    p  = (1.0 / n) # make probabilities inverse to weights
    p /= np.sum(p) # normalization

    a = np.random.choice(5, p = p) # sampling numbers in the range [0...5)

    n[a] += 1 # update weights

print(n)

Output

[20260 20194 20290 20305 20392]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!