bnlearn.inference

Inferences.

Description

Inference is same as asking conditional probability questions to the models.

bnlearn.inference.fit(model, variables=None, evidence=None, to_df=True, verbose=3)

Inference using using Variable Elimination.

Parameters
  • model (dict) – Contains model.

  • variables (List, optional) –

    For exact inference, P(variables | evidence). The default is None.
    • [‘Name_of_node_1’]

    • [‘Name_of_node_1’, ‘Name_of_node_2’]

  • evidence (dict, optional) –

    For exact inference, P(variables | evidence). The default is None.
    • {‘Rain’:1}

    • {‘Rain’:1, ‘Sprinkler’:0, ‘Cloudy’:1}

  • to_df (Bool, (default is True)) – The output is converted in the dataframe [query.df]. Enabling this function may heavily impact the processing speed.

  • verbose (int, optional) – Print progress to screen. The default is 3. 0: None, 1: ERROR, 2: WARN, 3: INFO (default), 4: DEBUG, 5: TRACE

Returns

Return type

query inference object.

Examples

>>> import bnlearn as bn
>>>
>>> # Load example data
>>> model = bn.import_DAG('sprinkler')
>>> bn.plot(model)
>>>
>>> # Do the inference
>>> query = bn.inference.fit(model, variables=['Wet_Grass'], evidence={'Rain':1, 'Sprinkler':0, 'Cloudy':1})
>>> print(query)
>>> query.df
>>>
>>> query = bn.inference.fit(model, variables=['Wet_Grass','Rain'], evidence={'Sprinkler':1})
>>> print(query)
>>> query.df
>>>