Other

项目介绍

erdogant/bnlearn 是一个托管在 GitHub 上的开源项目,主要使用 Jupyter Notebook 编写,采用 Other 许可证,目前获得 645 个 Star、61 次 Fork。

仓库全名
erdogant/bnlearn
开源许可证
Other
Star 数
645
Fork 数
61
未关闭 Issue
2

README 摘要

bnlearn is Python package for causal discovery by learning the graphical structure of Bayesian networks, parameter learning, inference, and sampling methods. Because probabilistic graphical models can be difficult to use, Bnlearn contains the most-wanted pipelines. Navigate to API documentations for more detailed information. ⭐️ Star it if you like it ⭐️ Key Features Feature | Description | Medium | Gumroad/Podcast…

完整内容见 GitHub 原始 README 或本页 。

常见问题

bnlearn 用什么语言开发?

主要使用 Jupyter Notebook 编写,采用 Other 许可证,目前有 645 个 Star。

bnlearn 可以免费商用吗?

仓库标注的许可证为 Other,商用前请先阅读仓库 LICENSE 文件确认授权范围。

如何获取 bnlearn 的源码?

可以直接克隆仓库:git clone https://github.com/erdogant/bnlearn.git,默认分支为 master;安装与运行步骤见仓库 README。

bnlearn 最近还在维护吗?

仓库最近一次更新为 2 周前,当前有 2 个未关闭 Issue;Star 数为 645,可作为社区活跃度的参考。

关于

Python package for Causal Discovery by learning the graphical structure of Bayesian networks. Structure Learning, Parameter Learning, Inferences, Sampling methods.

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

语言占比

Jupyter Notebook
Python

Release

README

Python PyPI Version GitHub Repo stars License Forks Open Issues Project Status Downloads Downloads Docs Medium GitHub repo size Donate Colab skills.sh

bnlearn is Python package for causal discovery by learning the graphical structure of Bayesian networks, parameter learning, inference, and sampling methods. Because probabilistic graphical models can be difficult to use, Bnlearn contains the most-wanted pipelines. Navigate to API documentations for more detailed information. ⭐️ Star it if you like it ⭐️


Key Features

Feature Description Medium Gumroad/Podcast
Causal Discovery - Overview and Starters Guide Learn the basics of causal modelling. link —
Structure Learning Learn the model structure from data or with expert knowledge. link Podcast
Causal Predictions Learn to make causal predictions and explore interventions. link —
Parameter Learning Estimate model parameters (e.g., conditional probability distributions) from observed data and combine data with expert knowledge. link Podcast
Causal Inference Compute interventional and counterfactual distributions using Bayesian inference. link Podcast
Generate Synthetic Data Generate synthetic data using Bayesian sampling. link Podcast
Discretize Data Discretize continuous datasets for Bayesian network modelling. — —
Data Types Discrete, continuous and mixed datasets. — —
Comparisons Compare causal discovery libraries and their ability to find hidden causal relationships. link Podcast
LLMs and Causal Discovery Benchmark large language models against Bayesian methods for causal discovery. link —


The following functions are available after installation:

Feature Description
Key Pipelines
Structure learning bn.structure_learning.fit()
Parameter learning bn.parameter_learning.fit()
Inference bn.inference.fit()
Make predictions bn.predict()
Generate Synthetic Data bn.sampling()
Compute Edge Strength bn.independence_test()
Key Functions
Imputations bn.knn_imputer()
Discretizing bn.discretize()
Check Model Parameters bn.check_model()
Create DAG bn.make_DAG()
Get Node Properties bn.get_node_properties()
Get Edge Properties bn.get_edge_properties()
Get Parents From Edges bn.get_parents()
Generate Default CPT per Node bn.generate_cpt()
Generate Default CPTs for All Edges bn.build_cpts_from_structure()
Make Plots
Plotting bn.plot()
Plot Graphviz bn.plot_graphviz()
Compare 2 Networks bn.compare()
Load DAG (bif files) bn.import_BIF()
Load Examples bn.import_example()
Transformation Functions
Convert DAG to Undirected bn.to_undirected()
Convert to one-hot bn.df2onehot()
Convert Adjacency Matrix to Vector bn.adjmat2vec()
Convert Adjacency Matrix to Dictionary bn.adjmat2dict()
Convert Vector to Adjacency Matrix bn.vec2adjmat()
Convert DAG to Adjacency Matrix bn.dag2adjmat()
Convert DataFrame to Onehot bn.df2onehot()
Convert Query to DataFrame bn.query2df()
Convert Vector to DataFrame bn.vec2df()
Metrics
Compute Topological Ordering bn.topological_sort()
Compute Structure Scores bn.structure_scores()
General
Save Model bn.save()
Load Model bn.load()
Print CPTs bn.print_CPD()

Installation

Python 3.10 or newer is required.

Installation: uv-pypi
uv venv
.venv\Scripts\activate
uv pip install bnlearn
Installation: uv-local
uv venv
.venv\Scripts\activate
git clone https://github.com/erdogant/bnlearn.git
cd bnlearn
uv pip install -U -e .
Installation: conda-pypi
conda create -n env_bnlearn python=3.12
conda activate env_bnlearn
pip install bnlearn
Installation: GitHub source
pip install git+https://github.com/erdogant/bnlearn
uv pip install git+https://github.com/erdogant/bnlearn
Run tests (uv)
uv pip install -e ".[dev]"
uv run pytest
Run tests (pip)
pip install -e ".[dev]"
pytest -v
Install skill:
npx skills add erdogant/bnlearn --skill bnlearn

Load library

# Import library
import bnlearn as bn

Code Examples


    import bnlearn as bn
    # Example dataframe sprinkler_data.csv can be loaded with:
    df = bn.import_example()
    # df = pd.read_csv('sprinkler_data.csv')

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


    model = bn.structure_learning.fit(df)
    # Compute edge strength with the chi-square test statistic
    model = bn.independence_test(model, df)
    G = bn.plot(model)


# Example: Structure Learning

    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')
    model_cl      = bn.structure_learning.fit(df, methodtype='cl', root_node='Wet_Grass')
    model_tan     = bn.structure_learning.fit(df, methodtype='tan', root_node='Wet_Grass', class_node='Rain')

# Example: Parameter Learning

    import bnlearn as bn
    # Import dataframe
    df = bn.import_example()
    # As an example we set the CPD at False which returns an "empty" DAG
    model = bn.import_BIF('sprinkler', CPD=False)
    # Now we learn the parameters of the DAG using the df
    model_update = bn.parameter_learning.fit(model, df)
    # Make plot
    G = bn.plot(model_update)

# Example: Inference

    import bnlearn as bn
    model = bn.import_BIF('sprinkler')
    query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1,'Sprinkler':0, 'Wet_Grass':1})
    print(query)
    print(query.df)

    # Lets try another inference
    query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1})
    print(query)
    print(query.df)


Star history

Star History Chart

Contributors

Thank the contributors!

Maintainer

  • Erdogan Taskesen, github: erdogant
  • Contributions are welcome.
  • Yes! This library is entirely free but it runs on coffee! :) Feel free to support with a Coffee.

Buy me a coffee

评论

暂无评论