def cp(target, given):
'Given is a one or two tuple and target is the letter following'
g = 0.0
g_and_t = 0.0
n = len(given)
for case, count in model.iteritems():
if case[:n] == given:
g += count
if case[n] == target:
g_and_t += count
return g_and_t / g if g else 0.0
print cp(target='r', given=('f', 'o'))
计算条件概率最简单的方法是循环计算模型中的情况,计算1)条件发生的情况,2)条件和目标字母出现的情况。条件概率是这两者的比率。在
相关问题 更多 >
编程相关推荐