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
476 stars 46 forks source link

How to fix the order of variables in an inference result ? #40

Closed SachiniEMSPE closed 2 years ago

SachiniEMSPE commented 2 years ago

Hi @erdogant , 👋

I tried to make the same inference using a list of DAGs (with a total of 5 variables) as below. prob = bn.inference.fit(G_list[k],variables=['X3','X1','X2'], evidence={},verbose=3) prob_df = prob.values. I'm interested in finding variable values for a certain phi () value using the prob_dfindex. However, I noticed that every time I'm getting a different order of variables as attached below. I was wondering whether there is any way to fix that output variable order? I even tried sending a fixed variable elimination order. But, it didn't work. I would appreciate any suggestions on this? 🤔 Thank you! 🙏

+-------+-------+-------+-----------------+
| X2    | X3    | X1    |   phi(X2,X3,X1) |
+=======+=======+=======+=================+
| X2(0) | X3(0) | X1(0) |          0.0224 |
+-------+-------+-------+-----------------+
| X2(0) | X3(0) | X1(1) |          0.6438 |
+-------+-------+-------+-----------------+

+-------+-------+-------+-----------------+
| X2    | X1    | X3    |   phi(X2,X1,X3) |
+=======+=======+=======+=================+
| X2(0) | X1(0) | X3(0) |          0.0554 |
+-------+-------+-------+-----------------+
| X2(0) | X1(0) | X3(1) |          0.0595 |
+-------+-------+-------+-----------------+`
erdogant commented 2 years ago

Good point. I changed it and released a new version!

Update with: pip install -U bnlearn

Try this instead:

prob = bn.inference.fit(G_list[k],variables=['X3','X1','X2'], evidence={},verbose=3)
prob.df
SachiniEMSPE commented 2 years ago

Thank you for updating! However, after updating bnlearn to 0.4.6, I'm getting an error (ModuleNotFoundError: No module named 'tabulate') as attached. Seems like I cannot run bnlearn. Do you have any suggestions on that? Thanks! image

erdogant commented 2 years ago

I added the tabulate package to the setup file now. Try to pip install again.

pip install -U bnlearn

SachiniEMSPE commented 2 years ago

I see. Now it works! 😃 Thank you!

erdogant commented 2 years ago

have fun! :)