class_docstr: Base class for Markov Model.
  
  A MarkovNetwork stores nodes and edges with potentials
  
  MarkovNetwork holds undirected edges.
  
  Parameters
  ----------
  data : input graph
      Data to initialize graph.  If data=None (default) an empty
      graph is created.  The data can be an edge list, or any
      NetworkX graph object.
  
  Examples
  --------
  Create an empty Markov Model with no nodes and no edges.
  
  >>> from pgmpy.models import MarkovNetwork
  >>> G = MarkovNetwork()
  
  G can be grown in several ways.
  
  **Nodes:**
  
  Add one node at a time:
  
  >>> G.add_node('a')
  
  Add the nodes from any container (a list, set or tuple or the nodes
  from another graph).
  
  >>> G.add_nodes_from(['a', 'b'])
  
  **Edges:**
  
  G can also be grown by adding edges.
  
  Add one edge,
  
  >>> G.add_edge('a', 'b')
  
  a list of edges,
  
  >>> G.add_edges_from([('a', 'b'), ('b', 'c')])
  
  If some edges connect nodes not yet in the model, the nodes
  are added automatically.  There are no errors when adding
  nodes or edges that already exist.
  
  **Shortcuts:**
  
  Many common graph features allow python syntax for speed reporting.
  
  >>> 'a' in G     # check if node in graph
  True
  >>> len(G)  # number of nodes in graph
  3
class_docstr_tok: ["Base","class","for","Markov","Model",".","A","MarkovNetwork","stores","nodes","and","edges","with","potentials","MarkovNetwork","holds","undirected","edges",".","Parameters","--","--","--","--","--","data",":","input","graph","Data","to","initialize","graph",".","If","data=None","(","default",")","an","empty","graph","is","created",".","The","data","can","be","an","edge","list",",","or","any","NetworkX","graph","object",".","Examples","--","--","--","--","Create","an","empty","Markov","Model","with","no","nodes","and","no","edges",".",">",">",">","from","pgmpy.models","import","MarkovNetwork",">",">",">","G","=","MarkovNetwork","(",")","G","can","be","grown","in","several","ways",".","*","*","Nodes",":","*","*","Add","one","node","at","a","time",":",">",">",">","G.add_node","(","'","a","'",")","Add","the","nodes","from","any","container","(","a","list",",","set","or","tuple","or","the","nodes","from","another","graph",")",".",">",">",">","G.add_nodes_from","(","[","'","a","'",",","'","b","'","]",")","*","*","Edges",":","*","*","G","can","also","be","grown","by","adding","edges",".","Add","one","edge",",",">",">",">","G.add_edge","(","'","a","'",",","'","b","'",")","a","list","of","edges",",",">",">",">","G.add_edges_from","(","[","(","'","a","'",",","'","b","'",")",",","(","'","b","'",",","'","c","'",")","]",")","If","some","edges","connect","nodes","not","yet","in","the","model",",","the","nodes","are","added","automatically",".","There","are","no","errors","when","adding","nodes","or","edges","that","already","exist",".","*","*","Shortcuts",":","*","*","Many","common","graph","features","allow","python","syntax","for","speed","reporting",".",">",">",">","'","a","'","in","G","#","check","if","node","in","graph","True",">",">",">","len","(","G",")","#","number","of","nodes","in","graph","3"]
docstr: Converts the Markov Model into Factor Graph.
  
  A Factor Graph contains two types of nodes. One type corresponds to
  random variables whereas the second type corresponds to factors over
  these variables. The graph only contains edges between variables and
  factor nodes. Each factor node is associated with one factor whose
  scope is the set of variables that are its neighbors.
  
  Examples
  --------
  >>> from pgmpy.models import MarkovNetwork
  >>> from pgmpy.factors.discrete import DiscreteFactor
  >>> student = MarkovNetwork([('Alice', 'Bob'), ('Bob', 'Charles')])
  >>> factor1 = DiscreteFactor(['Alice', 'Bob'], [3, 2], np.random.rand(6))
  >>> factor2 = DiscreteFactor(['Bob', 'Charles'], [2, 2], np.random.rand(4))
  >>> student.add_factors(factor1, factor2)
  >>> factor_graph = student.to_factor_graph()
docstr_tok: ["Converts","the","Markov","Model","into","Factor","Graph",".","A","Factor","Graph","contains","two","types","of","nodes",".","One","type","corresponds","to","random","variables","whereas","the","second","type","corresponds","to","factors","over","these","variables",".","The","graph","only","contains","edges","between","variables","and","factor","nodes",".","Each","factor","node","is","associated","with","one","factor","whose","scope","is","the","set","of","variables","that","are","its","neighbors",".","Examples","--","--","--","--",">",">",">","from","pgmpy.models","import","MarkovNetwork",">",">",">","from","pgmpy.factors.discrete","import","DiscreteFactor",">",">",">","student","=","MarkovNetwork","(","[","(","'Alice","'",",","'Bob","'",")",",","(","'Bob","'",",","'Charles","'",")","]",")",">",">",">","factor1","=","DiscreteFactor","(","[","'Alice","'",",","'Bob","'","]",",","[","3",",","2","]",",","np.random.rand","(","6",")",")",">",">",">","factor2","=","DiscreteFactor","(","[","'Bob","'",",","'Charles","'","]",",","[","2",",","2","]",",","np.random.rand","(","4",")",")",">",">",">","student.add_factors","(","factor1",",","factor2",")",">",">",">","factor_graph","=","student.to_factor_graph","(",")"]
code: def to_factor_graph(self):
      """
      Converts the Markov Model into Factor Graph.
  
      A Factor Graph contains two types of nodes. One type corresponds to
      random variables whereas the second type corresponds to factors over
      these variables. The graph only contains edges between variables and
      factor nodes. Each factor node is associated with one factor whose
      scope is the set of variables that are its neighbors.
  
      Examples
      --------
      >>> from pgmpy.models import MarkovNetwork
      >>> from pgmpy.factors.discrete import DiscreteFactor
      >>> student = MarkovNetwork([('Alice', 'Bob'), ('Bob', 'Charles')])
      >>> factor1 = DiscreteFactor(['Alice', 'Bob'], [3, 2], np.random.rand(6))
      >>> factor2 = DiscreteFactor(['Bob', 'Charles'], [2, 2], np.random.rand(4))
      >>> student.add_factors(factor1, factor2)
      >>> factor_graph = student.to_factor_graph()
      """
      from pgmpy.models import FactorGraph
  
      factor_graph = FactorGraph()
  
      if not self.factors:
          raise ValueError(
              "Factors not associated with the random variables."
          )
  
      factor_graph.add_nodes_from(self.nodes())
      for factor in self.factors:
          scope = factor.scope()
          factor_node = "phi_" + "_".join(scope)
          factor_graph.add_edges_from(
              itertools.product(scope, [factor_node])
          )
          factor_graph.add_factors(factor)
  
      return factor_graph
  
imports: import itertools
  from collections import defaultdict
  import networkx
  import numpy
  from networkx.algorithms.components import connected_components
  from pgmpy.base import UndirectedGraph
  from pgmpy.factors import factor_product
  from pgmpy.factors.discrete import DiscreteFactor
  from pgmpy.independencies import Independencies
label_desc: Use image node_id 9 for calling the MarkovNetwork obj's underlying member method code with example usage: obj.to_factor_graph() and returns: factor_graph