Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods.
Project description
bnlearn - Library for Bayesian network learning and inference
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.
Star this repo if you like it! ⭐️
Blogs
Read the blogs to get a structured overview of bayesian methods and detailed usage of bnlearn
.
Documentation pages
On the documentation pages you can find detailed information about the working of the bnlearn
with many examples.
Installation
It is advisable to create a new environment (e.g. with Conda).
conda create -n env_bnlearn python=3.8
conda activate env_bnlearn
Install bnlearn from PyPI
pip install bnlearn
The following functions are available after installation:
# Import library
import bnlearn as bn
# Structure learning
bn.structure_learning.fit()
# Compute edge strength with the test statistic
bn.independence_test(model, df, test='chi_square', prune=True)
# Parameter learning
bn.parameter_learning.fit()
# Inference
bn.inference.fit()
# Make predictions
bn.predict()
# Based on a DAG, you can sample the number of samples you want.
bn.sampling()
# Load well known examples to play arround with or load your own .bif file.
bn.import_DAG()
# Load simple dataframe of sprinkler dataset.
bn.import_example()
# Compare 2 graphs
bn.compare_networks()
# Plot graph
bn.plot()
# To make the directed grapyh undirected
bn.to_undirected()
# Convert to one-hot datamatrix
bn.df2onehot()
# Derive the topological ordering of the (entire) graph
bn.topological_sort()
# 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
Method overview
Learning a Bayesian network can be split into the underneath problems which are all implemented in this package:
-
Structure learning: Given the data: Estimate a DAG that captures the dependencies between the variables.
- There are multiple manners to perform structure learning.
- Exhaustivesearch
- Hillclimbsearch
- NaiveBayes
- TreeSearch
- Chow-liu
- Tree-augmented Naive Bayes (TAN)
- There are multiple manners to perform structure learning.
-
Parameter learning: Given the data and DAG: Estimate the (conditional) probability distributions of the individual variables.
-
Inference: Given the learned model: Determine the exact probability values for your queries.
Examples
A structured overview of all examples are now available on the documentation pages.
Structure learning
-
Example: Learn structure on the Sprinkler dataset based on a simple dataframe
-
Example: Comparison method and scoring types types for structure learning
Parameter learning
Inferences
Complete examples
Plotting
Various
Unstructured 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')
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)
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 = 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)
Example: Sampling to create dataframe
import bnlearn as bn
model = bn.import_DAG('sprinkler')
df = bn.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: Titanic example
import bnlearn as bn
# Load example mixed dataset
df = bn.import_example(data='titanic')
# Convert to onehot
dfhot, dfnum = bn.df2onehot(df)
# Structure learning
# model = bn.structure_learning.fit(dfnum, methodtype='cl', black_list=['Embarked','Parch','Name'], root_node='Survived', bw_list_method='nodes')
model = bn.structure_learning.fit(dfnum)
# Plot
G = bn.plot(model, interactive=False)
# Compute edge strength with the chi_square test statistic
model = bn.independence_test(model, dfnum, test='chi_square', prune=True)
# Plot
bn.plot(model, interactive=False, pos=G['pos'])
# Parameter learning
model = bn.parameter_learning.fit(model, dfnum)
# Make inference
query = bn.inference.fit(model, variables=['Survived'], evidence={'Sex':True, 'Pclass':True})
print(query)
print(query.df)
# Another inference using only sex for evidence
query = bn.inference.fit(model, variables=['Survived'], evidence={'Sex':0})
print(query)
print(query.df)
# Print model
bn.print_CPD(model)
Plot DAG
Example of interactive plotting
Example of interactive and static plotting
References
- https://erdogant.github.io/bnlearn/
- http://pgmpy.org
- https://programtalk.com/python-examples/pgmpy.factors.discrete.TabularCPD/
- http://www.bnlearn.com/
- http://www.bnlearn.com/bnrepository/
Maintainer
Erdogan Taskesen, github: [erdogant](https://github.com/erdogant)
Please cite in your publications if this is useful for your research (see citation).
All kinds of contributions are welcome!
If you wish to buy me a <a href="https://www.buymeacoffee.com/erdogant">Coffee</a> for this work, it is very appreciated :)
See [LICENSE](LICENSE) for details.
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.