A BayesianNetwork represents a directed dependency structure for categorical variables, then fits the associated conditional probability tables from observed data. In pgmpy, edges encode relationships such as A→E, E→O, and O→T; the model stores each node’s conditional distribution. section 3.1.8
A practical workflow is:
- Choose variables whose definitions match the inference problem, rather than treating spreadsheet columns as automatically meaningful abstractions. section 3.1.8
- Construct the network with directed edges, call fit(data), and retrieve learned distributions with get_cpds(). section 3.1.8
- Interpret each TabularCPD as a conditional probability table whose outcomes sum to one for every parent-value combination. section 3.1.8
Transportation choice model
A fitted network can represent P(T | O,R), where transportation choice T depends on occupation O and resources R. For each O and R combination, probabilities for car, other, and train sum to one. section 3.1.8
Listing 3.4 Learning parameters for the causal Markov kernels
from pgmpy.models import BayesianNetwork
model = BayesianNetwork(
[
('A', 'E'),
('S', 'E'),
('E', 'O'),
('E', 'R'),
('O', 'T'),
('R', 'T')
]
)
model.fit(data) #1
causal_markov_kernels = model.get_cpds() #2
print(causal_markov_kernels) #2
Inspect the Python construction, fitting, and CPD-retrieval workflow for a categorical causal model. section 3.1.8
