Skip to main content

Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods.

Project description

bnlearn - Graphical structure of Bayesian networks

Python PyPI Version License Coffee Downloads Sphinx

Star it if you like it!

Bnlearn is Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods. This work is inspired by the R package (bnlearn.com) that has been very usefull to me for many years. Although there are very good Python packages for probabilistic graphical models, it still can remain difficult (and somethimes unnecessarily) to (re)build certain pipelines. Bnlearn for python (this package) is build on the pgmpy package and contains the most-wanted pipelines. Navigate to API documentations for more detailed information.

Method overview

Learning a Bayesian network can be split into two problems which are both implemented in this package:

  • Structure learning: Given a set of data samples, estimate a DAG that captures the dependencies between the variables.
  • Parameter learning: Given a set of data samples and a DAG that captures the dependencies between the variables, e stimate the (conditional) probability distributions of the individual variables.

The following functions are available after importing bnlearn.

# Structure learning
bnlearn.structure_learning.fit()
# Parameter learning
bnlearn.parameter_learning.fit()
# Inference
bnlearn.inference.fit()
# Based on a DAG, you can sample the number of samples you want.
bnlearn.sampling()
# Load well known examples to play arround with or load your own .bif file.
bnlearn.import_DAG()
# Load simple dataframe of sprinkler dataset.
bnlearn.import_example()
# Compare 2 graphs
bnlearn.compare_networks()
# Plot graph
bnlearn.plot()
# To make the directed grapyh undirected
bnlearn.to_undirected()
# Convert to one-hot datamatrix
bnlearn.df2onehot()

# See below for the exact working of the functions

The following methods are also included:

  • inference
  • sampling
  • comparing two networks
  • loading bif files
  • conversion of directed to undirected graphs

Contents

Installation

  • Install bnlearn from PyPI (recommended). bnlearn is compatible with Python 3.6+ and runs on Linux, MacOS X and Windows.
  • It is distributed under the MIT license.

Requirements

  • It is advisable to create a new environment.
conda create -n env_BNLEARN python=3.6
conda activate env_BNLEARN
conda install -c ankurankan pgmpy
#conda install pytorch -c pytorch

# You may need to deactivate and then activate your environment otherwise the packages may not been recognized.
conda deactivate
conda activate env_BNLEARN

# The packages below are handled by the requirements in the bnlearn pip installer. So you dont need to do them manually.
pip install sklearn pandas tqdm funcsigs statsmodels community packaging

Quick Start

pip install bnlearn
  • Alternatively, install bnlearn from the GitHub source:
git clone https://github.com/erdogant/bnlearn.git
cd bnlearn
python setup.py install

Import bnlearn package

 import bnlearn

Example: Structure Learning

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

df looks like this

     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

  • Choosing various methodtypes and scoringtypes:
model_hc_bic  = bnlearn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
model_hc_k2   = bnlearn.structure_learning.fit(df, methodtype='hc', scoretype='k2')
model_hc_bdeu = bnlearn.structure_learning.fit(df, methodtype='hc', scoretype='bdeu')
model_ex_bic  = bnlearn.structure_learning.fit(df, methodtype='ex', scoretype='bic')
model_ex_k2   = bnlearn.structure_learning.fit(df, methodtype='ex', scoretype='k2')
model_ex_bdeu = bnlearn.structure_learning.fit(df, methodtype='ex', scoretype='bdeu')

Example: Parameter Learning

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

Example: Inference

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

Example: Sampling to create dataframe

model = bnlearn.import_DAG('sprinkler')
df = bnlearn.sampling(model, n=1000)
  • Output of the model:
[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)

Example: Loading DAG from bif files

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 dataset
model = bnlearn.import_DAG(bif_file)

Example: Comparing networks

# Load asia DAG
model = bnlearn.import_DAG('asia')
# plot ground truth
G = bnlearn.plot(model)
# Sampling
df = bnlearn.sampling(model, n=10000)
# Structure learning of sampled dataset
model_sl = bnlearn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
# Plot based on structure learning of sampled data
bnlearn.plot(model_sl, pos=G['pos'])
# Compare networks and make plot
bnlearn.compare_networks(model, model_sl, pos=G['pos'])

Graph of ground truth

Graph based on Structure learning

Graph comparison ground truth vs. structure learning

Citation

Please cite bnlearn in your publications if this is useful for your research. Here is an example BibTeX entry:

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

References

Maintainer

  • Erdogan Taskesen, github: erdogant
  • Contributions are welcome.
  • If you wish to buy me a Coffee for this work, it is very appreciated :)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bnlearn-0.3.4.tar.gz (323.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bnlearn-0.3.4-py3-none-any.whl (325.5 kB view details)

Uploaded Python 3

File details

Details for the file bnlearn-0.3.4.tar.gz.

File metadata

  • Download URL: bnlearn-0.3.4.tar.gz
  • Upload date:
  • Size: 323.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

File hashes

Hashes for bnlearn-0.3.4.tar.gz
Algorithm Hash digest
SHA256 4ae72412b954b5ce46a0852882a27e239d2e84e75e098349e3ef605047c50bce
MD5 8b52b0391dab42a404ed08c3dba65f34
BLAKE2b-256 5cf92054799d3d2e3c2d09ab744ea73f9967aef65953f1d72eaa2479693ae863

See more details on using hashes here.

File details

Details for the file bnlearn-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: bnlearn-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 325.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

File hashes

Hashes for bnlearn-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0ba340300d865e2495ea9f438eb186552d18aaaf4756e2173c70439d896ac535
MD5 44d87f68f6f51d37f6d20c8106d93ffc
BLAKE2b-256 e820ce21d3c36f00e1f923bc4f7418bd0ebee2d92d61df70851dd084e2a0dc25

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page