gpt4 book ai didi

python - 如何使用NLTK中的混淆矩阵模块?

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:56 26 4
gpt4 key购买 nike

我按照 NLTK 书使用混淆矩阵,但混淆矩阵看起来很奇怪。

#empirically exam where tagger is making mistakes
test_tags = [tag for sent in brown.sents(categories='editorial')
for (word, tag) in t2.tag(sent)]
gold_tags = [tag for (word, tag) in brown.tagged_words(categories='editorial')]
print nltk.ConfusionMatrix(gold_tags, test_tags)

谁能解释一下如何使用混淆矩阵?

最佳答案

首先,我假设您从旧的 NLTK 获得了代码第 05 章:https://nltk.googlecode.com/svn/trunk/doc/book/ch05.py ,特别是你正在看这个部分:http://pastebin.com/EC8fFqLU

现在,让我们看看 NLTK 中的混淆矩阵, 尝试:

from nltk.metrics import ConfusionMatrix
ref = 'DET NN VB DET JJ NN NN IN DET NN'.split()
tagged = 'DET VB VB DET NN NN NN IN DET NN'.split()
cm = ConfusionMatrix(ref, tagged)
print cm

[输出]:

    | D         |
| E I J N V |
| T N J N B |
----+-----------+
DET |<3>. . . . |
IN | .<1>. . . |
JJ | . .<.>1 . |
NN | . . .<3>1 |
VB | . . . .<1>|
----+-----------+
(row = reference; col = test)

<> 中嵌入的数字是真阳性(tp)。从上面的示例中,您可以看到 JJ 中的一个来自引用被错误地标记为 NN从标记的输出。对于这种情况,它算作 NN 的一个误报。和一个 JJ 的假阴性.

要访问混淆矩阵(用于计算精度/召回率/fscore),您可以通过以下方式访问假阴性、假阳性和真阳性:

labels = set('DET NN VB IN JJ'.split())

true_positives = Counter()
false_negatives = Counter()
false_positives = Counter()

for i in labels:
for j in labels:
if i == j:
true_positives[i] += cm[i,j]
else:
false_negatives[i] += cm[i,j]
false_positives[j] += cm[i,j]

print "TP:", sum(true_positives.values()), true_positives
print "FN:", sum(false_negatives.values()), false_negatives
print "FP:", sum(false_positives.values()), false_positives

[输出]:

TP: 8 Counter({'DET': 3, 'NN': 3, 'VB': 1, 'IN': 1, 'JJ': 0})
FN: 2 Counter({'NN': 1, 'JJ': 1, 'VB': 0, 'DET': 0, 'IN': 0})
FP: 2 Counter({'VB': 1, 'NN': 1, 'DET': 0, 'JJ': 0, 'IN': 0})

计算每个标签的 Fscore:

for i in sorted(labels):
if true_positives[i] == 0:
fscore = 0
else:
precision = true_positives[i] / float(true_positives[i]+false_positives[i])
recall = true_positives[i] / float(true_positives[i]+false_negatives[i])
fscore = 2 * (precision * recall) / float(precision + recall)
print i, fscore

[输出]:

DET 1.0
IN 1.0
JJ 0
NN 0.75
VB 0.666666666667

我希望上面的内容能够消除 NLTK 中混淆矩阵用法的混淆。 ,这里是上面例子的完整代码:

from collections import Counter
from nltk.metrics import ConfusionMatrix

ref = 'DET NN VB DET JJ NN NN IN DET NN'.split()
tagged = 'DET VB VB DET NN NN NN IN DET NN'.split()
cm = ConfusionMatrix(ref, tagged)

print cm

labels = set('DET NN VB IN JJ'.split())

true_positives = Counter()
false_negatives = Counter()
false_positives = Counter()

for i in labels:
for j in labels:
if i == j:
true_positives[i] += cm[i,j]
else:
false_negatives[i] += cm[i,j]
false_positives[j] += cm[i,j]

print "TP:", sum(true_positives.values()), true_positives
print "FN:", sum(false_negatives.values()), false_negatives
print "FP:", sum(false_positives.values()), false_positives
print

for i in sorted(labels):
if true_positives[i] == 0:
fscore = 0
else:
precision = true_positives[i] / float(true_positives[i]+false_positives[i])
recall = true_positives[i] / float(true_positives[i]+false_negatives[i])
fscore = 2 * (precision * recall) / float(precision + recall)
print i, fscore

关于python - 如何使用NLTK中的混淆矩阵模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23704361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com