pgmpy is a Python library for working with Probabilistic Graphical Models (PGMs). It provides implementations of various models, inference algorithms, and learning algorithms for Bayesian Networks, Markov Networks, and other causal and probabilistic reasoning tasks. The current version is 1.1.0, with minor releases occurring periodically and major versions like 1.0.0 introducing significant breaking changes.
pip install pgmpyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a Bayesian Network, add Conditional Probability Distributions (CPDs) to it, check its validity, and perform exact inference using the Variable Elimination algorithm. The example builds a small network for a student's performance.
Update class imports from `BayesianModel` to `BayesianNetwork` and `MarkovModel` to `MarkovNetwork`.
Carefully align the `values` matrix with the `evidence` variables' order and `evidence_card`. Consult the documentation for the specific indexing order (e.g., last variable changes fastest).
For large models, explore approximate inference algorithms (e.g., `pgmpy.inference.sampling`) or simplify the model structure.
The `BayesianModel` class was renamed to `BayesianNetwork`. Update your code to `from pgmpy.models import BayesianNetwork`.
Review the `values` array in your `TabularCPD` definition. Ensure that each column (representing a parent configuration) sums to 1. Double-check the order of variables and their cardinalities.
Import `VariableElimination` from its specific submodule: `from pgmpy.inference.exact import VariableElimination`.