首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >双图概率

双图概率
EN

Stack Overflow用户
提问于 2020-07-13 00:38:08
回答 1查看 1.5K关注 0票数 2

我有一个白鲸迪克语料库,我需要计算双字“象牙腿”的概率。我知道这个命令给我列出了所有的表格

代码语言:javascript
运行
复制
bigrams = [w1+" "+w2 for w1,w2 in zip(words[:-1], words[1:])]

但是,我如何才能得到这两个词的概率呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-13 01:00:51

你可以数数所有的比例尺和具体的比例尺,你正在寻找。双图发生P( bigram )的概率与它们的商有关。word1给出单词P的条件概率P(w1 \ w)是双字数在w数上出现次数的商数。

代码语言:javascript
运行
复制
s = 'this is some text about some text but not some other stuff'.split()

bigrams = [(s1, s2) for s1, s2 in zip(s, s[1:])]

# [('this', 'is'),
#  ('is', 'some'),
# ('some', 'text'),
# ('text', 'about'),
# ...

number_of_bigrams = len(bigrams)
# 11

# how many times 'some' occurs 
some_count = s.count('some')
# 3

# how many times bigram occurs
bg_count = bigrams.count(('some', 'text'))
# 2

# probabily of 'text' given 'some' P(bigram | some)
# i.e. you found `some`, what's the probability that its' makes the bigram:
bg_count/some_count
# 0.666

# probabilty of bigram in text P(some text)
# i.e. pick a bigram at random, what's the probability it's your bigram:
bg_count/number_of_bigrams
# 0.181818
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62867820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档