classify.py 文件源码

python
阅读 97 收藏 0 点赞 0 评论 0

项目:FreeFoodCalendar 作者: Yuliang-Zou 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号