erdogant/bnlearn
Python package for Causal Discovery by learning the graphical structure of Bayesian networks. Structure Learning, Parameter Learning, Inferences, Sampling methods.
bnlearn – Python library for Bayesian‑network learning & causal analysis
What it does – bnlearn lets you build, train, and query probabilistic graphical models (Bayesian networks). It covers the whole pipeline:
- Structure learning – discover the directed‑acyclic graph (DAG) that best explains your data, using classic scores (BIC, K2, BDeu) and search strategies (exhaustive, hill‑climbing, Chow‑Liu, TAN, etc.).
- Parameter learning – estimate conditional probability tables (CPTs) for a given DAG, even with incomplete data.
- Inference & prediction – compute posterior, interventional (do‑calculus) and counter‑factual queries, and make causal predictions.
- Synthetic data & utilities – sample new data from a learned model, discretise continuous variables, impute missing values, and convert between graph representations (adjacency matrix, edge list, Graphviz).
- Visualization – plot the network structure with Matplotlib or Graphviz, compare two networks, and export models.
All of this is wrapped in a tidy, high‑level API (bn.structure_learning.fit(), bn.parameter_learning.fit(), bn.inference.fit(), bn.predict(), …) so you can go from a raw pandas DataFrame to a fully‑fledged causal model in a few lines of code.
Quick start
# Install (Python 3.10+ required)
pip install bnlearn # or via conda, uv, or directly from GitHub
import bnlearn as bn
# Load an example dataset (the classic sprinkler network)
df = bn.import_example()
# 1️⃣ Learn the network structure from data
model = bn.structure_learning.fit(df)
# 2️⃣ Optionally compute edge‑strength statistics
model = bn.independence_test(model, df)
# 3️⃣ Visualise the learned DAG
bn.plot(model)
# 4️⃣ Learn CPTs (parameter learning)
model = bn.parameter_learning.fit(model, df)
# 5️⃣ Perform causal inference
query = bn.inference.fit(model, variables=['Rain'],
evidence={'Cloudy': 1, 'Sprinkler': 0, 'Wet_Grass': 1})
print(query.df)
The library also provides ready‑made pipelines for specific tasks (e.g., bn.sampling() to generate synthetic data, bn.knn_imputer() for missing‑value imputation, bn.discretize() for binning continuous features).
Where it fits
- Causal discovery – ideal for researchers and data scientists who need to uncover directed relationships from observational data.
- Probabilistic modelling – useful for any domain where uncertainty must be quantified (medicine, finance, reliability engineering, etc.).
- Education & prototyping – the extensive documentation, notebooks, and blog‑post tutorials make it a good teaching tool for Bayesian‑network concepts.
Documentation & community
- Full API docs – https://erdogant.github.io/bnlearn/
- Example notebooks – Google‑Colab links from the docs.
- Blog & podcasts – medium articles and short audio explainers for each major feature.
- Open source – MIT‑licensed, actively maintained (≈ 2 k stars, regular releases), and welcomes contributions.
TL;DR
bnlearn is a well‑documented, pip‑installable Python package that streamlines the end‑to‑end workflow of Bayesian‑network based causal modelling: discover structure, learn parameters, run inference, generate data, and visualise results—all with a few high‑level function calls.
関連
- プロジェクト
- プロジェクト
- プロジェクト
- プロジェクト
- プロジェクト