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: Reduces the factor to the context of given variable values. The variables which
  are reduced would be removed from the factor.
  
  Parameters
  ----------
  values: list, array-like
      A list of tuples of the form (variable_name, variable_state).
  
  inplace: boolean
      If inplace=True it will modify the factor itself, else would return
      a new factor.
  
  show_warnings: boolean
      Whether to show warning when state name not found.
  
  Returns
  -------
  Reduced 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.reduce([('x1', 0), ('x2', 0)])
  >>> phi.variables
  ['x3']
  >>> phi.cardinality
  array([2])
  >>> phi.values
  array([0., 1.])
docstr_tok: ["Reduces","the","factor","to","the","context","of","given","variable","values",".","The","variables","which","are","reduced","would","be","removed","from","the","factor",".","Parameters","--","--","--","--","--","values",":","list",",","array-like","A","list","of","tuples","of","the","form","(","variable_name",",","variable_state",")",".","inplace",":","boolean","If","inplace=True","it","will","modify","the","factor","itself",",","else","would","return","a","new","factor",".","show_warnings",":","boolean","Whether","to","show","warning","when","state","name","not","found",".","Returns","--","--","--","-","Reduced","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.reduce","(","[","(","'x1","'",",","0",")",",","(","'x2","'",",","0",")","]",")",">",">",">","phi.variables","[","'x3","'","]",">",">",">","phi.cardinality","array","(","[","2","]",")",">",">",">","phi.values","array","(","[","0.",",","1",".","]",")"]
code: def reduce(self, values, inplace=True, show_warnings=True):
      """
      Reduces the factor to the context of given variable values. The variables which
      are reduced would be removed from the factor.
  
      Parameters
      ----------
      values: list, array-like
          A list of tuples of the form (variable_name, variable_state).
  
      inplace: boolean
          If inplace=True it will modify the factor itself, else would return
          a new factor.
  
      show_warnings: boolean
          Whether to show warning when state name not found.
  
      Returns
      -------
      Reduced 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.reduce([('x1', 0), ('x2', 0)])
      >>> phi.variables
      ['x3']
      >>> phi.cardinality
      array([2])
      >>> phi.values
      array([0., 1.])
      """
      # Check if values is an array
      if isinstance(values, str):
          raise TypeError(
              "values: Expected type list or array-like, got type str"
          )
  
      if not all(
          [isinstance(state_tuple, tuple) for state_tuple in values]
      ):
          raise TypeError(
              "values: Expected type list of tuples, get type {type}",
              type(values[0]),
          )
  
      # Check if all variables in values are in the factor
      for var, _ in values:
          if var not in self.variables:
              raise ValueError(
                  f"The variable: {var} is not in the factor"
              )
  
      phi = self if inplace else self.copy()
  
      # Convert the state names to state number. If state name not found treat them as
      # state numbers.
      try:
          values = [
              (var, self.get_state_no(var, state_name))
              for var, state_name in values
          ]
      except KeyError:
          if show_warnings:
              logger.warning(
                  "Found unknown state name. Trying to switch to using all state names as state numbers"
              )
  
      var_index_to_del = []
      slice_ = [slice(None)] * len(self.variables)
      for var, state in values:
          var_index = phi.variables.index(var)
          slice_[var_index] = state
          var_index_to_del.append(var_index)
  
      var_index_to_keep = sorted(
          set(range(len(phi.variables))) - set(var_index_to_del)
      )
      # set difference is not guaranteed to maintain ordering
      phi.variables = [
          phi.variables[index] for index in var_index_to_keep
      ]
      phi.cardinality = phi.cardinality[var_index_to_keep]
      phi.del_state_names([var for var, _ in values])
  
      phi.values = phi.values[tuple(slice_)]
  
      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 11 for calling the DiscreteFactor obj's underlying member method code with example usage: obj.reduce(values, inplace, show_warnings) and returns: phi