pomegranate包实现贝叶斯网络

0. 贝叶斯网络

贝叶斯网络包括一个有向无环图(DAG)和一个条件概率表集合。DAG中每个节点表示一个随机变量,可以是可直接观测变量或隐藏变量。有向边表示随机变量的条件依赖;条件概率表每个元素对应DAG唯一的节点。
贝叶斯网络任意随机变量组合的联合条件概率分布:
P ( x 1 , x 2 , … , x n ) = ∏ i = 1 n P ( x i ∣  Parents  ( x i ) ) P\left(x_{1}, x_{2}, \ldots, x_{n}\right)=\prod_{i=1}^{n} P\left(x_{i} | \text { Parents }\left(x_{i}\right)\right) P(x1,x2,,xn)=i=1nP(xi Parents (xi))

Parents 表示 x i x_i xi的直接前驱节点的联合。
贝叶斯网络的构造和学习
两步:
1、确定随机变量间的拓扑关系,形成DAG。拓扑结构一般需要领域专家完成,并需要不断迭代,改进。
2、训练贝叶斯网络。完成条件概率表的构造。

1. 包安装

pip install pomegranate==0.10

2. 示例

示例1

from pomegranate import *

import numpy as np

mydb=np.array([[1,2,3],[1,2,4],[1,2,5],[1,2,6],[1,3,8],[2,3,8],[1,2,4]])

bnet = BayesianNetwork.from_samples(mydb)

print(bnet.node_count())

print(bnet.probability([[1,2,3]]))

print (bnet.probability([[1,2,8]]))

运行结果:

3
0.14285714285714288
0.0

示例2:
蒙提霍尔问题(Monty Hall problem):源于电视节目,参赛者面前3扇门;其中只有1扇门有车,其他两扇门是山羊。参赛者可以选一扇门,然后主持人(Monty )告知剩下两扇门中是山羊的那扇。这时,只有两扇门未知 了,参赛者有一次选择,是否更换第一次的选择。

答案:更换,更换获得礼品的概率是2/3;不更换是1/3。
解释:参赛者第一次选择1/3的概率获得;主持人已经排除了一扇山羊的门,另一扇是车的概率就是2/3.
在这里插入图片描述

#Import required packages
import math
from pomegranate import *
 
# Initially the door selected by the guest is completely random
guest =DiscreteDistribution( { 'A': 1./3, 'B': 1./3, 'C': 1./3 } )
 
# The door containing the prize is also a random process
prize =DiscreteDistribution( { 'A': 1./3, 'B': 1./3, 'C': 1./3 } )
 
# The door Monty picks, depends on the choice of the guest and the prize door
monty =ConditionalProbabilityTable(
[[ 'A', 'A', 'A', 0.0 ],
[ 'A', 'A', 'B', 0.5 ],
[ 'A', 'A', 'C', 0.5 ],
[ 'A', 'B', 'A', 0.0 ],
[ 'A', 'B', 'B', 0.0 ],
[ 'A', 'B', 'C', 1.0 ],
[ 'A', 'C', 'A', 0.0 ],
[ 'A', 'C', 'B', 1.0 ],
[ 'A', 'C', 'C', 0.0 ],
[ 'B', 'A', 'A', 0.0 ],
[ 'B', 'A', 'B', 0.0 ],
[ 'B', 'A', 'C', 1.0 ],
[ 'B', 'B', 'A', 0.5 ],
[ 'B', 'B', 'B', 0.0 ],
[ 'B', 'B', 'C', 0.5 ],
[ 'B', 'C', 'A', 1.0 ],
[ 'B', 'C', 'B', 0.0 ],
[ 'B', 'C', 'C', 0.0 ],
[ 'C', 'A', 'A', 0.0 ],
[ 'C', 'A', 'B', 1.0 ],
[ 'C', 'A', 'C', 0.0 ],
[ 'C', 'B', 'A', 1.0 ],
[ 'C', 'B', 'B', 0.0 ],
[ 'C', 'B', 'C', 0.0 ],
[ 'C', 'C', 'A', 0.5 ],
[ 'C', 'C', 'B', 0.5 ],
[ 'C', 'C', 'C', 0.0 ]], [guest, prize] )
 
d1 = State( guest, name="guest" )
d2 = State( prize, name="prize" )
d3 = State( monty, name="monty" )
 
#Building the Bayesian Network
network = BayesianNetwork( "Solving the Monty Hall Problem With Bayesian Networks" )
network.add_states(d1, d2, d3)
network.add_edge(d1, d3)
network.add_edge(d2, d3)
network.bake()

# 预测
beliefs = network.predict_proba({'guest' : 'A', 'monty' : 'B'})
print("n".join( "{}t{}".format( state.name, str(belief) ) for state, belief in zip( network.states, beliefs )))

运行结果:
在参赛者选择A后,主持人选择B,车在A,B的概率分别为0.33,0.67。

guest A
prize {
"class" :"Distribution",
"dtype" :"str",
"name" :"DiscreteDistribution",
"parameters" :[
{
"A" :0.3333333333333334,
"B" :0.0,
"C" :0.6666666666666664
}
],
}

3. 其他Bayes Network 包

pgmpy,Edward。


参考:

  1. How To Implement Bayesian Networks In Python? – Bayesian Networks Explained With Examples;
  2. 算法杂货铺——分类算法之贝叶斯网络;
  3. Dynamic Bayesian Network library in Python ;
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页