如何解释 Python NLTK 二元似然比?
How to interpret Python NLTK bigram likelihood ratios?
我正在尝试找出如何正确解释 nltk
的 "likelihood ratio",给出以下代码(取自此 question)。
import nltk.collocations
import nltk.corpus
import collections
bgm = nltk.collocations.BigramAssocMeasures()
finder = nltk.collocations.BigramCollocationFinder.from_words(nltk.corpus.brown.words())
scored = finder.score_ngrams(bgm.likelihood_ratio)
# Group bigrams by first word in bigram.
prefix_keys = collections.defaultdict(list)
for key, scores in scored:
prefix_keys[key[0]].append((key[1], scores))
for key in prefix_keys:
prefix_keys[key].sort(key = lambda x: -x[1])
prefix_keys['baseball']
输出如下:
[('game', 32.11075451975229),
('cap', 27.81891372457088),
('park', 23.509042621473505),
('games', 23.10503351305401),
("player's", 16.22787286342467),
('rightfully', 16.22787286342467),
[...]
查看 docs,看起来每个二元组旁边打印的似然比来自
"Scores ngrams using likelihood ratios as in Manning and Schutze
5.3.4."
指的是 this article,它在 pg. 22:
One advantage of likelihood ratios is that they have a clear intuitive
interpretation. For example, the bigram powerful computers is
e^(.5*82.96) = 1.3*10^18 times more likely under the hypothesis that
computers is more likely to follow powerful than its base rate of
occurrence would suggest. This number is easier to interpret than the
scores of the t test or the 2 test which we have to look up in a
table.
我感到困惑的是,如果我将上面提到的 nltk
代码与我自己的数据一起使用,那么 "base rate of occurence" 会是什么。例如,可以肯定地说 "game" 在当前数据集中出现在 "baseball" 旁边的可能性是标准英语的平均使用率的 32 倍吗?还是在 相同的 数据集中,"game" 比出现在 "baseball" 旁边的其他词更有可能出现在 "baseball" 旁边?
非常感谢任何 help/guidance 更清晰的解释或示例!
nltk 没有通用的英语语言语料库来模拟 'game' 跟随 'baseball' 的概率。
使用它确实可用的语料库,将可能性计算为“棒球”在成为“游戏”之前给出的单词的后验概率。
nltk.corpus.brown
是内置的语料库或观察集,任何基于概率的模型的预测能力完全由用于构建或训练它的观察来定义。
nltk.collocations.BigramAssocMeasures().raw_freq
使用 t 检验对原始频率建模,不太适合二元组等稀疏数据,因此提供了似然比。
Manning 和 Schutze 计算的似然比不等于频率。
https://nlp.stanford.edu/fsnlp/promo/colloc.pdf
第 5.3.4 节详细介绍了他们的计算方法。
可能性可以无限大。
这张图表可能会有帮助:
可能性计算为最左边的列。
我正在尝试找出如何正确解释 nltk
的 "likelihood ratio",给出以下代码(取自此 question)。
import nltk.collocations
import nltk.corpus
import collections
bgm = nltk.collocations.BigramAssocMeasures()
finder = nltk.collocations.BigramCollocationFinder.from_words(nltk.corpus.brown.words())
scored = finder.score_ngrams(bgm.likelihood_ratio)
# Group bigrams by first word in bigram.
prefix_keys = collections.defaultdict(list)
for key, scores in scored:
prefix_keys[key[0]].append((key[1], scores))
for key in prefix_keys:
prefix_keys[key].sort(key = lambda x: -x[1])
prefix_keys['baseball']
输出如下:
[('game', 32.11075451975229),
('cap', 27.81891372457088),
('park', 23.509042621473505),
('games', 23.10503351305401),
("player's", 16.22787286342467),
('rightfully', 16.22787286342467),
[...]
查看 docs,看起来每个二元组旁边打印的似然比来自
"Scores ngrams using likelihood ratios as in Manning and Schutze 5.3.4."
指的是 this article,它在 pg. 22:
One advantage of likelihood ratios is that they have a clear intuitive interpretation. For example, the bigram powerful computers is e^(.5*82.96) = 1.3*10^18 times more likely under the hypothesis that computers is more likely to follow powerful than its base rate of occurrence would suggest. This number is easier to interpret than the scores of the t test or the 2 test which we have to look up in a table.
我感到困惑的是,如果我将上面提到的 nltk
代码与我自己的数据一起使用,那么 "base rate of occurence" 会是什么。例如,可以肯定地说 "game" 在当前数据集中出现在 "baseball" 旁边的可能性是标准英语的平均使用率的 32 倍吗?还是在 相同的 数据集中,"game" 比出现在 "baseball" 旁边的其他词更有可能出现在 "baseball" 旁边?
非常感谢任何 help/guidance 更清晰的解释或示例!
nltk 没有通用的英语语言语料库来模拟 'game' 跟随 'baseball' 的概率。
使用它确实可用的语料库,将可能性计算为“棒球”在成为“游戏”之前给出的单词的后验概率。
nltk.corpus.brown
是内置的语料库或观察集,任何基于概率的模型的预测能力完全由用于构建或训练它的观察来定义。
nltk.collocations.BigramAssocMeasures().raw_freq
使用 t 检验对原始频率建模,不太适合二元组等稀疏数据,因此提供了似然比。
Manning 和 Schutze 计算的似然比不等于频率。
https://nlp.stanford.edu/fsnlp/promo/colloc.pdf
第 5.3.4 节详细介绍了他们的计算方法。
可能性可以无限大。
这张图表可能会有帮助:
可能性计算为最左边的列。