朴素贝叶斯
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