erdogant / bnlearn

Python package for Causal Discovery by learning the graphical structure of Bayesian networks. Structure Learning, Parameter Learning, Inferences, Sampling methods.
https://erdogant.github.io/bnlearn
Other
469 stars 45 forks source link

Example not working #84

Closed suciokhan closed 1 year ago

suciokhan commented 1 year ago

In the Naive Bayes example shown on this page: https://erdogant.github.io/bnlearn/pages/html/Structure%20learning.html

I receive an error when executing the following line of code, which was copied + pasted from the example:

DAG = bn.make_DAG(DAG, CPD=[cpd_A, cpd_B, cpd_C, cpd_D], checkmodel=True)

I created the cpd objects via copy/pasting as well; as follows:

cpd_A = TabularCPD(variable='A', variable_card=3, values=[[0.3], [0.5], [0.2]])
cpd_B = TabularCPD(variable='B', variable_card=2, values=[[0.4, 0.9], [0.6, 0.1]], evidence=['A'], evidence_card=[2])
cpd_C = TabularCPD(variable='C', variable_card=2, values=[[0.4, 0.9], [0.6, 0.1]], evidence=['A'], evidence_card=[2])
cpd_D = TabularCPD(variable='D', variable_card=2, values=[[0.4, 0.9], [0.6, 0.1]], evidence=['A'], evidence_card=[2])

The error generated is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[87], line 2
      1 # Make the DAG
----> 2 DAG = bn.make_DAG(DAG, CPD=[cpd_A, cpd_B, cpd_C, cpd_D], checkmodel=True)

File [~/anaconda3/envs/bnlearn/lib/python3.10/site-packages/bnlearn/bnlearn.py:158](https://file+.vscode-resource.vscode-cdn.net/home/suciokhan/rca/~/anaconda3/envs/bnlearn/lib/python3.10/site-packages/bnlearn/bnlearn.py:158), in make_DAG(DAG, CPD, methodtype, checkmodel, verbose)
    156     # Check model
    157     if checkmodel:
--> 158         check_model(DAG, verbose=verbose)
    160 # Create adjacency matrix from DAG
    161 out = {}

File [~/anaconda3/envs/bnlearn/lib/python3.10/site-packages/bnlearn/bnlearn.py:281](https://file+.vscode-resource.vscode-cdn.net/home/suciokhan/rca/~/anaconda3/envs/bnlearn/lib/python3.10/site-packages/bnlearn/bnlearn.py:281), in check_model(DAG, verbose)
    279         if not np.all(cpd.values.astype(Decimal).sum(axis=0)==1):
    280             if verbose>=3: print('[bnlearn] >CPD [%s] does not add up to 1 but is: %s' %(cpd.variable, cpd.values.sum(axis=0)))
--> 281     if verbose>=3: print('[bnlearn] >Check whether CPDs associated with the nodes are consistent: %s' %(DAG.check_model()))
    282 else:
    283     if verbose>=2: print('[bnlearn] >No model found containing CPDs.')

File [~/anaconda3/envs/bnlearn/lib/python3.10/site-packages/pgmpy/models/BayesianNetwork.py:438](https://file+.vscode-resource.vscode-cdn.net/home/suciokhan/rca/~/anaconda3/envs/bnlearn/lib/python3.10/site-packages/pgmpy/models/BayesianNetwork.py:438), in BayesianNetwork.check_model(self)
    436 # Check if the evidence cardinality specified is same as parent's cardinality
    437 if parent_cpd.cardinality[0] != cpd.cardinality[1 + index]:
--> 438     raise ValueError(
    439         f"The cardinality of {node} doesn't match in it's child nodes."
    440     )
    441 # Check if the state_names are the same in parent and child CPDs.
    442 if parent_cpd.state_names[node] != cpd.state_names[node]:

ValueError: The cardinality of A doesn't match in it's child nodes.

Please review the code in the example on the website and fix as needed.

erdogant commented 1 year ago

Thank you for mentioning it! There was a double-check that I now removed. Update should be working again. Update to the latest version with:

pip install -U bnlearn