bayesian-networkspgmpy

Saving pgmpy CPDs to PDF or excel file


I am using Expectation Maximization to do parameter learning with Bayesian networks in pgmpy. In my code, I successfully 'train' the Bayesian network to learn the CPDs from labeled data and I am able to perform inference using new observable data.

I have symptom variables and fault variables; given symptom variables, I am trying to compute the marginal posterior probabilities of the fault variables. All variables have discrete states/cardinalities, including the labeled training data I use to learn the CPDs.

I am trying to print the CPDs to the terminal, but I see that they are truncated because of size. In this question "How to print the printing full cpd from pgmpy?" there is a solution to print the full CPDs to the terminal, but when I try this, because of how large the CPD tables are, the output is a bunch of lines and the tabular CPD form is lost.

How can I save the CPDs to an excel spreadsheet, or print them as a pdf?


Solution

  • I just added an option to export CPDs to csv files. If you install the latest dev branch (release scheduled on 1st Oct 2023): https://pgmpy.org/started/install.html, you should be able to use the TabularCPD.to_csv method. Example:

    from pgmpy.utils import get_example_model
    model = get_example_model('alarm')
    cpd = model.get_cpds('SAO2')
    cpd.to_csv(filename='sao2_cpd.csv')