pythonbayesian-networks

Library for Drawing the structure and plugging the conditional probability of a bayesian Network?


I am trying to draw the structure of a bayesian network and but I can't wirte by hand the conditional probability. I have trying bnlearn, pomgranate... someone know a library that will allow me?

import pandas as pd
from pomegranate import *
import bnlearn

edges = [
     ('A', 'B'),
    ("B", "C"),
    ("C", "D")]

DAG = bn.make_DAG(edges, verbose =0)
df = pd.DataFrame({'A':[0,0,0,1,0], 'B':[0,0,1,0,0], 'C':[1,1,0,0,1], 'D':[0,1,0,1,1]})
df.head()

model = BayesianNetwork.from_samples(df.to_numpy(), state_names=df.columns.values, algorithm='exact')
print(model)

Someone know how I can add the conditional probability between the variable?


Solution

  • I think that many of them have a way to do that. You can try for instance pyAgrum (https://agrum.org).

    import pyAgrum as gum
    
    # create the Bayesian network
    bn=gum.BayesNet()
    
    #nodes (binary by default)
    bn.add("s")
    bn.add("c")
    
    #arcs
    bn.addArc("c","s")
    
    #cpts
    bn.cpt("c")[:]=[0.1,0.9]
    bn.cpt("s")[:]=[ [0.5,0.5],[0.9,0.1]]
    

    For more, see the notebooks : https://pyagrum.readthedocs.io/en/1.1.1/notebooks.html