{"id":16554663,"url":"https://github.com/hayesall/bn-rule-extraction","last_synced_at":"2025-08-18T10:10:02.265Z","repository":{"id":45167539,"uuid":"317892653","full_name":"hayesall/bn-rule-extraction","owner":"hayesall","description":"Rule Extraction from Bayesian Networks","archived":false,"fork":false,"pushed_at":"2022-01-04T03:44:47.000Z","size":173,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-11T10:19:35.360Z","etag":null,"topics":["bayesian-network","interpretability","machine-learning","machine-learning-interpretability","rule-extraction","trustworthy-ai"],"latest_commit_sha":null,"homepage":"https://hayesall.com/blog/bayes-net-rule-extraction/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hayesall.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-02T14:45:01.000Z","updated_at":"2025-02-22T12:15:59.000Z","dependencies_parsed_at":"2022-09-06T00:21:48.338Z","dependency_job_id":null,"html_url":"https://github.com/hayesall/bn-rule-extraction","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hayesall/bn-rule-extraction","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayesall%2Fbn-rule-extraction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayesall%2Fbn-rule-extraction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayesall%2Fbn-rule-extraction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayesall%2Fbn-rule-extraction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hayesall","download_url":"https://codeload.github.com/hayesall/bn-rule-extraction/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayesall%2Fbn-rule-extraction/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270976286,"owners_count":24678466,"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","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-network","interpretability","machine-learning","machine-learning-interpretability","rule-extraction","trustworthy-ai"],"created_at":"2024-10-11T19:52:04.974Z","updated_at":"2025-08-18T10:10:02.239Z","avatar_url":"https://github.com/hayesall.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Extracting Interpretable Rules from Bayesian Networks\n\nBased on the 2010 paper: \"*Bayesian rule learning for biomedical data mining*\"\nby Vanathi Gopalakrishnan, Jonathan L. Lustgarten, Shyam Visweswaran, and\nGregory F. Cooper.\n\n## Notebook Demos\n\nSome demos are implemented as Jupyter notebooks:\n\n| Notebook | Colab Link | View on GitHub |\n| :---- | :---- | ----: |\n| Mitchell Tennis Dataset | [![](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/hayesall/bn-rule-extraction/blob/main/docs/notebooks/tennis.ipynb) | [`tennis.ipynb`](https://github.com/hayesall/bn-rule-extraction/blob/main/docs/notebooks/tennis.ipynb) |\n| Adult Dataset | [![](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/hayesall/bn-rule-extraction/blob/main/docs/notebooks/adult.ipynb) | [`adult.ipynb`](https://github.com/hayesall/bn-rule-extraction/blob/main/docs/notebooks/adult.ipynb) |\n\n## Overview\n\nGiven a Bayesian Network structure and parameters in the form of conditional\nprobability tables, Gopalakrishnan et al. proposed to extract if/then rules\nbased on the edges and the ratio between outcome cases\n(from Fig. 3: \"CF is expressed as the\nlikelihood ratio of the conditional probability of the target value given\nthe value of its parent variables.\")\n\nFor example, this structure (CPTs not shown):\n\n![Structure of a Bayesian Network learned on the toy play tennis data set. This shows that whether a person plays tennis is dependent on the weather outlook and the wind; and suggests that temperature affects humidity, but both are independent of whether someone plays tennis.](docs/play_tennis_bn_1.png)\n\nCan be turned into the following rules:\n\n```text\nProbabilities:\n- Outlook\n  P( Outlook = sunny ) = 0.36\n  P( Outlook = overcast ) = 0.29\n  P( Outlook = rain ) = 0.36\n- Temperature\n  P( Temperature = hot ) = 0.29\n  P( Temperature = mild ) = 0.43\n  P( Temperature = cool ) = 0.29\n- Wind\n  P( Wind = weak ) = 0.57\n  P( Wind = strong ) = 0.43\n\nIF (Outlook = overcast ^ Wind = strong) THEN (PlayTennis = yes)\n\tCF = inf\nIF (Outlook = overcast ^ Wind = weak) THEN (PlayTennis = yes)\n\tCF = inf\nIF (Outlook = rain ^ Wind = strong) THEN (PlayTennis = no)\n\tCF = inf\nIF (Outlook = rain ^ Wind = weak) THEN (PlayTennis = yes)\n\tCF = inf\nIF (Outlook = sunny ^ Wind = strong) THEN (PlayTennis = no)\n\tCF = 1.00\nIF (Outlook = sunny ^ Wind = strong) THEN (PlayTennis = yes)\n\tCF = 1.00\nIF (Outlook = sunny ^ Wind = weak) THEN (PlayTennis = no)\n\tCF = 2.00\nIF (Temperature = cool) THEN (Humidity = normal)\n\tCF = inf\nIF (Temperature = hot) THEN (Humidity = high)\n\tCF = 3.00\nIF (Temperature = mild) THEN (Humidity = high)\n\tCF = 2.00\n```\n\n## Getting Started\n\n### Working with the Python package\n\nInstall requirements:\n\n```console\npip install git+https://github.com/hayesall/bn-rule-extraction.git\n```\n\nThe `bayes_rule_extraction` package exposes two functions: `print_rules` and `ordinal_encode`.\n\nHere's a minimal working example:\n\n```python\nfrom bayes_rule_extraction import ordinal_encode, print_rules\nfrom pomegranate import BayesianNetwork\nimport pandas as pd\n\ndf = pd.read_csv(\"https://raw.githubusercontent.com/hayesall/bn-rule-extraction/main/toy_decision.csv\")\n\nencoded, mapping = ordinal_encode(df.columns, df)\n\n# Encode a constraint that \"PlayTennis\" cannot be the parent of any other node.\nexcluded_edges = [tuple([0, i]) for i in range(1, len(df.columns))]\n\nmodel = BayesianNetwork().from_samples(\n    encoded,\n    exclude_edges=excluded_edges,\n    state_names=df.columns,\n)\n\nprint_rules(model, df.columns, mapping)\n```\n\n## Notes\n\n- This is implemented as an \"*explanation method*\" to help explain a Bayesian Network.\n  It's not currently possible to use the extracted rules directly for classification.\n- *Gopalakrishnan 2010* used a modified version of K2 for structure learning.\n- The `include_edges` parameter in the `pomegranate.BayesianNetwork.from_samples`\n  method seems to be required to learn \"*interesting*\" or \"*useful*\" rules,\n  especially if there is a specific outcome variable (like `PlayTennis`)\n  you are interested in. This might be explained by differences in structure\n  learning methods\u0026mdash;variable ordering in K2 provides some control\n  over influence between possible parents and children.\n\n## Acknowledgements\n\nThe Toy Decision data set is lifted from Tom Mitchell's *Machine Learning* book,\nsee section 3.4.2 (page 59 in my edition).\n\n### BibTex\n\n```bibtex\n@article{gopalakrishnan2010bayesian,\n  author = {Gopalakrishnan, Vanathi and Lustgarten, Jonathan L. and Visweswaran, Shyam and Cooper, Gregory F.},\n  title = \"{Bayesian rule learning for biomedical data mining}\",\n  journal = {Bioinformatics},\n  volume = {26},\n  number = {5},\n  pages = {668-675},\n  year = {2010},\n  month = {01},\n  issn = {1367-4803},\n  doi = {10.1093/bioinformatics/btq005},\n  url = {https://doi.org/10.1093/bioinformatics/btq005},\n  eprint = {https://academic.oup.com/bioinformatics/article-pdf/26/5/668/16897540/btq005.pdf},\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhayesall%2Fbn-rule-extraction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhayesall%2Fbn-rule-extraction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhayesall%2Fbn-rule-extraction/lists"}