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?
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 2 Answers
- Open file "pgmpy/factors/discrete/CPD.py"
In class "TabularCPD(DiscreteFactor)",
fine the line "cdf_str = self._truncate_strtable(cdf_str)"
(This line is in "def _make_table_str (~)")
cdf_str = self._truncate_strtable(cdf_str)
make this line deactivate by add "#" --> # cdf_str = self._truncate_strtable(cdf_str)
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 = backupThe 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:
- For Windows: Set Console Width (Windows, Python)
- For Linux: Resize the terminal with Python?