def classify_naive_bayes(X_test, prior, likelihood, num):
p_not = math.log10(prior[0])
p_free = math.log10(prior[1])
not_dict = likelihood[0]
free_dict = likelihood[1]
not_num = num[0]
free_num = num[1]
voc_num = num[2]
for word in X_test:
# not free
if word in not_dict:
p_not += math.log10(1.0 * not_dict[word])
else:
p_not += math.log10(1.0 / (not_num + voc_num))
# free
if word in free_dict:
p_free += math.log10(1.0 * free_dict[word])
else:
p_free += math.log10(1.0 / (free_num + voc_num))
if p_free >= p_not:
return True
else:
return False
评论列表
文章目录