数学之路(3)-机器学习(3)-机器学习算法-贝叶斯定理(6)
我们可以读取文本的标题,将标题也加入正文做为分词计算先验概率的对象,因为标题往往是全文的主题
#计算最大归属概率 maxgl=0. mychoice=0 for ti in xrange(0,len(yb_txt)): if testgl[ti]>maxgl: maxgl=testgl[ti] mychoice=ti print "\n\n%s\n:%s"%(mypage,txt_class[mychoice][0])
运行后,效果不错
>>> runfile(r'K:\book_prog\text_bayes2.py', wdir=r'K:\book_prog')
. . . . .
爬取汽车类网页:http://finance.chinanews.com/auto/gd.shtml
http://www.chinanews.com/auto/2013/09-18/5301023.shtml
http://www.chinanews.com/auto/2013/09-18/5301017.shtml
http://www.chinanews.com/auto/2013/09-18/5300854.shtml
.....................
读取待分类文本
http://www.chinanews.com/edu/2013/09-17/5296319.shtml读取成功.
计算待分类文本后验概率
http://www.chinanews.com/edu/2013/09-17/5296319.shtml
:教育
http://finance.chinanews.com/auto/2013/09-16/5290491.shtml读取成功.
计算待分类文本后验概率
http://finance.chinanews.com/auto/2013/09-16/5290491.shtml
:汽车
>>>