了解贝叶斯定理

发布于 2024-08-17 00:27:07 字数 445 浏览 6 评论 0原文

我正在研究朴素贝叶斯分类器的实现。 集体智能编程通过将贝叶斯定理描述为以下内容来介绍此主题:

Pr(A | B) = Pr(B | A) x Pr(A)/Pr(B)

以及与文档分类相关的具体示例:

Pr(Category | Document) = Pr(Document | Category) x Pr(Category) / Pr(Document)

我希望有人能向我解释一下这里使用的符号,Pr(A | B)Pr(A) 是什么意思?它看起来像某种函数,但是管道(“|”)是什么意思等等?

I'm working on an implementation of a Naive Bayes Classifier. Programming Collective Intelligence introduces this subject by describing Bayes Theorem as:

Pr(A | B) = Pr(B | A) x Pr(A)/Pr(B)

As well as a specific example relevant to document classification:

Pr(Category | Document) = Pr(Document | Category) x Pr(Category) / Pr(Document)

I was hoping someone could explain to me the notation used here, what do Pr(A | B) and Pr(A) mean? It looks like some sort of function but then what does the pipe ("|") mean, etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

如果没有你 2024-08-24 00:27:08
  • Pr(A | B) = 假设 B 已经发生,则 A 发生的概率
  • Pr(A) = A 发生的概率

但以上是关于条件概率的计算。你想要的是一个分类器,它利用这个原理,根据之前的概率来决定某物是否属于某个类别。

有关完整示例,请参阅 http://en.wikipedia.org/wiki/Naive_Bayes_classifier

  • Pr(A | B) = Probability of A happening given that B has already happened
  • Pr(A) = Probability of A happening

But the above is with respect to the calculation of conditional probability. What you want is a classifier, which uses this principle to decide whether something belongs to a category based on the previous probability.

See http://en.wikipedia.org/wiki/Naive_Bayes_classifier for a complete example

未央 2024-08-24 00:27:08

我认为他们已经让你了解了基础知识。

Pr(A | B) = Pr(B | A) x Pr(A)/Pr(B)

读作:给定 B 的 A 的概率等于给定 A 的 B 的概率乘以 A 的概率除以 B 的概率。通常当您可以测量 B 的概率并且试图找出 B 是否成立时使用它或者,换句话说,我们确实关心A,但我们可以更直接地衡量B,所以让我们从我们可以衡量的开始。

让我给你一个推导,使编写代码变得更容易。它来自Judea Pearl。我对此有些挣扎,但当我意识到 Pearl 如何帮助我们将理论转化为代码后,我的灵光就亮了。

先验赔率:

O(H) = P(H) / 1 - P(H)

似然比:

L(e|H) = P(e|H) / P(e|¬H)

后验赔率:

O(H|e) = L(e|H)O(H)

在英语中,我们说您对某件事感兴趣的赔率(H 表示假设)就是您发现某件事为真的次数除以您发现它不为真的次数。说实话。假设每天有 10,000 所房屋中有一所被抢劫。这意味着在不考虑任何其他证据的情况下,您有 1/10,000 的机会被抢劫。

下一步是衡量您正在查看的证据。当你的问题为真时看到你所看到的证据的概率是多少除以当你的问题不为真时看到你所看到的证据的概率。假设您听到防盗警报器响起。当警报应该响时(警报响起时有人打开窗户)与不应该响时(风将警报关闭),您多久会收到警报?如果窃贼触发警报的可能性为 95%,其他事物触发警报的可能性为 1%,则可能性为 95.0。

你的总体信念只是可能性 * 先验赔率。在这种情况下,它是:

((0.95/0.01) * ((10**-4)/(1 - (10**-4))))
# => 0.0095009500950095

我不知道这是否会让事情变得更清楚,但是使用一些代码来跟踪先前的赔率,其他代码来查看可能性,以及多一段代码来处理往往会更容易。结合这些信息。

I think they've got you covered on the basics.

Pr(A | B) = Pr(B | A) x Pr(A)/Pr(B)

reads: the probability of A given B is the same as the probability of B given A times the probability of A divided by the probability of B. It's usually used when you can measure the probability of B and you are trying to figure out if B is leading us to believe in A. Or, in other words, we really care about A, but we can measure B more directly, so let's start with what we can measure.

Let me give you one derivation that makes this easier for writing code. It comes from Judea Pearl. I struggled with this a little, but after I realized how Pearl helps us turn theory into code, the light turned on for me.

Prior Odds:

O(H) = P(H) / 1 - P(H)

Likelihood Ratio:

L(e|H) = P(e|H) / P(e|¬H)

Posterior Odds:

O(H|e) = L(e|H)O(H)

In English, we are saying that the odds of something you're interested in (H for hypothesis) are simply the number of times you find something to be true divided by the times you find it not to be true. So, say one house is robbed every day out of 10,000. That means that you have a 1/10,000 chance of being robbed, without any other evidence being considered.

The next one is measuring the evidence you're looking at. What is the probability of seeing the evidence you're seeing when your question is true divided by the probability of seeing the evidence you're seeing when your question is not true. Say you are hearing your burglar alarm go off. How often do you get that alarm when it's supposed to go off (someone opens a window when the alarm is on) versus when it's not supposed to go off (the wind set the alarm off). If you have a 95% chance of a burglar setting off the alarm and a 1% chance of something else setting off the alarm, then you have a likelihood of 95.0.

Your overall belief is just the likelihood * the prior odds. In this case it is:

((0.95/0.01) * ((10**-4)/(1 - (10**-4))))
# => 0.0095009500950095

I don't know if this makes it any more clear, but it tends to be easier to have some code that keeps track of prior odds, other code to look at likelihoods, and one more piece of code to combine this information.

岁月打碎记忆 2024-08-24 00:27:08

我已经用Python实现了它。这很容易理解,因为贝叶斯定理的所有公式都在单独的函数中:

#Bayes Theorem

def get_outcomes(sample_space, f_name='', e_name=''):
    outcomes = 0
    for e_k, e_v in sample_space.items():
        if f_name=='' or f_name==e_k:
            for se_k, se_v in e_v.items():
                if e_name!='' and se_k == e_name:
                    outcomes+=se_v
                elif e_name=='':
                    outcomes+=se_v
    return outcomes

def p(sample_space, f_name):
    return get_outcomes(sample_space, f_name) / get_outcomes(sample_space, '', '')

def p_inters(sample_space, f_name, e_name):
    return get_outcomes(sample_space, f_name, e_name) / get_outcomes(sample_space, '', '')

def p_conditional(sample_space, f_name, e_name):
    return p_inters(sample_space, f_name, e_name) / p(sample_space, f_name)

def bayes(sample_space, f, given_e):
    sum = 0;
    for e_k, e_v in sample_space.items():
        sum+=p(sample_space, e_k) * p_conditional(sample_space, e_k, given_e)
    return p(sample_space, f) * p_conditional(sample_space, f, given_e) / sum

sample_space = {'UK':{'Boy':10, 'Girl':20},
                'FR':{'Boy':10, 'Girl':10},
                'CA':{'Boy':10, 'Girl':30}}

print('Probability of being from FR:', p(sample_space, 'FR'))
print('Probability to be French Boy:', p_inters(sample_space, 'FR', 'Boy'))
print('Probability of being a Boy given a person is from FR:', p_conditional(sample_space, 'FR', 'Boy'))
print('Probability to be from France given person is Boy:', bayes(sample_space, 'FR', 'Boy'))

sample_space = {'Grow' :{'Up':160, 'Down':40},
                'Slows':{'Up':30, 'Down':70}}

print('Probability economy is growing when stock is Up:', bayes(sample_space, 'Grow', 'Up'))

I have implemented it in Python. It's very easy to understand because all formulas for Bayes theorem are in separate functions:

#Bayes Theorem

def get_outcomes(sample_space, f_name='', e_name=''):
    outcomes = 0
    for e_k, e_v in sample_space.items():
        if f_name=='' or f_name==e_k:
            for se_k, se_v in e_v.items():
                if e_name!='' and se_k == e_name:
                    outcomes+=se_v
                elif e_name=='':
                    outcomes+=se_v
    return outcomes

def p(sample_space, f_name):
    return get_outcomes(sample_space, f_name) / get_outcomes(sample_space, '', '')

def p_inters(sample_space, f_name, e_name):
    return get_outcomes(sample_space, f_name, e_name) / get_outcomes(sample_space, '', '')

def p_conditional(sample_space, f_name, e_name):
    return p_inters(sample_space, f_name, e_name) / p(sample_space, f_name)

def bayes(sample_space, f, given_e):
    sum = 0;
    for e_k, e_v in sample_space.items():
        sum+=p(sample_space, e_k) * p_conditional(sample_space, e_k, given_e)
    return p(sample_space, f) * p_conditional(sample_space, f, given_e) / sum

sample_space = {'UK':{'Boy':10, 'Girl':20},
                'FR':{'Boy':10, 'Girl':10},
                'CA':{'Boy':10, 'Girl':30}}

print('Probability of being from FR:', p(sample_space, 'FR'))
print('Probability to be French Boy:', p_inters(sample_space, 'FR', 'Boy'))
print('Probability of being a Boy given a person is from FR:', p_conditional(sample_space, 'FR', 'Boy'))
print('Probability to be from France given person is Boy:', bayes(sample_space, 'FR', 'Boy'))

sample_space = {'Grow' :{'Up':160, 'Down':40},
                'Slows':{'Up':30, 'Down':70}}

print('Probability economy is growing when stock is Up:', bayes(sample_space, 'Grow', 'Up'))
孤独患者 2024-08-24 00:27:08

Pr(A | B):A 的条件概率:即 A 的概率,假设我们只知道 B

Pr(A):A 的先验概率

Pr(A | B): Conditional probability of A : i.e. probability of A, given that all we know is B

Pr(A) : Prior probability of A

全部不再 2024-08-24 00:27:08

Pr 是概率,Pr(A|B) 是条件概率。

查看维基百科了解详细信息。

Pr is the probability, Pr(A|B) is the conditional probability.

Check wikipedia for details.

好倦 2024-08-24 00:27:08

竖线 (|) 表示“给定”。
给定 B 时 A 的概率等于给定 A x Pr(A)/Pr(B) 时 B 的概率

the pipe (|) means "given".
The probability of A given B is equal to the probability of B given A x Pr(A)/Pr(B)

浪漫人生路 2024-08-24 00:27:08

管道用于表示条件概率。
Pr(A | B) = 给定 B 时 A 的概率

示例:
假设您感觉不舒服,并且上网查找症状。互联网告诉您,如果您有这些症状,那么您就患有 XYZ 疾病。

在这种情况下:
Pr(A | B) 是您想要找出的内容,即:
鉴于您有某些症状,您患有 XYZ 的可能性。

Pr(A) 是患有 XYZ 疾病的概率

Pr(B) 是患有这些症状的概率

Pr(B | A) 是您从互联网上找到的,即:
鉴于您患有这种疾病,出现症状的可能性。

The pipe is used to represent conditional probability.
Pr(A | B) = Probability of A given B

Example:
Let's say you are not feeling well and you surf the web for the symptoms. And the internet tells you that if you have these symptoms then you have XYZ disease.

In this case:
Pr(A | B) is what you are trying to find out, which is:
The probability of you having XYZ GIVEN THAT you have certain symptoms.

Pr(A) is the probability of having the disease XYZ

Pr(B) is the probability of having those symptoms

Pr(B | A) is what you find out from the internet, which is:
The probability of having the symptoms GIVEN THAT you have the disease.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文