文本分类(4)——手动实现朴素贝叶斯

本文探讨了如何手动实现朴素贝叶斯文本分类,包括分类过程和结果展示,同时提到了在验证过程中遇到的不一致问题,以及展示了混淆矩阵的绘制方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

朴素贝叶斯

def N_Bayes(testVector,method): #第二个表示你用哪个字典吧
    for cate,corpus in testVector.items():
        result={}
        print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), '>', cate)
        for file_name,text in corpus.items():
            tempProb=1
            prob=0
            for word,p in text.items():#p相当于每个词在这篇文章中的权重
                if word in new_weigthDict[cate]:
                    tempProb*=new_weigthDict[cate][word][method]*p
            if tempProb*docLen[cate]>prob:
                prob=tempProb
                category=cate
            result.setdefault(file_name,category)
        fname='C:/lyr/DM/result/'+method+cate+'.json'
        with open(fname,'w') as fp:
            json.dump(result,fp)

N_Bayes(testVector,'tfidf')

结果

这是我重新做的,跑结果和之前差了好几天,不知道那里有问题,不太科学。。(不太想看了:( )

cate=['Auto','business','edu','ent','healthy','mil','policy','sports','tech','tourism','wom
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值