{"id":19795883,"url":"https://github.com/yimeng-zhang/rule_extraction_from_trees","last_synced_at":"2025-10-11T11:44:51.875Z","repository":{"id":137726974,"uuid":"152976817","full_name":"Yimeng-Zhang/Rule_Extraction_from_Trees","owner":"Yimeng-Zhang","description":"A toolkit for extracting comprehensible rules from tree-based algorithms","archived":false,"fork":false,"pushed_at":"2018-12-15T03:17:58.000Z","size":829,"stargazers_count":42,"open_issues_count":3,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-01T03:36:06.844Z","etag":null,"topics":["data-mining","decision-tree","machine-learning","rule-extraction","rule-mining"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Yimeng-Zhang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-10-14T13:29:37.000Z","updated_at":"2024-12-15T14:08:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"55bb9bee-442f-4cb6-b492-5252b274d71f","html_url":"https://github.com/Yimeng-Zhang/Rule_Extraction_from_Trees","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Yimeng-Zhang/Rule_Extraction_from_Trees","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yimeng-Zhang%2FRule_Extraction_from_Trees","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yimeng-Zhang%2FRule_Extraction_from_Trees/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yimeng-Zhang%2FRule_Extraction_from_Trees/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yimeng-Zhang%2FRule_Extraction_from_Trees/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Yimeng-Zhang","download_url":"https://codeload.github.com/Yimeng-Zhang/Rule_Extraction_from_Trees/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yimeng-Zhang%2FRule_Extraction_from_Trees/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279007062,"owners_count":26084230,"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-10-11T02:00:06.511Z","response_time":55,"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":["data-mining","decision-tree","machine-learning","rule-extraction","rule-mining"],"created_at":"2024-11-12T07:17:40.879Z","updated_at":"2025-10-11T11:44:51.869Z","avatar_url":"https://github.com/Yimeng-Zhang.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rule_Extraction_From_Trees\n\nA toolkit for extracting comprehensible rules and  selecting the best performing rule set from tree-based algorithms, based on  [Skope-rules](https://github.com/scikit-learn-contrib/skope-rules). Currently only supports 2-classes classification task.\n\n**Major groups of functionalities:**\n\n1. Visualize tree structures and output as images;\n2. Rule extraction from trained tree models; \n3. Filter rules based on recall/precision threshold on a given dataset;\n4. Make predictions by rule voting.\n\n**Model supported:**\n\n1. DecisionTreeClassifier/DecisionTreeRegressor\n2. BaggingClassifier/BaggingRegressor\n3. RandomForestClassifier/RandomForestRegressor\n4. ExtraTreesClassifier/ ExtraTreeRegressor\n\n\n\n## Installation\n\nThis project requires:\n\n- Python (\u003e= 2.7 or \u003e= 3.3)\n- NumPy (\u003e= 1.10.4)\n- SciPy (\u003e= 0.17.0)\n- Pandas (\u003e= 0.18.1)\n- Scikit-Learn (\u003e= 0.17.1)\n- pydotplus (\u003e=2.0.2)\n- graphviz (\u003e=0.8.2)\n\n\n\n**Installing graphviz (for windows user):**\n\n1. Download and install executable from https://graphviz.gitlab.io/_pages/Download/Download_windows.html\n\n2. Set the PATH variable as follows\n\n   ![install_graphviz](images/install_graphviz.png)\n\n3. Restart your currently running application that requires the path\n\n4. pip install pydotplus\n\n\n\n## Quick Start\n\nSee **Demo1** [here](https://github.com/Yimeng-Zhang/Rule_Extraction_from_Trees/blob/master/Demo1_Rule_Extraction_from_Trees.ipynb) for a detailed example.\n\nFirst download the code into your project folder.\n\n1. Train or load a tree-based model. Having the dataset that is trained on is better.\n\n```\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import tree,ensemble,metrics\n\nfrom rule import Rule\nfrom rule_extraction import rule_extract,draw_tree\n\n# Train the model\nmodel = tree.DecisionTreeClassifier(criterion='gini',max_depth=3)\nmodel.fit(X_train,y_train)\n```\n\n2. Extract all the rules from the tree (all paths from root node to leaves)\n\n```python\nrules, _ = rule_extract(model=model,feature_names=X_train.columns)\nfor i in rules:\n    print(i)\n\n# output \nSex_ordered \u003e 0.4722778648138046 and Pclass_ordered \u003e 0.3504907488822937 and Fare \u003e 26.125\nSex_ordered \u003c= 0.4722778648138046 and Age \u003e 13.0 and Pclass_ordered \u003c= 0.5564569681882858\nSex_ordered \u003c= 0.4722778648138046 and Age \u003c= 13.0 and Pclass_ordered \u003c= 0.3504907488822937\nSex_ordered \u003e 0.4722778648138046 and Pclass_ordered \u003c= 0.3504907488822937 and Fare \u003c= 20.800000190734863\nSex_ordered \u003c= 0.4722778648138046 and Age \u003e 13.0 and Pclass_ordered \u003e 0.5564569681882858\nSex_ordered \u003c= 0.4722778648138046 and Age \u003c= 13.0 and Pclass_ordered \u003e 0.3504907488822937\nSex_ordered \u003e 0.4722778648138046 and Pclass_ordered \u003e 0.3504907488822937 and Fare \u003c= 26.125\nSex_ordered \u003e 0.4722778648138046 and Pclass_ordered \u003c= 0.3504907488822937 and Fare \u003e 20.800000190734863\n\n```\n\n3. Draw the structure of the tree\n\n```python\n# blue (class=1) denote the node make prediction of class 1\n# orange (class=0) denote the node make prediction of class 0\n# the darker the color, the more purity the node has \ndraw_tree(model=model,\n          outdir='./images/DecisionTree/',\n          feature_names=X_train.columns,\n          proportion=False, # show [proportion] or [number of samples] from a node\n          class_names=['0','1'])\n```\n\n\n\n![](./images/DecisionTree/DecisionTree.jpeg)\n\n\n\n4. Filter rules base on recall/precision on dataset\n\n```python\nrules, rule_dict = rule_extract(model=model_tree_clf,\n           \t\t   feature_names=X_train.columns,\n                   x_test=X_test,\n           \t  \t   y_test=y_test,\n                   recall_min_c0=0.9,  # recall threshold on class 1\n                   precision_min_c0=0.6)  # precision threshold on class 1\n\nfor i in rule_dict:\n    print(i)\n# return:(rule, recall on 1-class, prec on 1-class, recall on 0-class, prec on 0-class, nb) \n('Fare \u003e 26.125 and Pclass_ordered \u003e 0.3504907488822937 and Sex_ordered \u003e 0.4722778648138046', (0.328125, 0.9130434782608695, 0.9746835443037974, 0.6416666666666667, 1))\n('Fare \u003c= 26.125 and Pclass_ordered \u003e 0.3504907488822937 and Sex_ordered \u003e 0.4722778648138046', (0.21875, 0.875, 0.9746835443037974, 0.6062992125984252, 1))\n\n```\n\n\n\n## API Reference\n\nTODO\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyimeng-zhang%2Frule_extraction_from_trees","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyimeng-zhang%2Frule_extraction_from_trees","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyimeng-zhang%2Frule_extraction_from_trees/lists"}