是否存在快速方法或功能,而不是自动计算分布中项目的概率而不随机导入?
例如,请考虑以下分布(字典):
y = {"red":3, "blue":4, "green":2, "yellow":5}
有什么建议吗?
答案 0 :(得分:2)
对于频率:
y = {"red":3, "blue":4, "green":2, "yellow":5}
frequencies = {key:float(value)/sum(y.values()) for (key,value) in y.items()}
具有给定组合的概率是每个组合的概率乘以先前的概率。
combination = ["red", "green", "green"]
prob = 1. # initialized to 1
for ii in combination:
prob *= frequencies[ii]
print prob
0.00437317784257
听起来合理吗?