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. | link | --- |
| Parameter Learning | Estimate model parameters (e.g., conditional probability distributions) from observed data. | link | Podcast |
| Causal Inference | Compute interventional and counterfactual distributions using do-calculus. | link | Podcast |
| Generate Synthetic Data | Generate synthetic data. | link | Podcast |
| Discretize Data | Discretize continuous datasets. | --- | --- |
| Data Types | Discrete, continuous and mixed datasets. | --- | --- |
| Comparisons | Comparison with other causal libraries. | link | Podcast |
Resources and Links
- Example Notebooks: Examples
- Blog Posts: Medium
- Documentation: Website
- Bug Reports and Feature Requests: GitHub Issues
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
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
Installation of bnlearn skills
Installation of bnlearn skills for various harnesses. Default is .claude
Install skill (default claude)
bnlearn skill install
Install skill: harnass options {claude, opencode agents or any other name}
bnlearn skill install --harness opencode
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_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)
Star history
Contributors
Thank the contributors!
Maintainer
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bnlearn-0.14.2.tar.gz.
File metadata
- Download URL: bnlearn-0.14.2.tar.gz
- Upload date:
- Size: 170.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d968625d452b40b8eb6b39575cf61c9567b8af564ebe5f022c4d3b137224f78f
|
|
| MD5 |
b6d131339a58f8f03139c2bfd74ef775
|
|
| BLAKE2b-256 |
92f11c3f26895799859b5de0d60010e3b7e3d1c3d3aea1b9c4c9cf65bbf06d67
|
Provenance
The following attestation bundles were made for bnlearn-0.14.2.tar.gz:
Publisher:
release.yml on erdogant/bnlearn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bnlearn-0.14.2.tar.gz -
Subject digest:
d968625d452b40b8eb6b39575cf61c9567b8af564ebe5f022c4d3b137224f78f - Sigstore transparency entry: 2639567912
- Sigstore integration time:
-
Permalink:
erdogant/bnlearn@e9b14cffd09a74180f82a55ec89999b95b1af959 -
Branch / Tag:
refs/tags/0.14.2 - Owner: https://github.com/erdogant
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e9b14cffd09a74180f82a55ec89999b95b1af959 -
Trigger Event:
release
-
Statement type:
File details
Details for the file bnlearn-0.14.2-py3-none-any.whl.
File metadata
- Download URL: bnlearn-0.14.2-py3-none-any.whl
- Upload date:
- Size: 185.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d1cca02fe486266384bbc431d8033408dc81c0f72680eeaa96f14b36cae48a1
|
|
| MD5 |
11ef36dd085224be6726203fb38ffcad
|
|
| BLAKE2b-256 |
449b1d25eeb3ca8a46bcb7e0d717549857fadb58fc686927cf30b9127ed70bef
|
Provenance
The following attestation bundles were made for bnlearn-0.14.2-py3-none-any.whl:
Publisher:
release.yml on erdogant/bnlearn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bnlearn-0.14.2-py3-none-any.whl -
Subject digest:
0d1cca02fe486266384bbc431d8033408dc81c0f72680eeaa96f14b36cae48a1 - Sigstore transparency entry: 2639569046
- Sigstore integration time:
-
Permalink:
erdogant/bnlearn@e9b14cffd09a74180f82a55ec89999b95b1af959 -
Branch / Tag:
refs/tags/0.14.2 - Owner: https://github.com/erdogant
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e9b14cffd09a74180f82a55ec89999b95b1af959 -
Trigger Event:
release
-
Statement type: