首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Python3.x创建贝叶斯网络并学习参数

使用Python3.x创建贝叶斯网络并学习参数
EN

Stack Overflow用户
提问于 2015-02-10 20:11:09
回答 5查看 28.1K关注 0票数 29

我正在为Windows上的python3.x寻找最合适的工具来创建一个贝叶斯网络,从数据中学习它的参数并执行推理。

我想要定义自己的网络结构如下:

它取自this论文。

除了"Size“和"GraspPose”之外,所有变量都是离散的(并且只能采用两种可能的状态),这两个变量是连续的,应该建模为高斯混合。

作者使用期望最大化算法来学习条件概率表的参数,并使用联合树算法来计算精确推理。

据我所知,所有这些都是用墨菲的贝叶斯网络工具箱在MatLab中实现的。

我尝试在python中搜索类似的内容,结果如下:

  1. Python贝叶斯网络工具箱http://sourceforge.net/projects/pbnt.berlios/ (http://pbnt.berlios.de/)。网站不工作,项目似乎不受支持。
  2. BayesPy https://github.com/bayespy/bayespy我想这才是我真正需要的,但我找不到一些与我的情况类似的例子,了解如何构建网络structure.
  3. PyMC似乎是一个强大的模块,但我有问题导入到Windows64,Python3.3上。安装开发版本时出现错误

警告(theano.configdefaults):未检测到g++!Theano将无法执行优化的C实现(针对CPU和GPU),并将默认使用Python实现。性能将严重下降。要删除此警告,请将Theano标志cxx设置为空字符串。

更新:

  1. libpgm (http://pythonhosted.org/libpgm/)。这正是我需要的,不幸的是Python3.x不支持
  2. 非常有趣的正在开发的库: PGMPY。不幸的是,目前还不支持连续变量和从数据中学习。https://github.com/pgmpy/pgmpy/

任何建议和具体的例子都将受到高度赞赏。

EN

回答 5

Stack Overflow用户

发布于 2015-10-20 23:53:57

看起来pomegranate最近更新了,加入了贝叶斯网络。我自己还没有试过,但界面看起来不错,而且很滑稽。

票数 12
EN

Stack Overflow用户

发布于 2020-06-18 22:59:32

试试bnlearn库,它包含许多函数,可以从数据中学习参数并执行推理。

代码语言:javascript
复制
pip install bnlearn

您的用例将如下所示:

代码语言:javascript
复制
# Import the library
import bnlearn

# Define the network structure
edges = [('task', 'size'),
         ('lat var', 'size'),
         ('task', 'fill level'),
         ('task', 'object shape'),
         ('task', 'side graspable'),
         ('size', 'GrasPose'),
         ('task', 'GrasPose'),
         ('fill level', 'GrasPose'),
         ('object shape', 'GrasPose'),
         ('side graspable', 'GrasPose'),
         ('GrasPose', 'latvar'),
]

# Make the actual Bayesian DAG
DAG = bnlearn.make_DAG(edges)

# DAG is stored in adjacency matrix
print(DAG['adjmat'])

# target           task   size  lat var  ...  side graspable  GrasPose  latvar
# source                                 ...                                  
# task            False   True    False  ...            True      True   False
# size            False  False    False  ...           False      True   False
# lat var         False   True    False  ...           False     False   False
# fill level      False  False    False  ...           False      True   False
# object shape    False  False    False  ...           False      True   False
# side graspable  False  False    False  ...           False      True   False
# GrasPose        False  False    False  ...           False     False    True
# latvar          False  False    False  ...           False     False   False
# 
# [8 rows x 8 columns]

# No CPDs are in the DAG. Lets see what happens if we print it.
bnlearn.print_CPD(DAG)
# >[BNLEARN.print_CPD] No CPDs to print. Use bnlearn.plot(DAG) to make a plot.

# Plot DAG. Note that it can be differently orientated if you re-make the plot.
bnlearn.plot(DAG)

现在我们需要数据来学习它的参数。假设这些都存储在您的df中。数据文件中的变量名必须存在于DAG中。

代码语言:javascript
复制
# Read data
df = pd.read_csv('path_to_your_data.csv')

# Learn the parameters and store CPDs in the DAG. Use the methodtype your desire. Options are maximumlikelihood or bayes.
DAG = bnlearn.parameter_learning.fit(DAG, df, methodtype='maximumlikelihood')
# CPDs are present in the DAG at this point.
bnlearn.print_CPD(DAG)

# Start making inferences now. As an example:
q1 = bnlearn.inference.fit(DAG, variables=['lat var'], evidence={'fill level':1, 'size':0, 'task':1})

下面是一个演示数据集(sprinkler)的工作示例。你可以玩这个。

代码语言:javascript
复制
# Import example dataset
df = bnlearn.import_example('sprinkler')
print(df)
#      Cloudy  Sprinkler  Rain  Wet_Grass
# 0         0          0     0          0
# 1         1          0     1          1
# 2         0          1     0          1
# 3         1          1     1          1
# 4         1          1     1          1
# ..      ...        ...   ...        ...
# 995       1          0     1          1
# 996       1          0     1          1
# 997       1          0     1          1
# 998       0          0     0          0
# 999       0          1     1          1

# [1000 rows x 4 columns]


# Define the network structure
edges = [('Cloudy', 'Sprinkler'),
         ('Cloudy', 'Rain'),
         ('Sprinkler', 'Wet_Grass'),
         ('Rain', 'Wet_Grass')]

# Make the actual Bayesian DAG
DAG = bnlearn.make_DAG(edges)
# Print the CPDs
bnlearn.print_CPD(DAG)
# [BNLEARN.print_CPD] No CPDs to print. Use bnlearn.plot(DAG) to make a plot.
# Plot the DAG
bnlearn.plot(DAG)

代码语言:javascript
复制
# Parameter learning on the user-defined DAG and input data
DAG = bnlearn.parameter_learning.fit(DAG, df)

# Print the learned CPDs
bnlearn.print_CPD(DAG)

# [BNLEARN.print_CPD] Independencies:
# (Cloudy _|_ Wet_Grass | Rain, Sprinkler)
# (Sprinkler _|_ Rain | Cloudy)
# (Rain _|_ Sprinkler | Cloudy)
# (Wet_Grass _|_ Cloudy | Rain, Sprinkler)
# [BNLEARN.print_CPD] Nodes: ['Cloudy', 'Sprinkler', 'Rain', 'Wet_Grass']
# [BNLEARN.print_CPD] Edges: [('Cloudy', 'Sprinkler'), ('Cloudy', 'Rain'), ('Sprinkler', 'Wet_Grass'), ('Rain', 'Wet_Grass')]
# CPD of Cloudy:
# +-----------+-------+
# | Cloudy(0) | 0.494 |
# +-----------+-------+
# | Cloudy(1) | 0.506 |
# +-----------+-------+
# CPD of Sprinkler:
# +--------------+--------------------+--------------------+
# | Cloudy       | Cloudy(0)          | Cloudy(1)          |
# +--------------+--------------------+--------------------+
# | Sprinkler(0) | 0.4807692307692308 | 0.7075098814229249 |
# +--------------+--------------------+--------------------+
# | Sprinkler(1) | 0.5192307692307693 | 0.2924901185770751 |
# +--------------+--------------------+--------------------+
# CPD of Rain:
# +---------+--------------------+---------------------+
# | Cloudy  | Cloudy(0)          | Cloudy(1)           |
# +---------+--------------------+---------------------+
# | Rain(0) | 0.6518218623481782 | 0.33695652173913043 |
# +---------+--------------------+---------------------+
# | Rain(1) | 0.3481781376518219 | 0.6630434782608695  |
# +---------+--------------------+---------------------+
# CPD of Wet_Grass:
# +--------------+--------------------+---------------------+---------------------+---------------------+
# | Rain         | Rain(0)            | Rain(0)             | Rain(1)             | Rain(1)             |
# +--------------+--------------------+---------------------+---------------------+---------------------+
# | Sprinkler    | Sprinkler(0)       | Sprinkler(1)        | Sprinkler(0)        | Sprinkler(1)        |
# +--------------+--------------------+---------------------+---------------------+---------------------+
# | Wet_Grass(0) | 0.7553816046966731 | 0.33755274261603374 | 0.25588235294117645 | 0.37910447761194027 |
# +--------------+--------------------+---------------------+---------------------+---------------------+
# | Wet_Grass(1) | 0.2446183953033268 | 0.6624472573839663  | 0.7441176470588236  | 0.6208955223880597  |
# +--------------+--------------------+---------------------+---------------------+---------------------+

# Make inference
q1 = bnlearn.inference.fit(DAG, variables=['Wet_Grass'], evidence={'Rain':1, 'Sprinkler':0, 'Cloudy':1})

# +--------------+------------------+
# | Wet_Grass    |   phi(Wet_Grass) |
# +==============+==================+
# | Wet_Grass(0) |           0.2559 |
# +--------------+------------------+
# | Wet_Grass(1) |           0.7441 |
# +--------------+------------------+

print(q1.values)
# array([0.25588235, 0.74411765])

有关更多示例,请访问bnlearn:https://erdogant.github.io/bnlearn

票数 5
EN

Stack Overflow用户

发布于 2018-10-03 13:45:11

我正在寻找一个类似的库,我发现pomegranate是一个很好的库。谢谢James Atwood

下面是一个如何使用它的示例。

代码语言:javascript
复制
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]]))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28431350

复制
相关文章

相似问题

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