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

Question about exporting learned bayes network in one of common exchange formats #97

Closed serhiy-yevtushenko closed 6 months ago

serhiy-yevtushenko commented 7 months ago

I have a following question, the answer to which I was not able to find in the documentation: is it possible after structure and parameter training to export the learned bayes network in one of common bayes network formats, like BIF, hugin or XMLBIF?

erdogant commented 7 months ago

Nop. That's not possible with internal functionalities. This seems like something that can be done with other packages.

serhiy-yevtushenko commented 7 months ago

I managed to get the export to BIF format as follows:

import pandas as pd
from pgmpy.readwrite import BIFWriter
# ...
df =pd.read_csv("input.csv")
# build model (will be 
model = bn.structure_learning.fit(df)
model = bn.parameter_learning.fit(model, df)
writer = BIFWriter(model['model'])
writer.write_bif(filename='model.bif')

Possibly it will be good to add to the documentation for other people, who will be interested in such possibilities

erdogant commented 7 months ago

Thanks! I will add to the documentation pages!

erdogant commented 6 months ago

I updated the pages and added your code snippet into it. Thanks!