数学之路(机器学习实践指南)-文本挖掘与NLP(6)

来源:互联网 发布:xp专业版激活软件 编辑:程序博客网 时间:2024/04/30 05:31
def wordfeatures(word):    return {"cnword":word}.....classifier=nltk.NaiveBayesClassifier.train(samplewords)#大学所属的类别 http://blog.csdn.net/myhasplprint u"----大学所属的类别-----"print classifier.classify({"cnword":u"大学"})#大脑所属的类别http://blog.csdn.net/myhasplprint u"----大脑所属的类别-----"print classifier.classify({"cnword":u"大脑"})#测试数据分类准确率http://blog.csdn.net/myhasplprint nltk.classify.accuracy(classifier,testwords)#特征0分类最有效的10个词http://blog.csdn.net/myhasplfor 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_probdistprint('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 Featurescnword=世界     科技 : 教育     =     20.6 : 1.0cnword=公司     科技 : 教育     =     12.4 : 1.0cnword=事先     科技 : 教育     =      5.8 : 1.0cnword=游戏     科技 : 教育     =      5.8 : 1.0cnword=之后     科技 : 教育     =      4.5 : 1.0cnword=领域     科技 : 教育     =      4.5 : 1.0cnword=采用     科技 : 教育     =      4.5 : 1.0cnword=学科     科技 : 教育     =      4.1 : 1.0cnword=里面     科技 : 教育     =      4.1 : 1.0cnword=技术     科技 : 教育     =      4.1 : 1.0

本博客所有内容是原创,如果转载请注明来源

http://blog.csdn.net/myhaspl/



朴素贝叶斯分类,对词条分类如上


1 0
原创粉丝点击