Create BayesianModel from pgmpy.models
There are two ways to create BayesianModel
no nodes and no edges.
with nodes and edges.
Example 1: no nodes and no edges.
from pgmpy.models import BayesianModel G = BayesianModel()
After that we can create any no of nodes or edges or just single node it's up to you.
Adding node to Bayesian Model
Adding single node to Bayesian Model
# Add one node at a time: G.add_node('a')
Adding more than one node to Bayesian Model or Add the nodes from any container (a list, set or tuple or the nodes from another graph).
Here a, and b are two nodes
Adding edges to Bayesian Model
Add one edge
Add a list of edges
G.add_edges_from([('a', 'b'), ('b', 'c')])
Here there are two edges ('a','b') from node a to node b and ('b','c') from node b to node c
check if node in graph
'a' in G
number of nodes in graph
Example 2: with nodes and edges
from pgmpy.models import BayesianModel from pgmpy.factors.discrete.CPD import TabularCPD # ('diff', 'grades'), ('intel', 'grades') are edges student = BayesianModel([('diff', 'grades'), ('intel', 'grades')]) grades_cpd = TabularCPD('grades', 3, [[0.1,0.1,0.1,0.1,0.1,0.1], [0.1,0.1,0.1,0.1,0.1,0.1], [0.8,0.8,0.8,0.8,0.8,0.8]], evidence=['diff', 'intel'], evidence_card=[2, 3]) student.add_cpds(grades_cpd)