python - 如何从 nltk 分类器中获取精度和召回率?

标签 python python-2.7 nltk

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() 获得准确率,是否有可能同时获得它的准确率和召回率?

最佳答案

如果您使用的是 nltk 包,那么您似乎可以使用 nltk.metrics.scores 中的 recallprecision 函数(see the docs)。

调用后函数应该可用

from nltk.metrics.scores import (precision, recall)

然后您需要使用reference(已知标签)和test(您的分类器在测试集上的输出)集来调用它们。

类似于下面的代码应该将这些集合生成为 refsetstestsets

refsets = collections.defaultdict(set)
testsets = collections.defaultdict(set)

for i, (feats, label) in enumerate(testing_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']) )
# `'pos'` is for the "positive" (as opposed to "negative") label 

关于python - 如何从 nltk 分类器中获取精度和召回率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45466041/

相关文章:

python - 如何在scrapy中使用spider从mysql获取数据并从web中提取数据

python - 从列表条目中连接 python 字符串

python - Django 使用来自表单字段的变量运行 python 脚本

python - 如何使用请求参数设置AWS lambda超时?

python - 'numpy.ndarray' 对象没有属性 'mode'

python - 从语料库中删除非 ASCII

python - 如何从 nltk 下载器中删除数据/模型?

python - psutil 总是返回 pid 存在

python - 我怎样才能用pyqt在qtableview中显示一个矩阵

python - 如何使用 Pandas 按类别分组然后计算单词的频率