ParameterEstimator#

class pgmpy.estimators.ParameterEstimator(model, data, **kwargs)[source]#

Bases: BaseEstimator

Base class for parameter estimators in pgmpy.

Parameters:
model: pgmpy.models.DiscreteBayesianNetwork or pgmpy.models.DiscreteMarkovNetwork model

for which parameter estimation is to be done.

data: pandas DataFrame object

dataframe object with column names identical to the variable names of the model. (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.

state_counts(variable, weighted=False, **kwargs)[source]#

Return counts how often each state of ‘variable’ occurred in the data. If the variable has parents, 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.

Returns:
state_counts: pandas.DataFrame

Table with state counts for ‘variable’

Examples

>>> import pandas as pd
>>> from pgmpy.models import DiscreteBayesianNetwork
>>> from pgmpy.estimators import ParameterEstimator
>>> model = DiscreteBayesianNetwork([("A", "C"), ("B", "C")])
>>> data = pd.DataFrame(
...     data={
...         "A": ["a1", "a1", "a2"],
...         "B": ["b1", "b2", "b1"],
...         "C": ["c1", "c1", "c2"],
...     }
... )
>>> estimator = ParameterEstimator(model, data)
>>> estimator.state_counts(variable="A").values
array([[2],
       [1]])
>>> estimator.state_counts(variable="C").values
array([[1., 1., 0., 0.],
       [0., 0., 1., 0.]])