{"id":13490002,"url":"https://github.com/erdogant/bnlearn","last_synced_at":"2025-05-14T17:09:58.158Z","repository":{"id":38360268,"uuid":"231263493","full_name":"erdogant/bnlearn","owner":"erdogant","description":"Python package for Causal Discovery by learning the graphical structure of Bayesian networks. Structure Learning, Parameter Learning, Inferences, Sampling methods.","archived":false,"fork":false,"pushed_at":"2025-05-04T19:55:15.000Z","size":41178,"stargazers_count":510,"open_issues_count":9,"forks_count":49,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-04T20:36:24.896Z","etag":null,"topics":["bayesian-inference","causal","causal-models","causal-networks","causality-analysis","dag","directed-acyclic-graph","machine-learning","parameter-learning","python","sampling-methods","structure-learning"],"latest_commit_sha":null,"homepage":"https://erdogant.github.io/bnlearn","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erdogant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["erdogant"],"buy_me_a_coffee":"erdogant","ko_fi":"erdogant","custom":["https://erdogant.github.io/bnlearn/pages/html/Documentation.html"],"patreon":null,"open_collective":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null}},"created_at":"2020-01-01T21:00:30.000Z","updated_at":"2025-05-04T19:55:18.000Z","dependencies_parsed_at":"2023-02-12T17:00:43.389Z","dependency_job_id":"1b02a9df-7df8-4a28-afcb-dab0bd224f40","html_url":"https://github.com/erdogant/bnlearn","commit_stats":{"total_commits":941,"total_committers":9,"mean_commits":"104.55555555555556","dds":"0.23591923485653565","last_synced_commit":"783bab5a54912e317ec8dd2da90278e5331ce004"},"previous_names":[],"tags_count":84,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erdogant%2Fbnlearn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erdogant%2Fbnlearn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erdogant%2Fbnlearn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erdogant%2Fbnlearn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erdogant","download_url":"https://codeload.github.com/erdogant/bnlearn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254190395,"owners_count":22029632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayesian-inference","causal","causal-models","causal-networks","causality-analysis","dag","directed-acyclic-graph","machine-learning","parameter-learning","python","sampling-methods","structure-learning"],"created_at":"2024-07-31T19:00:39.196Z","updated_at":"2025-05-14T17:09:58.152Z","avatar_url":"https://github.com/erdogant.png","language":"Jupyter Notebook","readme":"[![Python](https://img.shields.io/pypi/pyversions/bnlearn)](https://img.shields.io/pypi/pyversions/bnlearn)\n[![PyPI Version](https://img.shields.io/pypi/v/bnlearn)](https://pypi.org/project/bnlearn/)\n![GitHub Repo stars](https://img.shields.io/github/stars/erdogant/bnlearn)\n[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/erdogant/bnlearn/blob/master/LICENSE)\n[![Forks](https://img.shields.io/github/forks/erdogant/bnlearn.svg)](https://github.com/erdogant/bnlearn/network)\n[![Open Issues](https://img.shields.io/github/issues/erdogant/bnlearn.svg)](https://github.com/erdogant/bnlearn/issues)\n[![Project Status](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)\n[![Downloads](https://pepy.tech/badge/bnlearn/month)](https://pepy.tech/project/bnlearn/)\n[![Downloads](https://pepy.tech/badge/bnlearn)](https://pepy.tech/project/bnlearn)\n[![DOI](https://zenodo.org/badge/231263493.svg)](https://zenodo.org/badge/latestdoi/231263493)\n[![Docs](https://img.shields.io/badge/Sphinx-Docs-Green)](https://erdogant.github.io/bnlearn/)\n[![Medium](https://img.shields.io/badge/Medium-Blog-black)](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#medium-blog)\n![GitHub repo size](https://img.shields.io/github/repo-size/erdogant/bnlearn)\n[![Donate](https://img.shields.io/badge/Support%20this%20project-grey.svg?logo=github%20sponsors)](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#)\n[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#colab-notebook)\n\u003c!---[![BuyMeCoffee](https://img.shields.io/badge/buymea-coffee-yellow.svg)](https://www.buymeacoffee.com/erdogant)--\u003e\n\u003c!---[![Coffee](https://img.shields.io/badge/coffee-black-grey.svg)](https://erdogant.github.io/donate/?currency=USD\u0026amount=5)--\u003e\n\n\n### \n\n\u003cdiv\u003e\n\n\u003ca href=\"https://erdogant.github.io/bnlearn/\"\u003e\u003cimg src=\"https://github.com/erdogant/bnlearn/blob/master/docs/figs/logo.png\" width=\"175\" align=\"left\" /\u003e\u003c/a\u003e\n``bnlearn`` is Python package for causal discovery by learning the graphical structure of Bayesian networks, parameter learning, inference, and sampling methods.\nBecause probabilistic graphical models can be difficult to use, ``Bnlearn`` contains the most-wanted pipelines. Navigate to [API documentations](https://erdogant.github.io/bnlearn/) for more detailed information. **⭐️ Star it if you like it ⭐️**\n\u003c/div\u003e\n\n---\n\n### Key Pipelines\n\n| Feature | Description |\n|--------|-------------|\n| [**Causal Discovery / Structure Learning**](https://erdogant.github.io/bnlearn/pages/html/Structure%20learning.html) | Learn the model structure from data or with expert knowledge. |\n| [**Parameter Learning**](https://erdogant.github.io/bnlearn/pages/html/Parameter%20learning.html) | Estimate model parameters (e.g., conditional probability distributions) from observed data. |\n| [**Causal Inference**](https://pgmpy.org/examples/Causal%20Inference.html) | Compute interventional and counterfactual distributions using do-calculus. |\n| [**Generate Synthetic Data**](https://erdogant.github.io/bnlearn/pages/html/Sampling.html) | Generate synthetic data. |\n| [**Discretize Data**](https://erdogant.github.io/bnlearn/pages/html/Discretizing.html) | Discretize continuous datasets. |\n\n---\n\n### Resources and Links\n- **Example Notebooks:** [Examples](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#google-colab-notebooks)\n- **Blog Posts:** [Medium](https://erdogant.medium.com/)\n- **Documentation:** [Website](https://erdogant.github.io/bnlearn)\n- **Bug Reports and Feature Requests:** [GitHub Issues](https://github.com/erdogant/bnlearn/issues)\n\n---\n\n### The following functions are available after installation:\n\n| Feature | Description |\n|--------|-------------|\n| **Key Pipelines** | |\n|  [Structure learning](https://erdogant.github.io/bnlearn/pages/html/Structure%20learning.html#exhaustivesearch) | ```bn.structure_learning.fit()``` |\n| [Parameter learning](https://erdogant.github.io/bnlearn/pages/html/Parameter%20learning.html#parameter-learning-examples) | ```bn.parameter_learning.fit()``` |\n| [Inference](https://erdogant.github.io/bnlearn/pages/html/Inference.html#examples-inference) | ```bn.inference.fit()``` |\n| [Make predictions](https://erdogant.github.io/bnlearn/pages/html/Predict.html) | ```bn.predict()``` |\n| [Generate Synthetic Data](https://erdogant.github.io/bnlearn/pages/html/Sampling.html) | ```bn.sampling()``` |\n| [Compute Edge Strength](https://erdogant.github.io/bnlearn/pages/html/independence_test.html) | ```bn.independence_test()``` |\n| **Key Functions** | |\n| [Imputations](https://erdogant.github.io/bnlearn/pages/html/impute.html#) | ```bn.knn_imputer()``` |\n| [Discretizing](https://erdogant.github.io/bnlearn/pages/html/Discretizing.html#) | ```bn.discretize()``` |\n| [Check Model Parameters](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.check_model) | ```bn.check_model()``` |\n| [Create DAG](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.make_DAG) | ```bn.make_DAG()``` |\n| [Get Node Properties](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.get_node_properties) | ```bn.get_node_properties()``` |\n| [Get Edge Properties](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.get_edge_properties) | ```bn.get_edge_properties()``` |\n| [Get Parents From Edges](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.get_parents) | ```bn.get_parents()``` |\n| [Generate Default CPT per Node](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.generate_cpt) | ```bn.generate_cpt()``` |\n| [Generate Default CPTs for All Edges](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.build_cpts_from_structure) | ```bn.build_cpts_from_structure()``` |\n| **Make Plots** | |\n| [Plotting](https://erdogant.github.io/bnlearn/pages/html/Plot.html) | ```bn.plot()``` |\n| [Plot Graphviz](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.plot_graphviz) | ```bn.plot_graphviz()``` |\n| [Compare 2 Networks](https://erdogant.github.io/bnlearn/pages/html/Plot.html#comparison-of-two-networks) | ```bn.compare_networks()``` |\n| [Load DAG (bif files)](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.import_DAG) | ```bn.import_DAG()``` |\n| [Load Examples](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.import_example) | ```bn.import_example()``` |\n| **Transformation Functions** | |\n| [Convert DAG to Undirected](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.to_undirected) | ```bn.to_undirected()``` |\n| [Convert to one-hot](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.df2onehot) | ```bn.df2onehot()``` |\n| [Convert Adjacency Matrix to Vector](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.adjmat2vec) | ```bn.adjmat2vec()``` |\n| [Convert Adjacency Matrix to Dictionary](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.adjmat2dict) | ```bn.adjmat2dict()``` |\n| [Convert Vector to Adjacency Matrix](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.vec2adjmat) | ```bn.vec2adjmat()``` |\n| [Convert DAG to Adjacency Matrix](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.dag2adjmat) | ```bn.dag2adjmat()``` |\n| [Convert DataFrame to Onehot](https://erdogant.github.io/bnlearn/pages/html/Example%20Datasets.html) | ```bn.df2onehot()``` |\n| [Convert Query to DataFrame](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.query2df) | ```bn.query2df()``` |\n| [Convert Vector to DataFrame](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.vec2df) | ```bn.vec2df()``` |\n| **Metrics** | |\n| [Compute Topological Ordering](https://erdogant.github.io/bnlearn/pages/html/topological_sort.html) | ```bn.topological_sort()``` |\n| [Compute Structure Scores](https://erdogant.github.io/bnlearn/pages/html/Structure_scores.html) | ```bn.structure_scores()``` |\n| **General** | |\n| [Save Model](https://erdogant.github.io/bnlearn/pages/html/saving%20and%20loading.html) | ```bn.save()``` |\n| [Load Model](https://erdogant.github.io/bnlearn/pages/html/saving%20and%20loading.html) | ```bn.load()``` |\n| [Print CPTs](https://erdogant.github.io/bnlearn/pages/html/bnlearn.bnlearn.html#bnlearn.bnlearn.print_CPD) | ```bn.print_CPD()``` |\n\n---\n### Installation\n\n##### Install bnlearn from PyPI\n```bash\npip install bnlearn\n```\n##### Install bnlearn from github source\n```bash\npip install git+https://github.com/erdogant/bnlearn\n```\n##### Load library\n```python\n# Import library\nimport bnlearn as bn\n```\n---\n\n### Code Examples\n\n```python\n\n    import bnlearn as bn\n    # Example dataframe sprinkler_data.csv can be loaded with: \n    df = bn.import_example()\n    # df = pd.read_csv('sprinkler_data.csv')\n\nCloudy  Sprinkler  Rain  Wet_Grass\n0         0          1     0          1\n1         1          1     1          1\n2         1          0     1          1\n3         0          0     1          1\n4         1          0     1          1\n..      ...        ...   ...        ...\n995       0          0     0          0\n996       1          0     0          0\n997       0          0     1          0\n998       1          1     0          1\n999       1          0     1          1\n\n\n    model = bn.structure_learning.fit(df)\n    # Compute edge strength with the chi-square test statistic\n    model = bn.independence_test(model, df)\n    G = bn.plot(model)\n```\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/erdogant/bnlearn/blob/master/docs/figs/fig_sprinkler_sl.png\" width=\"600\" /\u003e\n\u003c/p\u003e\n\n\n```python\n\n# Example: Structure Learning\n\n    model_hc_bic  = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic')\n    model_hc_k2   = bn.structure_learning.fit(df, methodtype='hc', scoretype='k2')\n    model_hc_bdeu = bn.structure_learning.fit(df, methodtype='hc', scoretype='bdeu')\n    model_ex_bic  = bn.structure_learning.fit(df, methodtype='ex', scoretype='bic')\n    model_ex_k2   = bn.structure_learning.fit(df, methodtype='ex', scoretype='k2')\n    model_ex_bdeu = bn.structure_learning.fit(df, methodtype='ex', scoretype='bdeu')\n    model_cl      = bn.structure_learning.fit(df, methodtype='cl', root_node='Wet_Grass')\n    model_tan     = bn.structure_learning.fit(df, methodtype='tan', root_node='Wet_Grass', class_node='Rain')\n\n# Example: Parameter Learning\n\n    import bnlearn as bn\n    # Import dataframe\n    df = bn.import_example()\n    # As an example we set the CPD at False which returns an \"empty\" DAG\n    model = bn.import_DAG('sprinkler', CPD=False)\n    # Now we learn the parameters of the DAG using the df\n    model_update = bn.parameter_learning.fit(model, df)\n    # Make plot\n    G = bn.plot(model_update)\n\n# Example: Inference\n\n    import bnlearn as bn\n    model = bn.import_DAG('sprinkler')\n    query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1,'Sprinkler':0, 'Wet_Grass':1})\n    print(query)\n    print(query.df)\n    \n    # Lets try another inference\n    query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1})\n    print(query)\n    print(query.df)\n\n```\n\n\u003chr\u003e\n\n### Contributors\nSetting up and maintaining bnlearn has been possible thanks to users and contributors. Thanks to:\n\n\u003cp align=\"left\"\u003e\n  \u003ca href=\"https://github.com/erdogant/bnlearn/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=erdogant/bnlearn\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n### Maintainer\n* Erdogan Taskesen, github: [erdogant](https://github.com/erdogant)\n* Contributions are welcome.\n* Yes! This library is entirely **free** but it runs on coffee! :) Feel free to support with a \u003ca href=\"https://erdogant.github.io/donate/?currency=USD\u0026amount=5\"\u003eCoffee\u003c/a\u003e.\n\n\u003ca href=\"https://www.buymeacoffee.com/erdogant\"\u003e\u003cimg src=\"https://img.buymeacoffee.com/button-api/?text=Buy me a coffee\u0026emoji=\u0026slug=erdogant\u0026button_colour=FFDD00\u0026font_colour=000000\u0026font_family=Cookie\u0026outline_colour=000000\u0026coffee_colour=ffffff\" /\u003e\u003c/a\u003e\n","funding_links":["https://github.com/sponsors/erdogant","https://buymeacoffee.com/erdogant","https://ko-fi.com/erdogant","https://erdogant.github.io/bnlearn/pages/html/Documentation.html","https://www.buymeacoffee.com/erdogant)--","https://www.buymeacoffee.com/erdogant","https://img.buymeacoffee.com/button-api/?text=Buy"],"categories":["Jupyter Notebook","其他_机器视觉"],"sub_categories":["网络服务_其他"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferdogant%2Fbnlearn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferdogant%2Fbnlearn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferdogant%2Fbnlearn/lists"}