class_docstr: Initialize a `DiscreteFactor` class.
  
  Defined above, we have the following mapping from variable
  assignments to the index of the row vector in the value field:
  +-----+-----+-----+-------------------+
  |  x1 |  x2 |  x3 |    phi(x1, x2, x3)|
  +-----+-----+-----+-------------------+
  | x1_0| x2_0| x3_0|     phi.value(0)  |
  +-----+-----+-----+-------------------+
  | x1_0| x2_0| x3_1|     phi.value(1)  |
  +-----+-----+-----+-------------------+
  | x1_0| x2_1| x3_0|     phi.value(2)  |
  +-----+-----+-----+-------------------+
  | x1_0| x2_1| x3_1|     phi.value(3)  |
  +-----+-----+-----+-------------------+
  | x1_1| x2_0| x3_0|     phi.value(4)  |
  +-----+-----+-----+-------------------+
  | x1_1| x2_0| x3_1|     phi.value(5)  |
  +-----+-----+-----+-------------------+
  | x1_1| x2_1| x3_0|     phi.value(6)  |
  +-----+-----+-----+-------------------+
  | x1_1| x2_1| x3_1|     phi.value(7)  |
  +-----+-----+-----+-------------------+
  
  Parameters
  ----------
  variables: list, array-like
      List of variables on which the factor is to be defined i.e. scope of the factor.
  
  cardinality: list, array_like
      List of cardinalities/no.of states of each variable. `cardinality`
      array must have a value corresponding to each variable in
      `variables`.
  
  values: list, array_like
      List of values of factor.
      A DiscreteFactor's values are stored in a row vector in the value
      using an ordering such that the left-most variables as defined in
      `variables` cycle through their values the fastest. Please refer
      to examples for usage examples.
  
  Examples
  --------
  >>> import numpy as np
  >>> from pgmpy.factors.discrete import DiscreteFactor
  >>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 2, 2], np.ones(8))
  >>> phi
  <DiscreteFactor representing phi(x1:2, x2:2, x3:2) at 0x7f8188fcaa90>
  >>> print(phi)
  +------+------+------+-----------------+
  | x1   | x2   | x3   |   phi(x1,x2,x3) |
  |------+------+------+-----------------|
  | x1_0 | x2_0 | x3_0 |          1.0000 |
  | x1_0 | x2_0 | x3_1 |          1.0000 |
  | x1_0 | x2_1 | x3_0 |          1.0000 |
  | x1_0 | x2_1 | x3_1 |          1.0000 |
  | x1_1 | x2_0 | x3_0 |          1.0000 |
  | x1_1 | x2_0 | x3_1 |          1.0000 |
  | x1_1 | x2_1 | x3_0 |          1.0000 |
  | x1_1 | x2_1 | x3_1 |          1.0000 |
  +------+------+------+-----------------+
class_docstr_tok: ["Initialize","a","`","DiscreteFactor","`","class",".","Defined","above",",","we","have","the","following","mapping","from","variable","assignments","to","the","index","of","the","row","vector","in","the","value","field",":","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1","|","x2","|","x3","|","phi","(","x1",",","x2",",","x3",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1_0|","x2_0|","x3_0|","phi.value","(","0",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1_0|","x2_0|","x3_1|","phi.value","(","1",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1_0|","x2_1|","x3_0|","phi.value","(","2",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1_0|","x2_1|","x3_1|","phi.value","(","3",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1_1|","x2_0|","x3_0|","phi.value","(","4",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1_1|","x2_0|","x3_1|","phi.value","(","5",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1_1|","x2_1|","x3_0|","phi.value","(","6",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","|","x1_1|","x2_1|","x3_1|","phi.value","(","7",")","|","+","--","--","-+","--","--","-+","--","--","-+","--","--","--","--","--","--","--","--","--","-+","Parameters","--","--","--","--","--","variables",":","list",",","array-like","List","of","variables","on","which","the","factor","is","to","be","defined","i.e",".","scope","of","the","factor",".","cardinality",":","list",",","array_like","List","of","cardinalities\/no.of","states","of","each","variable",".","`","cardinality","`","array","must","have","a","value","corresponding","to","each","variable","in","`","variables","`",".","values",":","list",",","array_like","List","of","values","of","factor",".","A","DiscreteFactor","'s","values","are","stored","in","a","row","vector","in","the","value","using","an","ordering","such","that","the","left-most","variables","as","defined","in","`","variables","`","cycle","through","their","values","the","fastest",".","Please","refer","to","examples","for","usage","examples",".","Examples","--","--","--","--",">",">",">","import","numpy","as","np",">",">",">","from","pgmpy.factors.discrete","import","DiscreteFactor",">",">",">","phi","=","DiscreteFactor","(","[","'x1","'",",","'x2","'",",","'x3","'","]",",","[","2",",","2",",","2","]",",","np.ones","(","8",")",")",">",">",">","phi","<","DiscreteFactor","representing","phi","(","x1:2",",","x2:2",",","x3:2",")","at","0x7f8188fcaa90",">",">",">",">","print","(","phi",")","+","--","--","--","+","--","--","--","+","--","--","--","+","--","--","--","--","--","--","--","--","-+","|","x1","|","x2","|","x3","|","phi","(","x1",",","x2",",","x3",")","|","|","--","--","--","+","--","--","--","+","--","--","--","+","--","--","--","--","--","--","--","--","-|","|","x1_0","|","x2_0","|","x3_0","|","1.0000","|","|","x1_0","|","x2_0","|","x3_1","|","1.0000","|","|","x1_0","|","x2_1","|","x3_0","|","1.0000","|","|","x1_0","|","x2_1","|","x3_1","|","1.0000","|","|","x1_1","|","x2_0","|","x3_0","|","1.0000","|","|","x1_1","|","x2_0","|","x3_1","|","1.0000","|","|","x1_1","|","x2_1","|","x3_0","|","1.0000","|","|","x1_1","|","x2_1","|","x3_1","|","1.0000","|","+","--","--","--","+","--","--","--","+","--","--","--","+","--","--","--","--","--","--","--","--","-+"]
docstr: Modifies the factor with marginalized values.
  
  Parameters
  ----------
  variables: list, array-like
      List of variables over which to marginalize.
  
  inplace: boolean
      If inplace=True it will modify the factor itself, else would return
      a new factor.
  
  Returns
  -------
  Marginalized factor: pgmpy.factors.discrete.DiscreteFactor or None
  If inplace=True (default) returns None else returns a new `DiscreteFactor` instance.
  
  Examples
  --------
  >>> from pgmpy.factors.discrete import DiscreteFactor
  >>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
  >>> phi.marginalize(['x1', 'x3'])
  >>> phi.values
  array([14., 22., 30.])
  >>> phi.variables
  ['x2']
docstr_tok: ["Modifies","the","factor","with","marginalized","values",".","Parameters","--","--","--","--","--","variables",":","list",",","array-like","List","of","variables","over","which","to","marginalize",".","inplace",":","boolean","If","inplace=True","it","will","modify","the","factor","itself",",","else","would","return","a","new","factor",".","Returns","--","--","--","-","Marginalized","factor",":","pgmpy.factors.discrete.DiscreteFactor","or","None","If","inplace=True","(","default",")","returns","None","else","returns","a","new","`","DiscreteFactor","`","instance",".","Examples","--","--","--","--",">",">",">","from","pgmpy.factors.discrete","import","DiscreteFactor",">",">",">","phi","=","DiscreteFactor","(","[","'x1","'",",","'x2","'",",","'x3","'","]",",","[","2",",","3",",","2","]",",","range","(","12",")",")",">",">",">","phi.marginalize","(","[","'x1","'",",","'x3","'","]",")",">",">",">","phi.values","array","(","[","14.",",","22.",",","30",".","]",")",">",">",">","phi.variables","[","'x2","'","]"]
code: def marginalize(self, variables, inplace=True):
      """
      Modifies the factor with marginalized values.
  
      Parameters
      ----------
      variables: list, array-like
          List of variables over which to marginalize.
  
      inplace: boolean
          If inplace=True it will modify the factor itself, else would return
          a new factor.
  
      Returns
      -------
      Marginalized factor: pgmpy.factors.discrete.DiscreteFactor or None
      If inplace=True (default) returns None else returns a new `DiscreteFactor` instance.
  
      Examples
      --------
      >>> from pgmpy.factors.discrete import DiscreteFactor
      >>> phi = DiscreteFactor(['x1', 'x2', 'x3'], [2, 3, 2], range(12))
      >>> phi.marginalize(['x1', 'x3'])
      >>> phi.values
      array([14., 22., 30.])
      >>> phi.variables
      ['x2']
      """
  
      if isinstance(variables, str):
          raise TypeError(
              "variables: Expected type list or array-like, got type str"
          )
  
      phi = self if inplace else self.copy()
  
      for var in variables:
          if var not in phi.variables:
              raise ValueError(f"{var} not in scope.")
  
      var_indexes = [phi.variables.index(var) for var in variables]
  
      index_to_keep = sorted(
          set(range(len(self.variables))) - set(var_indexes)
      )
      n_variables = len(self.variables)
      phi.variables = [phi.variables[index] for index in index_to_keep]
      phi.cardinality = phi.cardinality[index_to_keep]
      phi.del_state_names(variables)
  
      phi.values = compat_fns.einsum(
          phi.values, range(n_variables), index_to_keep
      )
  
      if not inplace:
          return phi
  
imports: from collections import namedtuple
  from itertools import product
  import numpy
  import pandas
  import torch
  from pgmpy import config
  from pgmpy.extern import tabulate
  from pgmpy.factors.base import BaseFactor
  from pgmpy.global_vars import logger
  from pgmpy.utils import StateNameMixin, compat_fns
label_desc: Use image node_id 8 for calling the DiscreteFactor obj's underlying member method code with example usage: obj.marginalize(variables, inplace) and returns: phi