import nltk
from nltk.corpus import movie_reviews
from nltk.tokenize import word_tokenize
documents = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
all_words = []
for w in movie_reviews.words():
all_words.append(w.lower())
all_words = nltk.FreqDist(all_words)
word_features = list(all_words.keys())[:3000]
def find_features(document):
words = set(document)
features = {}
for w in word_features:
features[w] = (w in words)
return features
featuresets = [(find_features(rev), category) for (rev, category) in documents]
training_set = featuresets[500:1500]
testing_set = featuresets[:1500]
classifier = nltk.DecisionTreeClassifier.train(training_set)
print "Classifier accuracy percent:",(nltk.classify.accuracy(classifier, testing_set))*100 , "%"
string = raw_input("Enter the string: ")
print (classifier.classify(find_features(word_tokenize(string))))
此代码将显示分类器的准确性,然后从用户获取输入。它返回用户输入的字符串的极性。
但是我的问题是:由于我可以使用nltk.accuracy()
获得准确性,是否有可能获得其精确度和召回率?
答案 0 :(得分:5)
如果您使用的是nltk软件包,则可以使用recall
See the Docs
precision
和nltk.metrics.scores
函数
后,这些功能应该可用
from nltk.metrics.scores import (precision, recall)
然后您需要使用reference
(已知标签)和test
(测试集上分类器的输出)集来调用它们。
以下代码之类的内容应将这些集合生成为refsets
和testsets
refsets = collections.defaultdict(set)
testsets = collections.defaultdict(set)
for i, (feats, label) in enumerate(cross_valid_set):
refsets[label].add(i)
observed = classifier.classify(feats)
testsets[observed].add(i)
然后,您可以通过类似
之类的内容查看精确度并回忆正面预测print 'Precision:', nltk.metrics.precision(refsets['pos'], testsets['pos'])
print 'Recall:', nltk.metrics.recall(refsets['pos'], testsets['pos'])