top of page

Machine Learning

Public·3 members

Create bayesian model from pgmpy.models

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).

G.add_nodes_from(['a', 'b'])

Here a, and b are two nodes


Adding edges to Bayesian Model


  • Add one edge

G.add_edge('a', 'b')
  • 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

len(G) 


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)
print(grades_cpd)

Create BayesianModel in python
Create BayesianModel in python

#BayesianModel #MachineLearning #pgmpy #python

480 Views
bottom of page