Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Machine learning 用朴素贝叶斯方法求类的概率_Machine Learning_Classification_Probability_Text Classification_Naivebayes - Fatal编程技术网

Machine learning 用朴素贝叶斯方法求类的概率

Machine learning 用朴素贝叶斯方法求类的概率,machine-learning,classification,probability,text-classification,naivebayes,Machine Learning,Classification,Probability,Text Classification,Naivebayes,我试图用两个类对输入进行分类,下面是代码dino和crypto是两类: for w, cnt in list(counts.items()): #count is dict with word and it's count value p_word = vocab[w] / sum(vocab.values()) p_w_given_dino = (word_counts["dino"].get(w, 0.0) + 1) / (sum(word_counts["dino"].v

我试图用两个类对输入进行分类,下面是代码
dino
crypto
是两类:

for w, cnt in list(counts.items()): #count is dict with word and it's count value
    p_word = vocab[w] / sum(vocab.values()) 
    p_w_given_dino = (word_counts["dino"].get(w, 0.0) + 1) / (sum(word_counts["dino"].values()) + v) 
    p_w_given_crypto = (word_counts["crypto"].get(w, 0.0) + 1) / (sum(word_counts["crypto"].values()) + v)

    log_prob_dino += math.log(cnt * p_w_given_dino / p_word)
    log_prob_crypto += math.log(cnt * p_w_given_crypto / p_word)

print("Score(dino)  :", math.exp(log_prob_dino + math.log(prior_dino)))
print("Score(crypto):", math.exp(log_prob_crypto + math.log(prior_crypto)))
另一种方法是:

prior_dino = (priors["dino"] / sum(priors.values()))
prior_crypto = (priors["crypto"] / sum(priors.values()))
for w, cnt in list(counts.items()):
    p_word = vocab[w] / sum(vocab.values())
    p_w_given_dino = (word_counts["dino"].get(w, 0.0) + 1) / (sum(word_counts["dino"].values()) + v) 
    p_w_given_crypto = (word_counts["crypto"].get(w, 0.0) + 1) / (sum(word_counts["crypto"].values()) + v)
    prob_dino *= p_w_given_dino
    prob_crypto *= p_w_given_crypto
t_prior_dino = prob_dino * prior_dino
t_prior_crypto = prob_crypto * prior_crypto
在第二种方法中,我得到了非常小的值


哪一个是正确的,或者两者都是正确的?

这些方法完全相同。然而,第一种方法更可取,因为处理概率的对数会使整个过程在数值上更加稳定。结果应相同(最大数值误差)

然而,第二种方法似乎有错误

prob_dino *= p_w_given_dino
不使用您有
cnt
发生的事实;应该是这样的

prob_dino *= pow(p_w_given_dino, cnt) 

第二种方法是将概率相乘,概率可能略高于零,相乘后的总结果接近零。使用log()可以避免这个问题。@user3760780:两者的结果都相当正确吗?表示的唯一方法是改变在使用log()之后,对于最可能的类,您仍然应该获得最高的分数。除此之外,我认为您在第二种方法中没有使用
p_word
。您也只在第一种方法中使用了
cnt
。哦,谢谢你,伙计,实际上我以为它在按for循环迭代时会成倍增加,没想到我已经得到了计数值,每个单词只迭代了一次