Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when calling the .predict method #87

Closed
zzzrbx opened this issue Oct 27, 2023 · 5 comments
Closed

Error when calling the .predict method #87

zzzrbx opened this issue Oct 27, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@zzzrbx
Copy link

zzzrbx commented Oct 27, 2023

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
Copy link
Contributor

ankh1999 commented Nov 2, 2023

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
Copy link
Contributor

ankh1999 commented Nov 2, 2023

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
Copy link
Owner

erdogant commented Nov 2, 2023

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 ;)

@erdogant erdogant added the bug Something isn't working label Nov 2, 2023
@ankh1999
Copy link
Contributor

ankh1999 commented Nov 3, 2023

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
Copy link
Owner

erdogant commented Nov 11, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants