在Python 2.7

时间:2016-03-17 15:22:52

标签: python statistics nlp

我有以下代码:

for c in word:
    bigram = prev_char+c
    prev_char = c
    prob_es = prob_es*(float(char_value_es.get(bigram, 0)))
在这种情况下,概率是一个单词中每个字母的乘法。 如何将其转换为以下公式?

prob_es = SUM(log2 p(float(char_value_es.get(bigram,0)))

prob_ca = math.log(prob_ca,2)+math.log((float(char_value_ca.get(bigram, 0)),2))

错误类型是:

prob_ca = math.log(prob_ca,2)+math.log((float(char_value_ca.get(bigram, 0)),2))
TypeError: a float is required

1 个答案:

答案 0 :(得分:1)

9.2.2. Power and logarithmic functions

您应该在进入循环之前初始化您的bigram和prob_es值。

您的原始代码是

for c in word:
    bigram = prev_char+c
    prev_char = c
    prob_es *= float(char_value_es.get(bigram, 0))

将其替换为

import math
bigram = ''
log_es = 0
for c in word:
    bigram = prev_char+c
    prev_char = c
    log_es += math.log(float(char_value_es.get(bigram, 0)))
prob_es = math.exp(log_es)