bnlearn

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

https://github.com/erdogant/bnlearn

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: zenodo.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (12.6%) to scientific vocabulary

Keywords

bayesian-inference causal causal-models causal-networks causality-analysis dag directed-acyclic-graph machine-learning parameter-learning python sampling-methods structure-learning
Last synced: 6 months ago · JSON representation

Repository

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

Basic Info
Statistics
  • Stars: 531
  • Watchers: 7
  • Forks: 51
  • Open Issues: 11
  • Releases: 85
Topics
bayesian-inference causal causal-models causal-networks causality-analysis dag directed-acyclic-graph machine-learning parameter-learning python sampling-methods structure-learning
Created about 6 years ago · Last pushed 8 months ago
Metadata Files
Readme Funding License Citation

README.md

Python PyPI Version GitHub Repo stars License Forks Open Issues Project Status Downloads Downloads DOI Docs Medium GitHub repo size Donate Colab <!---BuyMeCoffee--> <!---Coffee-->

``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](https://erdogant.github.io/bnlearn/) for more detailed information. ** Star it if you like it **

Star history

Star History Chart


Key Pipelines

| Feature | Description | |--------|-------------| | Causal Discovery / Structure Learning | Learn the model structure from data or with expert knowledge. | | Parameter Learning | Estimate model parameters (e.g., conditional probability distributions) from observed data. | | Causal Inference | Compute interventional and counterfactual distributions using do-calculus. | | Generate Synthetic Data | Generate synthetic data. | | Discretize Data | Discretize continuous datasets. |


Resources and Links


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_networks() | | Load DAG (bif files) | bn.import_DAG() | | 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

Install bnlearn from PyPI

bash pip install bnlearn

Install bnlearn from github source

bash pip install git+https://github.com/erdogant/bnlearn

Load library

```python

Import library

import bnlearn as bn

```

Code Examples

```python

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)

```

```python

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_DAG('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_DAG('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)

```


Contributors

Setting up and maintaining bnlearn has been possible thanks to users and contributors. Thanks to:

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

Owner

  • Name: Erdogan
  • Login: erdogant
  • Kind: user
  • Location: Den Haag

Machine Learning | Statistics | Bayesian | D3js | Visualizations

GitHub Events

Total
  • Create event: 7
  • Issues event: 23
  • Release event: 5
  • Watch event: 64
  • Issue comment event: 40
  • Push event: 62
  • Pull request event: 4
  • Fork event: 7
Last Year
  • Create event: 7
  • Issues event: 23
  • Release event: 5
  • Watch event: 64
  • Issue comment event: 40
  • Push event: 62
  • Pull request event: 4
  • Fork event: 7

Committers

Last synced: 11 months ago

All Time
  • Total Commits: 942
  • Total Committers: 8
  • Avg Commits per committer: 117.75
  • Development Distribution Score (DDS): 0.016
Past Year
  • Commits: 95
  • Committers: 3
  • Avg Commits per committer: 31.667
  • Development Distribution Score (DDS): 0.021
Top Committers
Name Email Commits
erdogant e****t@g****m 927
Oliver Verver o****r@s****l 7
ankh1999 8****9 2
Michael Shapiro 1****0 2
Thomas Kraxner 7****o 1
Ivy Lee i****e 1
Ben Evans b****n@b****o 1
Ananyapam De a****7@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 103
  • Total pull requests: 12
  • Average time to close issues: 3 months
  • Average time to close pull requests: 2 days
  • Total issue authors: 76
  • Total pull request authors: 7
  • Average comments per issue: 3.92
  • Average comments per pull request: 0.67
  • Merged pull requests: 12
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 12
  • Pull requests: 3
  • Average time to close issues: about 1 month
  • Average time to close pull requests: about 10 hours
  • Issue authors: 10
  • Pull request authors: 2
  • Average comments per issue: 2.5
  • Average comments per pull request: 1.0
  • Merged pull requests: 3
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • PARODBE (10)
  • Subbui (4)
  • samanemami (4)
  • adam2392 (3)
  • erdogant (3)
  • ms440 (2)
  • piper0124 (2)
  • Mikcy1595 (2)
  • srivhash (2)
  • arainboldt (2)
  • zzzrbx (2)
  • SachiniEMSPE (2)
  • nsankar (1)
  • arita37 (1)
  • chrisflatley (1)
Pull Request Authors
  • oliver3 (4)
  • ankh1999 (2)
  • Ananyapam7 (2)
  • srivhash (2)
  • Kraego (1)
  • ivylee (1)
  • bencevans (1)
  • ms440 (1)
Top Labels
Issue Labels
question (4) Optimization (4) bug (3) enhancement (3)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 11,597 last-month
  • Total dependent packages: 4
    (may contain duplicates)
  • Total dependent repositories: 13
    (may contain duplicates)
  • Total versions: 85
  • Total maintainers: 1
pypi.org: bnlearn

bnlearn is a Python package for Causal Discovery by learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods.

  • Homepage: https://erdogant.github.io/bnlearn
  • Documentation: https://bnlearn.readthedocs.io/
  • License: MIT License Copyright (c) 2020 Erdogan Taskesen bnlearn - Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Latest release: 0.12.0
    published 8 months ago
  • Versions: 84
  • Dependent Packages: 4
  • Dependent Repositories: 13
  • Downloads: 11,597 Last month
Rankings
Dependent packages count: 2.4%
Downloads: 3.1%
Stargazers count: 3.2%
Average: 3.8%
Dependent repos count: 4.0%
Forks count: 6.0%
Maintainers (1)
Last synced: 6 months ago
proxy.golang.org: github.com/erdogant/bnlearn
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 6.5%
Average: 6.7%
Dependent repos count: 7.0%
Last synced: 6 months ago

Dependencies

requirements-dev.txt pypi
  • irelease *
  • pytest *
  • rst2pdf *
  • sphinx *
  • sphinx_rtd_theme *
  • spyder-kernels ==2.2.
requirements.txt pypi
  • community *
  • df2onehot *
  • fsspec *
  • funcsigs *
  • ipywidgets *
  • ismember *
  • matplotlib >=
  • networkx >=
  • numpy *
  • packaging *
  • pandas *
  • pgmpy >=
  • pypickle *
  • pyvis *
  • sklearn *
  • statsmodels *
  • tabulate *
  • tqdm *
  • wget *
setup.py pypi
  • community *
  • df2onehot *
  • fsspec *
  • funcsigs *
  • ipywidgets *
  • ismember *
  • packaging *
  • pandas *
  • pgmpy >=0.1.13
  • pypickle *
  • pyvis *
  • sklearn *
  • statsmodels *
  • tabulate *
  • tqdm *
  • wget *
.github/workflows/codeql-analysis.yml actions
  • actions/checkout v2 composite
  • github/codeql-action/analyze v1 composite
  • github/codeql-action/autobuild v1 composite
  • github/codeql-action/init v1 composite
.github/workflows/pytest.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite