class_docstr: Base class for estimators in pgmpy; `ParameterEstimator`,
  `StructureEstimator` and `StructureScore` derive from this class.
  
  Parameters
  ----------
  data: pandas DataFrame object
      object where each column represents one variable.
      (If some values in the data are missing the data cells should be set to `numpy.NaN`.
      Note that pandas converts each column containing `numpy.NaN`s to dtype `float`.)
  
  state_names: dict (optional)
      A dict indicating, for each variable, the discrete set of states (or values)
      that the variable can take. If unspecified, the observed values in the data set
      are taken to be the only possible states.
docstr: Return counts how often each state of 'variable' occurred in the data.
  If a list of parents is provided, counting is done conditionally
  for each state configuration of the parents.
  
  Parameters
  ----------
  variable: string
      Name of the variable for which the state count is to be done.
  
  parents: list
      Optional list of variable parents, if conditional counting is desired.
      Order of parents in list is reflected in the returned DataFrame
  
  weighted: bool
      If True, data must have a `_weight` column specifying the weight of the
      datapoint (row). If False, each datapoint has a weight of `1`.
  
  reindex: bool
      If True, returns a data frame with all possible parents state combinations
      as the columns. If False, drops the state combinations which are not
      present in the data.
  
  Returns
  -------
  state_counts: pandas.DataFrame
      Table with state counts for 'variable'
  
  Examples
  --------
  >>> import pandas as pd
  >>> from pgmpy.estimators import BaseEstimator
  >>> data = pd.DataFrame(data={'A': ['a1', 'a1', 'a2'],
                                'B': ['b1', 'b2', 'b1'],
                                'C': ['c1', 'c1', 'c2']})
  >>> estimator = BaseEstimator(data)
  >>> estimator.state_counts('A')
      A
  a1  2
  a2  1
  >>> estimator.state_counts('C', parents=['A', 'B'])
  A  a1      a2
  B  b1  b2  b1  b2
  C
  c1  1   1   0   0
  c2  0   0   1   0
  >>> estimator.state_counts('C', parents=['A'])
  A    a1   a2
  C
  c1  2.0  0.0
  c2  0.0  1.0
docstr_tok: ["Return","counts","how","often","each","state","of","'variable","'","occurred","in","the","data",".","If","a","list","of","parents","is","provided",",","counting","is","done","conditionally","for","each","state","configuration","of","the","parents",".","Parameters","--","--","--","--","--","variable",":","string","Name","of","the","variable","for","which","the","state","count","is","to","be","done",".","parents",":","list","Optional","list","of","variable","parents",",","if","conditional","counting","is","desired",".","Order","of","parents","in","list","is","reflected","in","the","returned","DataFrame","weighted",":","bool","If","True",",","data","must","have","a","`","_weight","`","column","specifying","the","weight","of","the","datapoint","(","row",")",".","If","False",",","each","datapoint","has","a","weight","of","`","1","`",".","reindex",":","bool","If","True",",","returns","a","data","frame","with","all","possible","parents","state","combinations","as","the","columns",".","If","False",",","drops","the","state","combinations","which","are","not","present","in","the","data",".","Returns","--","--","--","-","state_counts",":","pandas.DataFrame","Table","with","state","counts","for","'variable'","Examples","--","--","--","--",">",">",">","import","pandas","as","pd",">",">",">","from","pgmpy.estimators","import","BaseEstimator",">",">",">","data","=","pd.DataFrame","(","data=","{","'","A","'",":","[","'a1","'",",","'a1","'",",","'a2","'","]",",","'","B","'",":","[","'b1","'",",","'b2","'",",","'b1","'","]",",","'","C","'",":","[","'c1","'",",","'c1","'",",","'c2","'","]","}",")",">",">",">","estimator","=","BaseEstimator","(","data",")",">",">",">","estimator.state_counts","(","'","A","'",")","A","a1","2","a2","1",">",">",">","estimator.state_counts","(","'","C","'",",","parents=","[","'","A","'",",","'","B","'","]",")","A","a1","a2","B","b1","b2","b1","b2","C","c1","1","1","0","0","c2","0","0","1","0",">",">",">","estimator.state_counts","(","'","C","'",",","parents=","[","'","A","'","]",")","A","a1","a2","C","c1","2.0","0.0","c2","0.0","1.0"]
code: def state_counts(
      self,
      variable,
      parents=[],
      weighted=False,
      reindex=True,
  ):
      """
      Return counts how often each state of 'variable' occurred in the data.
      If a list of parents is provided, counting is done conditionally
      for each state configuration of the parents.
  
      Parameters
      ----------
      variable: string
          Name of the variable for which the state count is to be done.
  
      parents: list
          Optional list of variable parents, if conditional counting is desired.
          Order of parents in list is reflected in the returned DataFrame
  
      weighted: bool
          If True, data must have a `_weight` column specifying the weight of the
          datapoint (row). If False, each datapoint has a weight of `1`.
  
      reindex: bool
          If True, returns a data frame with all possible parents state combinations
          as the columns. If False, drops the state combinations which are not
          present in the data.
  
      Returns
      -------
      state_counts: pandas.DataFrame
          Table with state counts for 'variable'
  
      Examples
      --------
      >>> import pandas as pd
      >>> from pgmpy.estimators import BaseEstimator
      >>> data = pd.DataFrame(data={'A': ['a1', 'a1', 'a2'],
                                    'B': ['b1', 'b2', 'b1'],
                                    'C': ['c1', 'c1', 'c2']})
      >>> estimator = BaseEstimator(data)
      >>> estimator.state_counts('A')
          A
      a1  2
      a2  1
      >>> estimator.state_counts('C', parents=['A', 'B'])
      A  a1      a2
      B  b1  b2  b1  b2
      C
      c1  1   1   0   0
      c2  0   0   1   0
      >>> estimator.state_counts('C', parents=['A'])
      A    a1   a2
      C
      c1  2.0  0.0
      c2  0.0  1.0
      """
      parents = list(parents)
  
      if weighted and ("_weight" not in self.data.columns):
          raise ValueError(
              "data must contain a `_weight` column if weighted=True"
          )
  
      if not parents:
          # count how often each state of 'variable' occurred
          if weighted:
              state_count_data = self.data.groupby([variable])[
                  "_weight"
              ].sum()
          else:
              state_count_data = self.data.loc[
                  :, variable
              ].value_counts()
  
          state_counts = (
              state_count_data.reindex(self.state_names[variable])
              .fillna(0)
              .to_frame()
          )
  
      else:
          parents_states = [
              self.state_names[parent] for parent in parents
          ]
          # count how often each state of 'variable' occurred, conditional on parents' states
          if weighted:
              state_count_data = (
                  self.data.groupby([variable] + parents)["_weight"]
                  .sum()
                  .unstack(parents)
              )
  
          else:
              state_count_data = (
                  self.data.groupby([variable] + parents)
                  .size()
                  .unstack(parents)
              )
  
          if not isinstance(state_count_data.columns, pd.MultiIndex):
              state_count_data.columns = pd.MultiIndex.from_arrays(
                  [state_count_data.columns]
              )
  
          if reindex:
              # reindex rows & columns to sort them and to add missing ones
              # missing row    = some state of 'variable' did not occur in data
              # missing column = some state configuration of current 'variable's parents
              #                  did not occur in data
              row_index = self.state_names[variable]
              column_index = pd.MultiIndex.from_product(
                  parents_states, names=parents
              )
              state_counts = state_count_data.reindex(
                  index=row_index, columns=column_index
              ).fillna(0)
          else:
              state_counts = state_count_data.fillna(0)
  
      return state_counts
  
file_path: pgmpy/pgmpy/estimators/base.py
imports: from collections import OrderedDict
  import pandas
label_desc: Use image node_id 3 for calling the BaseEstimator obj's underlying member method code with example usage: obj.state_counts(variable, parents, weighted, reindex) and returns: state_counts