bnlearn contains several examples within the library that can be used to practice with the functionalities of bnlearn.structure_learning(), bnlearn.parameter_learning() and bnlearn.inference().

DataFrames

The sprinkler dataset is one of the few internal datasets to import a pandas dataframe. This dataset is readily one-hot coded and without missing values. Therefore it does not require any further pre-processing steps. Note that

import bnlearn as bn
# Import dataset
df = bn.import_example('sprinkler')

print(df)
#     Cloudy  Sprinkler  Rain  Wet_Grass
# 0         0          0     0          0
# 1         1          0     1          1
# 2         0          1     0          1
# ..      ...        ...   ...        ...
# 998       0          0     0          0
# 999       0          1     1          1

# Structure learning
model = bn.structure_learning.fit(df)

# Plot
G = bn.plot(model)

Import DAG/BIF

Each Bayesian DAG model that is loaded with bnlearn.bnlearn.import_DAG() is derived from a bif file. The bif file is a common format for Bayesian networks that can be used for the exchange of knowledge and experimental results in the community. More information can be found (here)[http://www.cs.washington.edu/dm/vfml/appendixes/bif.htm].

import bnlearn as bn

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 DAG with model parameters from bif file.
model = bn.import_DAG(bif_file)

With the bnlearn.bnlearn.sampling() function a DataFrame can be created for n samples.

Export DAG/BIF

The learned bayesian network can be exported in one of common bayes network formats, like BIF, hugin or XMLBIF by using the BIFwriter from pgmpy.

# Import packages
import pandas as pd
from pgmpy.readwrite import BIFWriter
import bnlearn as bn

# Import dataset
df = bn.import_example('sprinkler')
# build model
model = bn.structure_learning.fit(df)
model = bn.parameter_learning.fit(model, df)

# Write to BIF
writer = BIFWriter(model['model'])
writer.write_bif(filename='model.bif')