Python学习包,用于学习贝叶斯网络的图形结构、参数学习、推理和采样方法。

bnlearn的Python项目详细描述


贝叶斯网络的图形结构

PythonPyPI VersionLicenseCoffeeGithub ForksGitHub Open IssuesProject StatusDownloadsDownloadsSphinx

Star it if you like it!

bnlearn是一个Python包,用于学习贝叶斯网络的图形结构、参数学习、推理和采样方法。这件作品的灵感来源于R包(bnlearn.com网站)多年来这对我很有用。尽管有非常好的Python包用于概率图形模型,但是仍然很难(有时甚至不必要)构建某些管道。bnlearnforpython(这个包)构建在pgmpy包上,包含最需要的管道。导航到API documentations以获取更多详细信息。在

方法概述

学习贝叶斯网络可以分为两个问题,这两个问题都在本软件包中实现:

  • 结构学习:给定一组数据样本,估计一个捕获变量之间依赖关系的DAG。在
  • 参数学习:给定一组数据样本和一个捕获变量之间依赖关系的DAG,估计单个变量的(条件)概率分布。在

导入bn后,以下功能可用。

^{pr2}$

还包括以下方法:

  • 推论
  • 抽样
  • 比较两个网络
  • 加载bif文件
  • 有向图到无向图的转换

目录

安装

  • 从pybnpi学习(推荐)。bnlearn与python3.6+兼容,可在Linux、macosx和Windows上运行。在
  • 它是根据麻省理工学院的许可证发行的。在

要求

  • 创造一个新的环境是明智的。在
condacreate-nenv_bnlearnpython=3.8condaactivateenv_bnlearncondainstall-cankurankanpgmpy# You may need to deactivate and then activate your environment otherwise the packages may not been recognized.condadeactivatecondaactivateenv_bnlearn# The packages below are handled by the requirements in the bnlearn pip installer. So you dont need to do them manually.pipinstallsklearnpandastqdmfuncsigsstatsmodelscommunitypackaging

快速入门

pip install bnlearn
  • 或者,从GitHub源安装bnlearn:
git clone https://github.com/erdogant/bnlearn.git
cd bnlearn
pip install -U .

导入bnlearn包

importbnlearnasbn

示例:结构学习

# Example dataframe sprinkler_data.csv can be loaded with: df=bn.import_example()# df = pd.read_csv('sprinkler_data.csv')model=bn.structure_learning.fit(df)G=bn.plot(model)

df看起来像这样

     Cloudy  Sprinkler  Rain  Wet_Grass
0         0          1     0          1
1         1          1     1          1
2         1          0     1          1
3         0          0     1          1
4         1          0     1          1
..      ...        ...   ...        ...
995       0          0     0          0
996       1          0     0          0
997       0          0     1          0
998       1          1     0          1
999       1          0     1          1

  • 选择各种MethodType和ScoringType:
model_hc_bic=bn.structure_learning.fit(df,methodtype='hc',scoretype='bic')model_hc_k2=bn.structure_learning.fit(df,methodtype='hc',scoretype='k2')model_hc_bdeu=bn.structure_learning.fit(df,methodtype='hc',scoretype='bdeu')model_ex_bic=bn.structure_learning.fit(df,methodtype='ex',scoretype='bic')model_ex_k2=bn.structure_learning.fit(df,methodtype='ex',scoretype='k2')model_ex_bdeu=bn.structure_learning.fit(df,methodtype='ex',scoretype='bdeu')

示例:参数学习

# Import dataframedf=bn.import_example()# As an example we set the CPD at False which returns an "empty" DAGmodel=bn.import_DAG('sprinkler',CPD=False)# Now we learn the parameters of the DAG using the dfmodel_update=bn.parameter_learning.fit(model,df)# Make plotG=bn.plot(model_update)

示例:推理

model=bn.import_DAG('sprinkler')q_1=bn.inference.fit(model,variables=['Rain'],evidence={'Cloudy':1,'Sprinkler':0,'Wet_Grass':1})q_2=bn.inference.fit(model,variables=['Rain'],evidence={'Cloudy':1})

示例:采样以创建数据帧

model=bn.import_DAG('sprinkler')df=bn.sampling(model,n=1000)
  • 模型输出:
[bnlearn] Model correct: True
CPD of Cloudy:
+-----------+-----+
| Cloudy(0) | 0.5 |
+-----------+-----+
| Cloudy(1) | 0.5 |
+-----------+-----+
CPD of Sprinkler:
+--------------+-----------+-----------+
| Cloudy       | Cloudy(0) | Cloudy(1) |
+--------------+-----------+-----------+
| Sprinkler(0) | 0.5       | 0.9       |
+--------------+-----------+-----------+
| Sprinkler(1) | 0.5       | 0.1       |
+--------------+-----------+-----------+
CPD of Rain:
+---------+-----------+-----------+
| Cloudy  | Cloudy(0) | Cloudy(1) |
+---------+-----------+-----------+
| Rain(0) | 0.8       | 0.2       |
+---------+-----------+-----------+
| Rain(1) | 0.2       | 0.8       |
+---------+-----------+-----------+
CPD of Wet_Grass:
+--------------+--------------+--------------+--------------+--------------+
| Sprinkler    | Sprinkler(0) | Sprinkler(0) | Sprinkler(1) | Sprinkler(1) |
+--------------+--------------+--------------+--------------+--------------+
| Rain         | Rain(0)      | Rain(1)      | Rain(0)      | Rain(1)      |
+--------------+--------------+--------------+--------------+--------------+
| Wet_Grass(0) | 1.0          | 0.1          | 0.1          | 0.01         |
+--------------+--------------+--------------+--------------+--------------+
| Wet_Grass(1) | 0.0          | 0.9          | 0.9          | 0.99         |
+--------------+--------------+--------------+--------------+--------------+
[bnlearn] Nodes: ['Cloudy', 'Sprinkler', 'Rain', 'Wet_Grass']
[bnlearn] Edges: [('Cloudy', 'Sprinkler'), ('Cloudy', 'Rain'), ('Sprinkler', 'Wet_Grass'), ('Rain', 'Wet_Grass')]
[bnlearn] Independencies:
(Cloudy _|_ Wet_Grass | Rain, Sprinkler)
(Sprinkler _|_ Rain | Cloudy)
(Rain _|_ Sprinkler | Cloudy)
(Wet_Grass _|_ Cloudy | Rain, Sprinkler)

示例:从bif文件加载DAG

bif_file='sprinkler'bif_file='alarm'bif_file='andes'bif_file='asia'bif_file='pathfinder'bif_file='sachs'bif_file='miserables'bif_file='filepath/to/model.bif'# Loading example datasetmodel=bn.import_DAG(bif_file)

示例:比较网络

# Load asia DAGmodel=bn.import_DAG('asia')# plot ground truthG=bn.plot(model)# Samplingdf=bn.sampling(model,n=10000)# Structure learning of sampled datasetmodel_sl=bn.structure_learning.fit(df,methodtype='hc',scoretype='bic')# Plot based on structure learning of sampled databn.plot(model_sl,pos=G['pos'])# Compare networks and make plotbn.compare_networks(model,model_sl,pos=G['pos'])

实际情况图

基于结构学习的图

图形比较基本真实与结构学习

引文

如果这对你的研究有用,请在你的出版物中引用bnlearn。以下是BibTeX条目示例:

@misc{erdogant2019bnlearn,title={bnlearn},author={Erdogan Taskesen},year={2019},howpublished={\url{https://github.com/erdogant/bnlearn}},}

参考文献

维护人员

  • Erdogan Taskesen,github:erdogant
  • 欢迎投稿。在
  • 如果你想为这件作品给我买一件Coffee的作品,我非常感激:)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java根据收到的短信找出手机的位置   java可以使用selenium在单个脚本中混合ios自动化和web自动化   java如何将@RequestParam映射到对象?   java JFrame关闭操作   java如何使用给定的JNDI名称连接到Websphere数据源?   hibernate Java一对多持久化哈希集   java如何设置JdbcUserDetailsManager以使用我的表?   java JUNIT测试Eclipse IDE引发错误   安卓如何根据Java中对象的一个成员变量从arraylist中删除另一个arraylist中存在的对象?   java使用枚举类中的方法,当在运行时获取枚举时   Java ArrayList中的安卓 NullPointerException   java如何使用jinterop在远程机器中编写二进制文件?   Java是跨平台的吗?   java将主题设置为具有自定义绘图功能的按钮   java Spring安全Oauth2:在Tomcat中成功注销,但在Glashfish中无法注销   使用HttpUrlConnection的Java类引发IOException,无法读取或写入   java Primefaces SelectOneRadio,选择了NoSelection选项   java在安卓中从字符串数组中删除元素   Java异常构造函数和方法   树映射中的java顺序错误