Skip to main content

bnlearn is a Python package for Causal Discovery by learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods.

Project description

Python PyPI Version GitHub Repo stars License Forks Open Issues Project Status Downloads Downloads DOI Docs Medium GitHub repo size Donate Colab

bnlearn is Python package for causal discovery by learning the graphical structure of Bayesian networks, parameter learning, inference, and sampling methods. Because probabilistic graphical models can be difficult to use, Bnlearn contains the most-wanted pipelines. Navigate to API documentations for more detailed information. ⭐️ Star it if you like it ⭐️


Key Features

Feature Description Medium Gumroad/Podcast
Causal Discovery / Structure Learning Learn the model structure from data or with expert knowledge. --- ---
Parameter Learning Estimate model parameters (e.g., conditional probability distributions) from observed data. --- ---
Causal Inference Compute interventional and counterfactual distributions using do-calculus. --- ---
Generate Synthetic Data Generate synthetic data. --- ---
Discretize Data Discretize continuous datasets. --- ---

Resources and Links


The following functions are available after installation:

Feature Description
Key Pipelines
Structure learning bn.structure_learning.fit()
Parameter learning bn.parameter_learning.fit()
Inference bn.inference.fit()
Make predictions bn.predict()
Generate Synthetic Data bn.sampling()
Compute Edge Strength bn.independence_test()
Key Functions
Imputations bn.knn_imputer()
Discretizing bn.discretize()
Check Model Parameters bn.check_model()
Create DAG bn.make_DAG()
Get Node Properties bn.get_node_properties()
Get Edge Properties bn.get_edge_properties()
Get Parents From Edges bn.get_parents()
Generate Default CPT per Node bn.generate_cpt()
Generate Default CPTs for All Edges bn.build_cpts_from_structure()
Make Plots
Plotting bn.plot()
Plot Graphviz bn.plot_graphviz()
Compare 2 Networks bn.compare_networks()
Load DAG (bif files) bn.import_DAG()
Load Examples bn.import_example()
Transformation Functions
Convert DAG to Undirected bn.to_undirected()
Convert to one-hot bn.df2onehot()
Convert Adjacency Matrix to Vector bn.adjmat2vec()
Convert Adjacency Matrix to Dictionary bn.adjmat2dict()
Convert Vector to Adjacency Matrix bn.vec2adjmat()
Convert DAG to Adjacency Matrix bn.dag2adjmat()
Convert DataFrame to Onehot bn.df2onehot()
Convert Query to DataFrame bn.query2df()
Convert Vector to DataFrame bn.vec2df()
Metrics
Compute Topological Ordering bn.topological_sort()
Compute Structure Scores bn.structure_scores()
General
Save Model bn.save()
Load Model bn.load()
Print CPTs bn.print_CPD()

Installation

Install bnlearn from PyPI
pip install bnlearn
Install bnlearn from github source
pip install git+https://github.com/erdogant/bnlearn
Load library
# Import library
import bnlearn as bn

Code Examples

    import bnlearn as bn
    # Example dataframe sprinkler_data.csv can be loaded with: 
    df = bn.import_example()
    # df = pd.read_csv('sprinkler_data.csv')

Cloudy  Sprinkler  Rain  Wet_Grass
0         0          1     0          1
1         1          1     1          1
2         1          0     1          1
3         0          0     1          1
4         1          0     1          1
..      ...        ...   ...        ...
995       0          0     0          0
996       1          0     0          0
997       0          0     1          0
998       1          1     0          1
999       1          0     1          1


    model = bn.structure_learning.fit(df)
    # Compute edge strength with the chi-square test statistic
    model = bn.independence_test(model, df)
    G = bn.plot(model)

# Example: Structure Learning

    model_hc_bic  = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic')
    model_hc_k2   = bn.structure_learning.fit(df, methodtype='hc', scoretype='k2')
    model_hc_bdeu = bn.structure_learning.fit(df, methodtype='hc', scoretype='bdeu')
    model_ex_bic  = bn.structure_learning.fit(df, methodtype='ex', scoretype='bic')
    model_ex_k2   = bn.structure_learning.fit(df, methodtype='ex', scoretype='k2')
    model_ex_bdeu = bn.structure_learning.fit(df, methodtype='ex', scoretype='bdeu')
    model_cl      = bn.structure_learning.fit(df, methodtype='cl', root_node='Wet_Grass')
    model_tan     = bn.structure_learning.fit(df, methodtype='tan', root_node='Wet_Grass', class_node='Rain')

# Example: Parameter Learning

    import bnlearn as bn
    # Import dataframe
    df = bn.import_example()
    # As an example we set the CPD at False which returns an "empty" DAG
    model = bn.import_DAG('sprinkler', CPD=False)
    # Now we learn the parameters of the DAG using the df
    model_update = bn.parameter_learning.fit(model, df)
    # Make plot
    G = bn.plot(model_update)

# Example: Inference

    import bnlearn as bn
    model = bn.import_DAG('sprinkler')
    query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1,'Sprinkler':0, 'Wet_Grass':1})
    print(query)
    print(query.df)
    
    # Lets try another inference
    query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1})
    print(query)
    print(query.df)

Star history

Star History Chart

Contributors

Thank the contributors!

Maintainer

  • Erdogan Taskesen, github: erdogant
  • Contributions are welcome.
  • Yes! This library is entirely free but it runs on coffee! :) Feel free to support with a Coffee.

Buy me a coffee

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bnlearn-0.12.2.tar.gz (80.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bnlearn-0.12.2-py3-none-any.whl (82.5 kB view details)

Uploaded Python 3

File details

Details for the file bnlearn-0.12.2.tar.gz.

File metadata

  • Download URL: bnlearn-0.12.2.tar.gz
  • Upload date:
  • Size: 80.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.10

File hashes

Hashes for bnlearn-0.12.2.tar.gz
Algorithm Hash digest
SHA256 a59176064124656fc0029f55071bc1d80ad211dde7a6b3a323646c3bd5536c6c
MD5 4a410d0000d50840531f901b51b12903
BLAKE2b-256 810014fcc59a12612775e1834693b06fc5b0e6ebf793750e4cede715050a72df

See more details on using hashes here.

File details

Details for the file bnlearn-0.12.2-py3-none-any.whl.

File metadata

  • Download URL: bnlearn-0.12.2-py3-none-any.whl
  • Upload date:
  • Size: 82.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.10

File hashes

Hashes for bnlearn-0.12.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c0272727fce8cc2e8c5880b949497d277b2fefaa2954b42aef29c92f562a2327
MD5 0eaea819ea3adc1b5b6f0654cbc2489b
BLAKE2b-256 d1d5241c6caa11dc39377440cae2d70ae70657fed795568a570ff0fca074ec38

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page