python 朴素贝叶斯简单实现

import re
import math

s=['this is yes','that is no']
r=[0,1]

def tokenize(message):
    '''分割单词'''
    message=message.lower()
    all_words = re.findall('[a-z0-9]+', message)
    print(all_words)
    return set(all_words)

word_set = tokenize(' '.join(s))
print(word_set)

def count_words(training_set):
    '''记录单词分别在不同类中出现的次数'''
    counts={}
    for message,is_spam in zip(s,r):
        print(message,is_spam)
        for word in tokenize(message):
            if word not in counts.keys():
                counts[word]=[0,0]
            counts[word][0 if is_spam else 1]+=1
    return counts
counts=count_words(s)
print(counts)

def word_probablities(counts, total_spams, total_non_spams, k=0.5):
    '''通过统计各单词在不同类中出现的次数计算出概率'''
    result=[]
    for w,(spam, non_spam) in dict(counts).items():
        result.append((w, (spam+k)/(total_spams+2*k), (non_spam+k)/(total_non_spams+2*k) ))
    return result
word_prob=word_probablities(counts, 1,1)
print(word_prob)

def spam_message(word_prob, message):
    '''计算新数据概率'''
    message_words = tokenize(message)

    log_prob_spam=0
    log_prob_non_spam=0
    for word, prob_spam, prob_non_spam in word_prob:
        if word in message_words:
            # 如果新单词在字典中出现
            log_prob_spam+=math.log(prob_spam)
            log_prob_non_spam+=math.log(prob_non_spam)
        else:
            # 如果新单词没有在字典中出现
            log_prob_spam+=math.log(1.0-prob_spam)
            log_prob_non_spam+=math.log(1.0-prob_non_spam)

    prob_is_spam=math.exp(log_prob_spam)
    prob_is_non_spam=math.exp(log_prob_non_spam)
    return prob_is_spam/(prob_is_spam+prob_is_non_spam)
print(spam_message(word_prob, "no is ns"))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值