erdogant / bnlearn

Python library for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods.
https://erdogant.github.io/bnlearn
Other
463 stars 45 forks source link

Error when calling the .predict method #87

Closed zzzrbx closed 9 months ago

zzzrbx commented 10 months ago

I'm getting an error when using the .predict method with this code:

import bnlearn as bn
import pandas as pd
import numpy as np

tmp = pd.DataFrame(np.random.random_integers(0, 2, (10, 4)), columns=['a', 'b', 'c', 'd'])
edges = [
    ('a', 'b'), 
    ('a', 'c'),
    ('c', 'd'),
    ('b', 'd'),
]
DAG = bn.make_DAG(edges)
DAG = bn.parameter_learning.fit(DAG, tmp, methodtype='maximumlikelihood')

bn.predict(DAG, tmp, variables=['d'])
/bnlearn.py:1524, in _get_prob(query, method)
   1522 if method=='max':
   1523     idx = np.argmax(Pq)
-> 1524     comb = allcomb[idx]
   1525     p = Pq[idx]
   1526     # Store in dict

IndexError: index 8 is out of bounds for axis 0 with size 4
ankh1999 commented 10 months ago

After debugging, I noticed that the issue seems to originate from the _get_prob() function. The line allcomb = np.array(list(itertools.product([0, 1], repeat=len(query.variables)))) generates a list consisting only of 0s and 1s. For instance, in your code, if you assign a value from 0 to 1 for the "random" variable, there is no issue. However, if you change it to a range from 0 to 100, the problem consistently occurs. I have recently encountered a similar issue and I am attempting to make modifications. If I succeed, I will either post it here or attempt a pull request (PR).You can also give it a try and attempt to make modifications.

ankh1999 commented 10 months ago

Alright, it looks like a small modification can solve this issue. I haven't encountered any problems on my end, so hopefully, this change will work in all scenarios. The modification is straightforward. You just need to replace the problematic line with the following:

possible_values = query.state_names.values()
allcomb = np.array(list(itertools.product(*possible_values)))

Since this change is minor and I haven't rigorously checked the variable order, I'll just post it here and hope this helps.

erdogant commented 10 months ago

Great contribution @ankh1999 Do you want to create a pull request? You will be tagged as a contributor and likely get a batch on your github page ;)

ankh1999 commented 10 months ago

Thanks for your recognition. In that case, I will check the correctness of the variable order and create a pull request accordingly in the coming days. Lastly, I'm grateful for your work in developing the library!

erdogant commented 9 months ago

Thank you for your contribution @ankh1999 ! I released a new version! install the latest version with pip install -U bnlearn https://github.com/erdogant/bnlearn/releases/tag/0.8.3