Skip to main content

贝叶斯信念网络中的学习和推理

项目描述

pybbn 徽标

PyBBN

PyBBN 是用于贝叶斯信念网络 (BBN) 精确推理的 Python 库,使用连接树算法或簇树中的概率传播 (PPTC)。该实现直接取自C. Huang 和 A. Darwiche,“Inference in Belief Networks: A Procedural Guide”,发表于 International Journal of Approximate Reasoning,vol。15,第 225--263 页,1999 年。在此 API 中,PPTC 应用于具有所有离散变量的 BBN。在处理具有所有高斯变量的 BBN(或高斯信念网络,GBN)时,通过操纵均值和协方差矩阵的增量算法进行精确推断。此外,还可以生成单连通图和多连通图,取自JS Ide 和 FG Cozman,“贝叶斯网络的随机生成”,人工智能进展,计算机科学讲义,第 2507 卷。还可以选择从 BBN 生成样本数据。可以汇总这些合成数据以生成您的后验边际概率,并作为一种近似推理形式工作。最后,我们添加了 Pearl 的do-operator因果推理。

通电,下一级

图灵_bbn pyspark-bbn
图灵_bbn标志 pyspark-bbn 徽标

如果您喜欢py-bbn,请在下方咨询我们的下一代产品!info@oneoffcoder.com

  • turing_bbn是 py-bbn 的 C++17 实现;将您的因果推理和概率推理提升到下一个计算水平!
  • pyspark-bbn是一个可扩展的大规模并行处理 MPP 框架,用于使用Apache Spark学习贝叶斯信念网络 BBN 的结构和参数。

精确推理,离散变量

下面是一个示例代码,用于创建贝叶斯信念网络,将其转换为连接树,然后设置观察证据。最后一行打印每个节点的边际概率。

from pybbn.graph.dag import Bbn
from pybbn.graph.edge import Edge, EdgeType
from pybbn.graph.jointree import EvidenceBuilder
from pybbn.graph.node import BbnNode
from pybbn.graph.variable import Variable
from pybbn.pptc.inferencecontroller import InferenceController

# create the nodes
a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5])
b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6])
c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8])
d = BbnNode(Variable(3, 'd', ['on', 'off']), [0.9, 0.1, 0.5, 0.5])
e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.3, 0.7, 0.6, 0.4])
f = BbnNode(Variable(5, 'f', ['on', 'off']), [0.01, 0.99, 0.01, 0.99, 0.01, 0.99, 0.99, 0.01])
g = BbnNode(Variable(6, 'g', ['on', 'off']), [0.8, 0.2, 0.1, 0.9])
h = BbnNode(Variable(7, 'h', ['on', 'off']), [0.05, 0.95, 0.95, 0.05, 0.95, 0.05, 0.95, 0.05])

# create the network structure
bbn = Bbn() \
    .add_node(a) \
    .add_node(b) \
    .add_node(c) \
    .add_node(d) \
    .add_node(e) \
    .add_node(f) \
    .add_node(g) \
    .add_node(h) \
    .add_edge(Edge(a, b, EdgeType.DIRECTED)) \
    .add_edge(Edge(a, c, EdgeType.DIRECTED)) \
    .add_edge(Edge(b, d, EdgeType.DIRECTED)) \
    .add_edge(Edge(c, e, EdgeType.DIRECTED)) \
    .add_edge(Edge(d, f, EdgeType.DIRECTED)) \
    .add_edge(Edge(e, f, EdgeType.DIRECTED)) \
    .add_edge(Edge(c, g, EdgeType.DIRECTED)) \
    .add_edge(Edge(e, h, EdgeType.DIRECTED)) \
    .add_edge(Edge(g, h, EdgeType.DIRECTED))

# convert the BBN to a join tree
join_tree = InferenceController.apply(bbn)

# insert an observation evidence
ev = EvidenceBuilder() \
    .with_node(join_tree.get_bbn_node_by_name('a')) \
    .with_evidence('on', 1.0) \
    .build()
join_tree.set_observation(ev)

# print the marginal probabilities
for node in join_tree.get_bbn_nodes():
    potential = join_tree.get_bbn_potential(node)
    print(node)
    print(potential)

精确推理,高斯变量

下面的示例显示了如何对多元高斯变量执行推断。

import numpy as np

from pybbn.gaussian.inference import GaussianInference


def get_cowell_data():
    """
    Gets Cowell data.

    :return: Data and headers.
    """
    n = 10000
    Y = np.random.normal(0, 1, n)
    X = np.random.normal(Y, 1, n)
    Z = np.random.normal(X, 1, n)

    D = np.vstack([Y, X, Z]).T
    return D, ['Y', 'X', 'Z']


# assume we have data and headers (variable names per column)
# X is the data (rows are observations, columns are variables)
# H is just a list of variable names
X, H = get_cowell_data()

# then we can compute the means and covariance matrix easily
M = X.mean(axis=0)
E = np.cov(X.T)

# the means and covariance matrix are all we need for gaussian inference
# notice how we keep `g` around?
# we'll use `g` over and over to do inference with evidence/observations
g = GaussianInference(H, M, E)
# {'Y': (0.00967, 0.98414), 'X': (0.01836, 2.02482), 'Z': (0.02373, 3.00646)}
print(g.P)

# we can make a single observation with do_inference()
g1 = g.do_inference('X', 1.5)
# {'X': (1.5, 0), 'Y': (0.76331, 0.49519), 'Z': (1.51893, 1.00406)}
print(g1.P)

# we can make multiple observations with do_inferences()
g2 = g.do_inferences([('Z', 1.5), ('X', 2.0)])
# {'Z': (1.5, 0), 'X': (2.0, 0), 'Y': (1.97926, 0.49509)}
print(g2.P)

建造

要构建,您将需要 3.7。强烈建议通过Anaconda管理环境,以便能够构建这个项目(但如果您知道自己在做什么,则不是绝对必需的)。假设您已经安装了 Anaconda,您可以创建如下环境(确保您cd进入该项目位置的根目录)。

要创建环境,请使用以下命令。

conda env create -f environment.yml

如果要使用 Jupyter 环境,请安装内核。

conda activate pybbn37
python -m ipykernel install --user --name pybbn37 --display-name "pybbn37"

然后您可以按如下方式构建项目。(请注意,在 Python 3.6 中,您会收到一些警告)。

make build

要构建文档,请进入 docs 子目录并输入以下内容。

make html

安装

来自 PyPi

使用 pip 安装已发布到PyPi的包。

pip install pybbn

从源头

如果您查看源代码,请执行以下操作。

pip list | grep pybbn
pip uninstall pybbn
python setup.py install
pip list | grep pybbn

GraphViz 问题

确保在系统上安装 GraphViz

  • 中央操作系统:yum install graphviz*
  • Ubuntu:sudo apt-get install graphviz libgraphviz-dev
  • Mac OSX:brew install graphviz当你安装 pygraphvizpip install pygraphviz --install-option="--include-path=/usr/local/lib/graphviz/" --install-option="--library-path=/usr/local/lib/graphviz/"
  • Windows:使用msi 安装程序

testpypi 问题

应该执行此操作,但如果您确实想从 安装testpypi,请添加--extra-index-url以下内容。

pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ pybbn

其他 Python 贝叶斯信念网络推理库

这是贝叶斯信念网络中用于推理的其他 Python 库的列表。

图书馆 算法 算法类型 执照
贝叶斯 变分消息传递 近似 麻省理工学院
石榴 循环信念 近似 麻省理工学院
pgmpy 近似/精确 麻省理工学院
libpgm 似然抽样 近似 所有权
贝叶斯推理 变量消除 精确的 没有任何

我也在 PyPI 中找到了其他

爪哇

但我来自 Java 母舰,我想在 Java 中使用贝叶斯信念网络。如何在 Java 中执行概率推理?

这个 Python 代码库是原始 Java 代码的一个端口。

帮助

引文

@misc{vang_2017, 
title={PyBBN}, 
url={https://github.com/vangj/py-bbn/}, 
journal={GitHub},
author={Vang, Jee}, 
year={2017}, 
month={Jan}}

版权资料

软件

Copyright 2017 Jee Vang

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

艺术版权

版权所有 2020 Daytchia Vang

赞助商,爱

项目详情