数学之路(机器学习实践指南)-文本挖掘与NLP(6)
2016-02-04 22:55
405 查看
def wordfeatures(word): return {"cnword":word} ..... classifier=nltk.NaiveBayesClassifier.train(samplewords) #大学所属的类别 http://blog.csdn.net/myhaspl print u"----大学所属的类别-----" print classifier.classify({"cnword":u"大学"}) #大脑所属的类别http://blog.csdn.net/myhaspl print u"----大脑所属的类别-----" print classifier.classify({"cnword":u"大脑"}) #测试数据分类准确率http://blog.csdn.net/myhaspl print nltk.classify.accuracy(classifier,testwords) #特征0分类最有效的10个词http://blog.csdn.net/myhaspl for wf,mostword in classifier.most_informative_features(10): print mostword, print #为显示utf-8,将show_most_informative_features代码进行修改http://blog.csdn.net/myhaspl #classifier.show_most_informative_features(10) 也可直接调用这句,但是UTF8显示有问题 http://blog.csdn.net/myhaspl cpdist = classifier._feature_probdist print('Most Informative Features') for (fname, fval) in classifier.most_informative_features(10): def labelprob(l): return cpdist[l, fname].prob(fval) labels = sorted([l for l in classifier._labels if fval in cpdist[l, fname].samples()], key=labelprob) if len(labels) == 1: continue l0 = labels[0] l1 = labels[-1] if cpdist[l0, fname].prob(fval) == 0: ratio = 'INF' else: ratio = '%8.1f' % (cpdist[l1, fname].prob(fval) / cpdist[l0, fname].prob(fval)) print fname+"="+fval, print(('%6s : %-6s = %s : 1.0' % (("%s" % l1)[:6], ("%s" % l0)[:6], ratio))) 运行结果: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =>| ----大学所属的类别----- 教育 ----大脑所属的类别----- 科技 0.977346278317 世界 公司 事先 游戏 之后 领域 采用 学科 里面 技术 Most Informative Features cnword=世界 科技 : 教育 = 20.6 : 1.0 cnword=公司 科技 : 教育 = 12.4 : 1.0 cnword=事先 科技 : 教育 = 5.8 : 1.0 cnword=游戏 科技 : 教育 = 5.8 : 1.0 cnword=之后 科技 : 教育 = 4.5 : 1.0 cnword=领域 科技 : 教育 = 4.5 : 1.0 cnword=采用 科技 : 教育 = 4.5 : 1.0 cnword=学科 科技 : 教育 = 4.1 : 1.0 cnword=里面 科技 : 教育 = 4.1 : 1.0 cnword=技术 科技 : 教育 = 4.1 : 1.0
本博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
朴素贝叶斯分类,对词条分类如上相关文章推荐
- jQuery表单选择器
- 第14、15课Java数组与方法零基础编程实战课堂笔记
- 安卓开发——Paint类
- 在 JDK 9 中更简洁使用 try-with-resources 语句
- eclipse中使用maven插件的时候,运行run as maven build/clean的时候报错
- 享元模式
- 命令模式
- 百团纳新
- Android_Studio快捷键和使用技巧
- springboot配置文件加载不到的问题
- 关于python对于aspx网页的post提交
- 关于OpenCV3.1读取摄像头图像黑屏情况
- 基本算法——第六单元 回溯
- 组态王为什么一定要定义一个COM口?
- 《C#微信开发系列(2)-自定义菜单管理》
- Linux文件系统
- Android Material Design I-基础知识
- 策略模式
- hdoj 3790 最短路径问题(根据两个变量的最短路)
- Eclipse快捷键大全