我对这方面知之甚少,但我可以分享我所了解的。如果我错了,请纠正我。从我从链接中看到的,没有提到使用 tf-idf 分数进行分类。您应该查看链接以了解如何使用朴素贝叶斯分类器。一般来说,代码看起来像这样(我从那个链接中获取了这个代码段)
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
def word_feats(words):
return dict([(word, True) for word in words])
negids = movie_reviews.fileids('neg')
posids = movie_reviews.fileids('pos')
negfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'neg') for f in negids]
posfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'pos') for f in posids]
negcutoff = len(negfeats)*3/4
poscutoff = len(posfeats)*3/4
trainfeats = negfeats[:negcutoff] + posfeats[:poscutoff]
testfeats = negfeats[negcutoff:] + posfeats[poscutoff:]
print 'train on %d instances, test on %d instances' % (len(trainfeats), len(testfeats))
classifier = NaiveBayesClassifier.train(trainfeats)
每个训练实例都是特征字典和类标签的元组,例如,它可以是({"sucks":True, "bad":True, "boring":True}, "Negative")
至于数字属性,我认为一种常见的方法是将它们分类为低/中/高等类别。
关于TF-IDF
分数,我不是很确定。我认为一种方法是它们可用于功能选择,例如,如果您没有。of features 太大,您可以将前 n 个单词作为特征。