bnlearn.inference
Inference is same as asking conditional probability questions to the models.
# ———————————— # Name : inference.py # Author : E.Taskesen # Contact : erdogant@gmail.com # Licence : See licences # ————————————
- bnlearn.inference.fit(model, variables=None, evidence=None, to_df=True, elimination_order='greedy', joint=True, groupby=None, plot=False, verbose=3)
Inference using using Variable Elimination.
The basic concept of variable elimination is same as doing marginalization over Joint Distribution. But variable elimination avoids computing the Joint Distribution by doing marginalization over much smaller factors. So basically if we want to eliminate X from our distribution, then we compute the product of all the factors involving X and marginalize over them, thus allowing us to work on much smaller factors.
- 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 impact the processing speed.
elimination_order (str or list (default='greedy')) – Order in which to eliminate the variables in the algorithm. If list is provided, should contain all variables in the model except the ones in variables. str options are: greedy, WeightedMinFill, MinNeighbors, MinWeight, MinFill. Please refer https://pgmpy.org/exact_infer/ve.html#module-pgmpy.inference.EliminationOrder for details.
joint (boolean (default: True)) – If True, returns a Joint Distribution over variables. If False, returns a dict of distributions over each of the variables.
groupby (list of strings (default: None)) – The query is grouped on the variable name by taking the maximum P value for each catagory.
plot (bool, optional) – If True, display a bar plot.
verbose (int, optional) – Print progress to screen. The default is 3. 0: None, 1: ERROR, 2: WARN, 3: INFO (default), 4: DEBUG, 5: TRACE
- 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 >>>
- bnlearn.inference.summarize_inference(variables, evidence, query, plot=False, verbose=3)
Summarize inference results based on a Bayesian Network inference output.
- Parameters:
variables (list of str) – Variables being queried (e.g., [‘Machine failure’] or multiple).
evidence (dict) – Evidence variables and their fixed values (e.g., {‘Torque [Nm]_category’: ‘high’}).
query (Object from inference.fit()) – Inference output containing the queried variables and probability ‘p’ in a Dataframe (query.df)
plot (bool, optional) – If True, display a bar plot.
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:
A textual summary.
- Return type:
str