4

I'm trying to get the example in Insurance data representation with Bayesian networks to work. See the section Maximum Likelihood Estimation. The data I got from the bnlearn package in R and then uploaded it my google drive. The data

The picture below shows ... I believe this is showing missing columns. How do I fix the code to display the correct information?

enter image description here

from pgmpy.models import BayesianModel

model = BayesianModel([('Antilock', 'Accident'), ('DrivingSkill', 'DrivQuality'), ('DrivQuality', 'Accident')])

# Maximum Likelihood Estimation
from pgmpy.estimators import MaximumLikelihoodEstimator 
mle = MaximumLikelihoodEstimator(model, df)

# Pour toutes les variables :
model.fit(df, estimator=MaximumLikelihoodEstimator)
for cpd in model.get_cpds():
  print(cpd)

Note1: code to get the data from R

data(insurance)

write.csv(insurance,"C:/Users/Administrator/OneDrive/University of London/AI/Assignment 1/insurance.csv")

Note2: if you are running this in colab you will need to install the package

!pip install pgmpy
user20650
  • 24,654
  • 5
  • 56
  • 91
user2946746
  • 1,740
  • 3
  • 21
  • 36

2 Answers2

5
  1. Open file "pgmpy/factors/discrete/CPD.py"

  1. In class "TabularCPD(DiscreteFactor)",

    fine the line "cdf_str = self._truncate_strtable(cdf_str)"

    (This line is in "def _make_table_str (~)")


  1. cdf_str = self._truncate_strtable(cdf_str)

    make this line deactivate by add "#" --> # cdf_str = self._truncate_strtable(cdf_str)

Jung Ah Lee
  • 51
  • 1
  • 4
  • I almost gave up on this before coming across this working fix. Thank you! This needs more upvotes, and pgmpy needs to provide a function call to change this config. – Suprateem Banerjee Jul 20 '22 at 00:36
  • I'd love if they gave the option to get the CPD as a Pandas DataFrame. Printing for inspection would be so much easier! – kynnemall Jul 26 '22 at 22:56
3

A less invasive variation of Jung Ah Lee's answer:

from pgmpy.factors.discrete.CPD import TabularCPD

def print_full(cpd):
    backup = TabularCPD._truncate_strtable
    TabularCPD._truncate_strtable = lambda self, x: x
    print(cpd)
    TabularCPD._truncate_strtable = backup

The print_full(cpd) function reassigns TabularCPD._truncate_strtable to return its input, prints the non-truncated CPD table string, and resets TabularCPD._truncate_strtable to the original function.

Replacing the print(cpd) call with print_full(cpd) in your for loop will print the full CPDs.


I guess it's also possible to mess with the terminal size, since _truncate_strtable makes use of the shutil.get_terminal_size function. Posts that might help with that:

upe
  • 1,862
  • 1
  • 19
  • 33