pyAgrum-nightly: Probabilistic Graphical Models
pyAgrum-nightly is a Python wrapper for the scientific C++ aGrUM library, providing a high-level interface for Bayesian networks, Markov Networks, Influence Diagrams, and other Probabilistic Graphical Models. As a nightly build, it offers the latest features and bug fixes from the development branch. It is actively maintained with frequent updates reflecting ongoing development.
Common errors
-
IOError: [pyAgrum] I/O Error: Stream states flags are not all unset
cause This error often occurs when attempting to save a Bayesian network or other graphical model to a file, but the specified output directory does not exist.fixEnsure that the directory path provided to save functions (e.g., `gum.saveBN(bn, 'path/to/directory/filename.bif')`) exists before calling the function. Create the directory if it doesn't exist. -
DatabaseError: [pyAgrum] Database error: The conditioning set <COL=0, RA=4, INC=0> for target node PARTNER never appears in the database.
cause This error typically arises during Bayesian network learning or inference when using data, especially with small datasets or specific data splits, where a particular combination of variable values (a 'conditioning set') exists in the query/test set but was never observed in the training data.fixConsider using 'smoothing' or adding 'priors' during the learning phase (e.g., using `BNLearner.useAprioriSmoothing()`) to handle unseen combinations. Also, evaluate the representativeness of your dataset and data splitting strategy.
Warnings
- breaking The main package import name changed from `pyAgrum` to `pyagrum` (all lowercase) starting from version 2.0.0. Older code using `import pyAgrum as gum` will fail.
- gotcha When defining Conditional Probability Tables (CPTs), especially for complex networks, directly assigning values can lead to indexing errors. pyAgrum recommends using dictionaries for clarity and to prevent common mistakes when introducing data.
Install
-
pip install pyAgrum-nightly
Imports
- pyagrum
import pyAgrum as gum
import pyagrum as gum
Quickstart
import pyagrum as gum
# Create a Bayesian Network
bn = gum.BayesNet("WaterSprinkler")
# Add variables
id_c = bn.add(gum.LabelizedVariable("c", "cloudy ?", 2))
id_s = bn.add(gum.LabelizedVariable("s", "sprinkler ?", 2))
id_r = bn.add(gum.LabelizedVariable("r", "rain ?", 2))
id_w = bn.add(gum.LabelizedVariable("w", "wet grass ?", 2))
# Add arcs (dependencies)
bn.addArc(id_c, id_s)
bn.addArc(id_c, id_r)
bn.addArc(id_s, id_w)
bn.addArc(id_r, id_w)
# Define Conditional Probability Tables (CPTs) using dictionaries
bn.cpt("c").fillWith([0.5, 0.5])
bn.cpt("s")[{"c": 0}] = [0.5, 0.5]
bn.cpt("s")[{"c": 1}] = [0.9, 0.1]
bn.cpt("r")[{"c": 0}] = [0.8, 0.2]
bn.cpt("r")[{"c": 1}] = [0.2, 0.8]
bn.cpt("w")[{"s": 0, "r": 0}] = [1, 0]
bn.cpt("w")[{"s": 0, "r": 1}] = [0.1, 0.9]
bn.cpt("w")[{"s": 1, "r": 0}] = [0.1, 0.9]
bn.cpt("w")[{"s": 1, "r": 1}] = [0.01, 0.99]
# Perform inference
ie = gum.LazyPropagation(bn)
ie.setEvidence({"w": 1}) # Wet grass is true
ie.makeInference()
# Get posterior probabilities
posterior_c = ie.posterior("c")
print(f"P(c|w=1): {posterior_c}")