Package: te2rules
TE2RulesTE2Rules is a technique to explain Tree Ensemble models (TE) like XGBoost, Random Forest, trained on a binary classification task, using a rule list. The extracted rule list (RL) captures the necessary and sufficient conditions for classification by the Tree Ensemble. The algorithm used by TE2Rules is based on Apriori Rule Mining. For more details on the algorithm, please check out ourpaper.TE2Rules provides aModelExplainerwhich takes a trained TE model and training data to extract rules. The training data is used for extracting rules with relevant combination of input features. Without data, an explainer would have to extract rules for all possible combinations of input features, including those combinations which are extremely rare in the data.InstallationTE2Rules package is available on PyPI and can be installed with pip:pipinstallte2rulesDocumentationThe official documentation of TE2Rules can be found atte2rules.readthedocs.io.TE2Rules contains aModelExplainerclass with anexplain()method which returns a rule list corresponding to the positive class prediciton of the tree ensemble. While using the rule list, any data instance that does not trigger any of the extracted rules is to be interpreted as belonging to the negative class. Theexplain()method has tunable parameters to control the interpretability, faithfulness, runtime and coverage of the extracted rules. These are:min_precision:min_precisioncontrols the minimum precision of extracted rules. Setting it to a smaller threshold, allows extracting shorter (more interpretable, but less faithful) rules. By default, the algorithm uses a minimum precision threshold of 0.95.num_stages: The algorithm runs in stages starting from stage 1, stage 2 to all the way till stage n where n is the number of trees in the ensemble. Stopping the algorithm at an early stage  results in a few short rules (with quicker run time, but less coverage in data). It is recommended to try running 2 or 3 stages. By default, the algorithm explores all stages before terminating.jaccard_threshold: This parameter (between 0 and 1) controls how rules from different node combinations are combined. As the algorithm proceeds in stages, combining two similar rules from the previous stage results in yet another similar rule in the next stage. This is not useful in finding new rules that explain the model. Settingjaccard_thresholdto a smaller value, speeds up the algorithm by only combining dissimilar rules from the previous stages. By default, the algorithm uses a jaccard threshold of 0.20.For evaluating the performance of the extracted rule list, theModelExplainerprovides a methodget_fidelity()which returns the fractions of data for which the rule list agrees with the tree ensemble.get_fidelity()returns the fidelity on positives, negatives and overall fidelity.UsageThe following notebook shows a typical use case of TE2Rules on Adult Income Data. The notebook can be foundhere. Let us start with importingte2rulesand other relevant librariesLet us load the training and testing data. All the data used in this notebook are preprocessed already and can be foundhere. The data can also be generated by runningpython3 data_prep/data_prep_adult.py.The tree ensemble model used in this notebook is a XGBoost model with 10 trees.Let us use TE2RulesModelExplainerto explain the positive class prediciton by the XGBoost model. We observe that TE2Rules extracts 5 rules to explain more than 99% of the positive class prediction by the tree ensemble model. In this usgae, we use the default values ofmin_precision(0.95) andnum_stages(10), since the algorithm runs quickly for a tree ensemble with 10 trees.For reproducing results in the paperRun the follwing python scripts to generate the results in thepaper:python3demo/demo/run_te2rules.py
python3demo/demo/run_defrag.py
python3demo/demo/run_intrees.py
python3plot_performance.py
python3plot_scalability.pyLicenseCreative Commons Attribution-NonCommercial 4.0 International Public
License, seeLICENSEfor more details.CitationPlease citeTE2Rulesin your publications if it helps your research:@article{te2rules2022,title={TE2Rules:ExplainingTreeEnsemblesusingRules},author={Lal,GRoshanandChen,XiaotongandMithal,Varun},journal={arXivpreprintarXiv:2206.14359},year={2022}}