[英]Is there a function similar to np.random.choice that has a higher probability of choosing the lower values in the probability distribution?
我有一個對象數組,每個對象都有相應的概率,比如說
sample = [a, b, c, d, e, f]
probability = [0.1, 0.15, 0.6, 0.05, 0.03, 0.07]
對於我班上的大多數函數,這非常適合與 np.random.choice 一起使用,因為我想選擇概率最高的值。
但是,在其中一個函數上,我需要將其偏向概率較低的值(即比 c 更有可能在樣本中選擇 e 和 d)。
是否有一個函數可以做到這一點,或者我是否需要考慮將概率轉換為某種逆概率,例如
inverse_probability = [(1-x) for x in probability]
inverse_probability = [x/sum(inverse_probability) for x in probability]
然后在 np.random.choice 函數中使用它?
提前致謝!
我相信你可以使用泊松分布:
from numpy.random import poisson
index = poisson()
return sample[min(len(sample, index)]
有關此分發的更多詳細信息,請參閱Wikipedia 。
注意:這僅在您對如何完成優先級沒有任何要求時才有效。
這是一個簡單的解決方案,但應該可以解決您的問題:
sample = ['a', 'b', 'c', 'd', 'e', 'f']
probability = [0.1, 0.15, 0.6, 0.05, 0.03, 0.07]
np.random.choice(a=sample, p=probability)
解決方案1:
inverse_probability = [(1-x) for x in probability]
inverse_probability = [x/sum(inverse_probability) for x in inverse_probability]
np.random.choice(a=sample, p=inverse_probability)
解決方案2:
inverse_probability = [1/x for x in probability]
inverse_probability = [x/sum(inverse_probability) for x in inverse_probability]
np.random.choice(a=sample, p=inverse_probability)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.