Registry / ai-ml / pgmpy
library1.1.2pypypi✓ verified 84d ago

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 pgmpy
INSTALL
IMPORT
SIG · PGMPY
P
pgmpy
ai-mlpythonv1.1.2
Install
23.5s avg
Import
6809ms
Disk
484MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 25.4s
py 3.11
✕ build_error
✓ 24.1s
py 3.12
✕ build_error
✓ 22.1s
py 3.13
✕ build_error
✓ 22.45s
py 3.9
1/4 runs
✕ timeout
484MB installed
● package 484MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BayesianNetwork
from pgmpy.models import BayesianNetwork
from pgmpy.models import BayesianModel
The class was renamed from `BayesianModel` to `BayesianNetwork` in v1.0.0.
MarkovNetwork
from pgmpy.models import MarkovNetwork
from pgmpy.models import MarkovModel
The class was renamed from `MarkovModel` to `MarkovNetwork` in v1.0.0.
TabularCPD
from pgmpy.factors.discrete import TabularCPD
Used for defining Conditional Probability Distributions for discrete variables.
VariableElimination
from pgmpy.inference.exact import VariableElimination
from pgmpy.inference import VariableElimination
Exact inference algorithms are now explicitly under `pgmpy.inference.exact`.

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.

from pgmpy.models import BayesianNetwork from pgmpy.factors.discrete import TabularCPD from pgmpy.inference import VariableElimination # 1. Define the network structure # D: Difficulty (Easy, Hard), I: Intelligence (Low, High) # G: Grade (A, B, C), L: Letter (Good, Bad), S: SAT (Low, High) model = BayesianNetwork([('D', 'G'), ('I', 'G'), ('G', 'L'), ('I', 'S')]) # 2. Define Conditional Probability Distributions (CPDs) cpd_d = TabularCPD(variable='D', variable_card=2, values=[[0.6], [0.4]]) cpd_i = TabularCPD(variable='I', variable_card=2, values=[[0.7], [0.3]]) # G depends on I and D. Order of evidence: I, D cpd_g = TabularCPD(variable='G', variable_card=3, values=[[0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3], [0.3, 0.7, 0.02, 0.2]], evidence=['I', 'D'], evidence_card=[2, 2]) cpd_l = TabularCPD(variable='L', variable_card=2, values=[[0.1, 0.4, 0.99], [0.9, 0.6, 0.01]], evidence=['G'], evidence_card=[3]) cpd_s = TabularCPD(variable='S', variable_card=2, values=[[0.95, 0.2], [0.05, 0.8]], evidence=['I'], evidence_card=[2]) # 3. Add CPDs to the model model.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s) # 4. Check if the model is valid (optional but good practice) assert model.check_model(), "Model is not valid!" # 5. Perform inference inference = VariableElimination(model) # Query for P(L | D=0, I=1) result = inference.query(variables=['L'], evidence={'D': 0, 'I': 1}) print("P(L | D=0, I=1):") print(result) # Query for P(G | S=0) result_g_s = inference.query(variables=['G'], evidence={'S': 0}) print("\nP(G | S=0):") print(result_g_s)
Debug
Known issues
breakingMajor breaking changes were introduced in v1.0.0. Specifically, `BayesianModel` was renamed to `BayesianNetwork` and `MarkovModel` to `MarkovNetwork`. Code using the old class names will fail with an `AttributeError`.
fix
Update class imports from `BayesianModel` to `BayesianNetwork` and `MarkovModel` to `MarkovNetwork`.
affects: >=1.0.0
gotchaThe order of evidence variables and their cardinality is critical when defining `TabularCPD`s. An incorrect order or mismatch between `evidence` and `evidence_card` will lead to incorrect probability distributions or errors.
fix
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).
affects: All versions
gotchaInference on large or densely connected graphical models can be computationally expensive and memory intensive, especially for exact inference methods like Variable Elimination. Consider approximate inference for such cases.
fix
For large models, explore approximate inference algorithms (e.g., `pgmpy.inference.sampling`) or simplify the model structure.
affects: All versions
Errors
Common errors & fixes
AttributeError: module 'pgmpy.models' has no attribute 'BayesianModel'
Attempting to import or use the `BayesianModel` class in pgmpy versions 1.0.0 or later.
fix
The `BayesianModel` class was renamed to `BayesianNetwork`. Update your code to `from pgmpy.models import BayesianNetwork`.
ValueError: The sum of the probability values for Variable [variable_name] is not 1.
When defining a `TabularCPD`, the probabilities for each state combination of the parent variables must sum to 1. This error indicates a mathematical inconsistency in your CPD definition.
fix
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.
ImportError: cannot import name 'VariableElimination' from 'pgmpy.inference'
The module path for exact inference algorithms changed. `VariableElimination` is no longer directly under `pgmpy.inference`.
fix
Import `VariableElimination` from its specific submodule: `from pgmpy.inference.exact import VariableElimination`.
Upgrade
Version history
1.1.2latest on PyPI · released Apr 30, 2026
Audit
Dependencies
numpyrequiredNumerical operations and array handling.
scipyrequiredScientific computing, statistical functions.
networkxrequiredGraph representation and manipulation for PGM structures.
pandasrequiredData manipulation, especially for learning algorithms and data input.
matplotliboptionalFor visualizing graphical models and inference results.
Agent activity
4 hits · last 30 days
node
4
Resources
pgmpy — pip install pgmpy · libregistry