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

Only binary target variables allowed for predictions? #75

Closed schmidj closed 9 months ago

schmidj commented 1 year ago

Hi,

I was wondering why I get an indexerror when I want to predict a categorical (non-binary) variable. I created a post about the error message here. I eventually found one possible source for this error: In the function _get_prob() line 1549, instead of creating allcomb = np.array(list(itertools.product([0, 1], repeat=len(query.variables)))) allcomb shoud rather be

if (query.df == None): n = len(query.values) allcomb = np.linspace([0],[n-1],n) # array with number of values of target variable else: allcomb = query.df.values[:,0:(len(query.df.values)-2)] In this case, predictions are not limited to binary variables anymore. Am I right?

erdogant commented 1 year ago

Can you demonstrate this issue with an example? This will help me debug the issue.

erdogant commented 1 year ago

I created a small example and does not result in an issue:

# Load example dataset
Xy_train = bn.import_example('titanic')
Xy_train.drop(labels='Cabin', axis=1, inplace=True)
Xy_train = Xy_train.dropna(axis=0)

tarvar='Survived'
model = bn.structure_learning.fit(Xy_train, 
                                  methodtype='tan', 
                                  class_node = 'Survived')
model = bn.parameter_learning.fit(model, 
                                  Xy_train, 
                                  methodtype='bayes',
                                  scoretype='bdeu')
y_train_pred = bn.predict(model, Xy_train, variables = tarvar, verbose=4)
erdogant commented 9 months ago

please reopen this issue when the issue is still there.