DPG 0.3.4__tar.gz → 0.3.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dpg-0.3.6/PKG-INFO ADDED
@@ -0,0 +1,190 @@
1
+ Metadata-Version: 2.4
2
+ Name: dpg
3
+ Version: 0.3.6
4
+ Summary: A Python library for extracting Decision Predicate Graphs and global explanations from ensemble models.
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Keywords: decision graph,ensemble learning,explainable ai,explainability,global explanations
8
+ Author: Sylvio Barbon Junior
9
+ Author-email: sylvio.barbonjunior@units.it
10
+ Requires-Python: >=3.10
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Provides-Extra: dev
19
+ Provides-Extra: docs
20
+ Requires-Dist: contourpy (>=1.2)
21
+ Requires-Dist: cycler (>=0.12)
22
+ Requires-Dist: fonttools (>=4.53)
23
+ Requires-Dist: graphviz (>=0.20)
24
+ Requires-Dist: joblib (>=1.4)
25
+ Requires-Dist: kiwisolver (>=1.4)
26
+ Requires-Dist: matplotlib (>=3.9.1)
27
+ Requires-Dist: myst-nb (>=1.1) ; extra == "docs"
28
+ Requires-Dist: myst-parser (>=3.0) ; extra == "docs"
29
+ Requires-Dist: networkx (>=3.3)
30
+ Requires-Dist: numpy (>=2.0.0)
31
+ Requires-Dist: packaging (>=24.1)
32
+ Requires-Dist: pandas (>=2.2)
33
+ Requires-Dist: pillow (>=10.4)
34
+ Requires-Dist: pydata-sphinx-theme (>=0.15) ; extra == "docs"
35
+ Requires-Dist: pyparsing (>=3.1)
36
+ Requires-Dist: python-dateutil (>=2.9)
37
+ Requires-Dist: pytz (>=2024.1)
38
+ Requires-Dist: pyyaml (>=6.0.2)
39
+ Requires-Dist: scikit-learn (>=1.5)
40
+ Requires-Dist: scipy (>=1.14)
41
+ Requires-Dist: six (>=1.16)
42
+ Requires-Dist: sphinx (>=7.3) ; extra == "docs"
43
+ Requires-Dist: sphinx-autoapi (>=3.1) ; extra == "docs"
44
+ Requires-Dist: sphinx-copybutton (>=0.5) ; extra == "docs"
45
+ Requires-Dist: sphinx-design (>=0.6) ; extra == "docs"
46
+ Requires-Dist: threadpoolctl (>=3.5)
47
+ Requires-Dist: tqdm (>=4.67)
48
+ Requires-Dist: tzdata (>=2024.1)
49
+ Project-URL: Homepage, https://github.com/Meta-Group/DPG
50
+ Project-URL: Repository, https://github.com/Meta-Group/DPG
51
+ Description-Content-Type: text/markdown
52
+
53
+ # Decision Predicate Graph (DPG)
54
+
55
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
56
+ [![Install with pip](https://img.shields.io/badge/pip%20install-dpg-3775A9?logo=pypi&logoColor=white)](https://pypi.org/project/dpg/)
57
+ [![Python Versions](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-blue.svg)](pyproject.toml)
58
+ [![Tests](https://github.com/Meta-Group/DPG/actions/workflows/tests.yml/badge.svg)](https://github.com/Meta-Group/DPG/actions/workflows/tests.yml)
59
+ [![Release](https://github.com/Meta-Group/DPG/actions/workflows/release.yml/badge.svg)](https://github.com/Meta-Group/DPG/actions/workflows/release.yml)
60
+ [![Documentation Status](https://img.shields.io/readthedocs/dpg)](https://dpg.readthedocs.io/en/latest/)
61
+ [![Try the DPG sandbox](https://img.shields.io/badge/DPG%20sandbox-Try%20it%20in%20browser-4B5E27?logo=googlechrome&logoColor=white)](https://machinelearning.inginf.units.it/DPG_web/#how)
62
+
63
+ <p align="center">
64
+ <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/DPG.png" alt="DPG logo" width="300">
65
+ </p>
66
+
67
+ DPG is a model-agnostic framework for explaining tree ensembles. It converts
68
+ their decision logic into a graph, making predicates, transitions, predictions,
69
+ and structural diagnostics available in one representation.
70
+
71
+ ## Highlights
72
+
73
+ - Global and local explanations through `DPGExplainer`
74
+ - Graph metrics, including constraints, betweenness centrality, local reaching
75
+ centrality, and communities
76
+ - Local-path inspection, local-on-global plots, and faithfulness diagnostics
77
+ - Support for common scikit-learn tree ensembles
78
+
79
+ ## Install
80
+
81
+ ```bash
82
+ pip install dpg
83
+ ```
84
+
85
+ DPG requires Python 3.10 or later. To render graphs, also install the Graphviz
86
+ system package so that `dot` is on your `PATH`.
87
+
88
+ For development and documentation builds, see [docs/README.md](docs/README.md).
89
+
90
+ ## Quick start
91
+
92
+ ```python
93
+ import numpy as np
94
+ from sklearn.datasets import load_iris
95
+ from sklearn.ensemble import RandomForestClassifier
96
+ from dpg import DPGExplainer
97
+
98
+ X, y = load_iris(return_X_y=True, as_frame=True)
99
+ model = RandomForestClassifier(n_estimators=5, random_state=42).fit(X, y)
100
+
101
+ explainer = DPGExplainer(
102
+ model=model,
103
+ feature_names=X.columns.tolist(),
104
+ target_names=np.unique(y).astype(str).tolist(),
105
+ )
106
+ explanation = explainer.explain_global(X.values, communities=True)
107
+ explainer.plot("iris_dpg", explanation, save_dir="results")
108
+ ```
109
+
110
+ ## How DPG works
111
+
112
+ The concept behind DPG is to convert a generic tree-based ensemble model for classification into a graph, where:
113
+ - Nodes represent predicates, i.e., the feature-value associations present in each node of every tree;
114
+ - Edges denote the frequency with which these predicates are satisfied during the model training phase by the samples of the dataset.
115
+
116
+ <p align="center">
117
+ <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example.png" width="600" />
118
+ </p>
119
+
120
+ ## Metrics
121
+
122
+ The graph-based nature of DPG provides significant enhancements in the direction of a complete mapping of the ensemble structure.
123
+
124
+ | Property | Definition | Utility |
125
+ |--------------|------------|---------|
126
+ | _Constraints_ | The intervals of values for each feature obtained from all predicates connected by a path that culminates in a given class. | Calculate the classification boundary values of each feature associated with each class. |
127
+ | _Betweenness centrality_ | Quantifies the fraction of all the shortest paths between every pair of nodes of the graph passing through the considered node. | Identify potential bottleneck nodes that correspond to crucial decisions. |
128
+ | _Local reaching centrality_ | Quantifies the proportion of other nodes reachable from the local node through its outgoing edges. | Assess the importance of nodes similarly to feature importance, but enrich the information by encompassing the values associated with features across all decisions. |
129
+ | _Community_ | A subset of nodes of the DPG which is characterised by dense interconnections between its elements and sparse connections with the other nodes of the DPG that do not belong to the community. | Understanding the characteristics of nodes to be assigned to a particular community class, identifying predominant predicates, and those that play a marginal role in the classification process. |
130
+
131
+ |Constraints | Betweenness centrality | Local reaching centrality | Community|
132
+ |------------|------------|--------------|--------------------|
133
+ | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_constraints.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_bc.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_lrc.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_community.png) |
134
+ | Constraints(Class 1) = val3 < F1 ≤ val1, F2 ≤ val2 | BC(F2 ≤ val2) = 4/24 | LRC(F1 ≤ val1) = 6 / 7 | Community(Class 1) = F1 ≤ val1, F2 ≤ val2 |
135
+
136
+ ## Local explanations and faithfulness
137
+
138
+ Fit the explainer once, then inspect an individual prediction or compare local
139
+ explanations with the fitted model:
140
+
141
+ ```python
142
+ explainer.fit(X.values)
143
+ local = explainer.explain_local(X.iloc[0].values, sample_id=0)
144
+ details = explainer.evaluate_faithfulness(X.values, return_details=True)
145
+
146
+ print(local.majority_vote)
147
+ print(details["faithfulness_score"])
148
+ ```
149
+
150
+ `evaluate_faithfulness` reports model agreement, trace coverage,
151
+ recombination, evidence margin, and a composite heuristic score. It is an
152
+ explanation-fidelity measure, not a probability of ground-truth correctness.
153
+
154
+ ## Documentation and examples
155
+
156
+ - [Full documentation](https://dpg.readthedocs.io/en/latest/)
157
+ - [Quickstart](https://dpg.readthedocs.io/en/latest/quickstart.html)
158
+ - [Supported models](https://dpg.readthedocs.io/en/latest/supported_models.html)
159
+ - [Visualizations](https://dpg.readthedocs.io/en/latest/visualization.html)
160
+ - [Examples](examples), including [local explanations](examples/local_explanation_iris.py)
161
+ - [Parameter-sensitivity benchmark](tutorials/parameter_sensitivity_benchmark/README.md)
162
+ - [Interactive DPG sandbox](https://machinelearning.inginf.units.it/DPG_web/#how) — explore how DPG turns an ensemble into a graph in the browser.
163
+
164
+ <p align="center">
165
+ <a href="https://machinelearning.inginf.units.it/DPG_web/#how">
166
+ <img src="docs/_static/DPG.png" alt="Open the interactive DPG sandbox" width="180">
167
+ </a>
168
+ </p>
169
+
170
+ The packaged CLI mirrors `examples/run_dpg_standard.py`. Run `dpg --help` for
171
+ its current options and use `examples/run_dpg_custom.py` for a dataset with a
172
+ specified target column.
173
+
174
+ ## Citation
175
+
176
+ If you use DPG in research, please cite:
177
+
178
+ ```bibtex
179
+ @inproceedings{arrighi2024dpg,
180
+ title={Decision Predicate Graphs: Enhancing Interpretability in Tree Ensembles},
181
+ author={Arrighi, Leonardo and Pennella, Luca and Marques Tavares, Gabriel and Barbon Junior, Sylvio},
182
+ booktitle={World Conference on Explainable Artificial Intelligence},
183
+ pages={311--332},
184
+ year={2024},
185
+ isbn={978-3-031-63797-1},
186
+ doi={10.1007/978-3-031-63797-1_16},
187
+ publisher={Springer Nature Switzerland},
188
+ }
189
+ ```
190
+
dpg-0.3.6/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # Decision Predicate Graph (DPG)
2
+
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
4
+ [![Install with pip](https://img.shields.io/badge/pip%20install-dpg-3775A9?logo=pypi&logoColor=white)](https://pypi.org/project/dpg/)
5
+ [![Python Versions](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-blue.svg)](pyproject.toml)
6
+ [![Tests](https://github.com/Meta-Group/DPG/actions/workflows/tests.yml/badge.svg)](https://github.com/Meta-Group/DPG/actions/workflows/tests.yml)
7
+ [![Release](https://github.com/Meta-Group/DPG/actions/workflows/release.yml/badge.svg)](https://github.com/Meta-Group/DPG/actions/workflows/release.yml)
8
+ [![Documentation Status](https://img.shields.io/readthedocs/dpg)](https://dpg.readthedocs.io/en/latest/)
9
+ [![Try the DPG sandbox](https://img.shields.io/badge/DPG%20sandbox-Try%20it%20in%20browser-4B5E27?logo=googlechrome&logoColor=white)](https://machinelearning.inginf.units.it/DPG_web/#how)
10
+
11
+ <p align="center">
12
+ <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/DPG.png" alt="DPG logo" width="300">
13
+ </p>
14
+
15
+ DPG is a model-agnostic framework for explaining tree ensembles. It converts
16
+ their decision logic into a graph, making predicates, transitions, predictions,
17
+ and structural diagnostics available in one representation.
18
+
19
+ ## Highlights
20
+
21
+ - Global and local explanations through `DPGExplainer`
22
+ - Graph metrics, including constraints, betweenness centrality, local reaching
23
+ centrality, and communities
24
+ - Local-path inspection, local-on-global plots, and faithfulness diagnostics
25
+ - Support for common scikit-learn tree ensembles
26
+
27
+ ## Install
28
+
29
+ ```bash
30
+ pip install dpg
31
+ ```
32
+
33
+ DPG requires Python 3.10 or later. To render graphs, also install the Graphviz
34
+ system package so that `dot` is on your `PATH`.
35
+
36
+ For development and documentation builds, see [docs/README.md](docs/README.md).
37
+
38
+ ## Quick start
39
+
40
+ ```python
41
+ import numpy as np
42
+ from sklearn.datasets import load_iris
43
+ from sklearn.ensemble import RandomForestClassifier
44
+ from dpg import DPGExplainer
45
+
46
+ X, y = load_iris(return_X_y=True, as_frame=True)
47
+ model = RandomForestClassifier(n_estimators=5, random_state=42).fit(X, y)
48
+
49
+ explainer = DPGExplainer(
50
+ model=model,
51
+ feature_names=X.columns.tolist(),
52
+ target_names=np.unique(y).astype(str).tolist(),
53
+ )
54
+ explanation = explainer.explain_global(X.values, communities=True)
55
+ explainer.plot("iris_dpg", explanation, save_dir="results")
56
+ ```
57
+
58
+ ## How DPG works
59
+
60
+ The concept behind DPG is to convert a generic tree-based ensemble model for classification into a graph, where:
61
+ - Nodes represent predicates, i.e., the feature-value associations present in each node of every tree;
62
+ - Edges denote the frequency with which these predicates are satisfied during the model training phase by the samples of the dataset.
63
+
64
+ <p align="center">
65
+ <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example.png" width="600" />
66
+ </p>
67
+
68
+ ## Metrics
69
+
70
+ The graph-based nature of DPG provides significant enhancements in the direction of a complete mapping of the ensemble structure.
71
+
72
+ | Property | Definition | Utility |
73
+ |--------------|------------|---------|
74
+ | _Constraints_ | The intervals of values for each feature obtained from all predicates connected by a path that culminates in a given class. | Calculate the classification boundary values of each feature associated with each class. |
75
+ | _Betweenness centrality_ | Quantifies the fraction of all the shortest paths between every pair of nodes of the graph passing through the considered node. | Identify potential bottleneck nodes that correspond to crucial decisions. |
76
+ | _Local reaching centrality_ | Quantifies the proportion of other nodes reachable from the local node through its outgoing edges. | Assess the importance of nodes similarly to feature importance, but enrich the information by encompassing the values associated with features across all decisions. |
77
+ | _Community_ | A subset of nodes of the DPG which is characterised by dense interconnections between its elements and sparse connections with the other nodes of the DPG that do not belong to the community. | Understanding the characteristics of nodes to be assigned to a particular community class, identifying predominant predicates, and those that play a marginal role in the classification process. |
78
+
79
+ |Constraints | Betweenness centrality | Local reaching centrality | Community|
80
+ |------------|------------|--------------|--------------------|
81
+ | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_constraints.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_bc.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_lrc.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_community.png) |
82
+ | Constraints(Class 1) = val3 < F1 ≤ val1, F2 ≤ val2 | BC(F2 ≤ val2) = 4/24 | LRC(F1 ≤ val1) = 6 / 7 | Community(Class 1) = F1 ≤ val1, F2 ≤ val2 |
83
+
84
+ ## Local explanations and faithfulness
85
+
86
+ Fit the explainer once, then inspect an individual prediction or compare local
87
+ explanations with the fitted model:
88
+
89
+ ```python
90
+ explainer.fit(X.values)
91
+ local = explainer.explain_local(X.iloc[0].values, sample_id=0)
92
+ details = explainer.evaluate_faithfulness(X.values, return_details=True)
93
+
94
+ print(local.majority_vote)
95
+ print(details["faithfulness_score"])
96
+ ```
97
+
98
+ `evaluate_faithfulness` reports model agreement, trace coverage,
99
+ recombination, evidence margin, and a composite heuristic score. It is an
100
+ explanation-fidelity measure, not a probability of ground-truth correctness.
101
+
102
+ ## Documentation and examples
103
+
104
+ - [Full documentation](https://dpg.readthedocs.io/en/latest/)
105
+ - [Quickstart](https://dpg.readthedocs.io/en/latest/quickstart.html)
106
+ - [Supported models](https://dpg.readthedocs.io/en/latest/supported_models.html)
107
+ - [Visualizations](https://dpg.readthedocs.io/en/latest/visualization.html)
108
+ - [Examples](examples), including [local explanations](examples/local_explanation_iris.py)
109
+ - [Parameter-sensitivity benchmark](tutorials/parameter_sensitivity_benchmark/README.md)
110
+ - [Interactive DPG sandbox](https://machinelearning.inginf.units.it/DPG_web/#how) — explore how DPG turns an ensemble into a graph in the browser.
111
+
112
+ <p align="center">
113
+ <a href="https://machinelearning.inginf.units.it/DPG_web/#how">
114
+ <img src="docs/_static/DPG.png" alt="Open the interactive DPG sandbox" width="180">
115
+ </a>
116
+ </p>
117
+
118
+ The packaged CLI mirrors `examples/run_dpg_standard.py`. Run `dpg --help` for
119
+ its current options and use `examples/run_dpg_custom.py` for a dataset with a
120
+ specified target column.
121
+
122
+ ## Citation
123
+
124
+ If you use DPG in research, please cite:
125
+
126
+ ```bibtex
127
+ @inproceedings{arrighi2024dpg,
128
+ title={Decision Predicate Graphs: Enhancing Interpretability in Tree Ensembles},
129
+ author={Arrighi, Leonardo and Pennella, Luca and Marques Tavares, Gabriel and Barbon Junior, Sylvio},
130
+ booktitle={World Conference on Explainable Artificial Intelligence},
131
+ pages={311--332},
132
+ year={2024},
133
+ isbn={978-3-031-63797-1},
134
+ doi={10.1007/978-3-031-63797-1_16},
135
+ publisher={Springer Nature Switzerland},
136
+ }
137
+ ```
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "dpg"
3
- version = "0.3.4"
3
+ version = "0.3.6"
4
4
  description = "A Python library for extracting Decision Predicate Graphs and global explanations from ensemble models."
5
5
  authors = ["Sylvio Barbon Junior <sylvio.barbonjunior@units.it>", "Leonardo Arrighi <leonardo.arrighi@phd.units.it>"]
6
6
  license = "MIT"
dpg-0.3.4/PKG-INFO DELETED
@@ -1,433 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: dpg
3
- Version: 0.3.4
4
- Summary: A Python library for extracting Decision Predicate Graphs and global explanations from ensemble models.
5
- License: MIT
6
- License-File: LICENSE
7
- Keywords: decision graph,ensemble learning,explainable ai,explainability,global explanations
8
- Author: Sylvio Barbon Junior
9
- Author-email: sylvio.barbonjunior@units.it
10
- Requires-Python: >=3.10
11
- Classifier: License :: OSI Approved :: MIT License
12
- Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.10
14
- Classifier: Programming Language :: Python :: 3.11
15
- Classifier: Programming Language :: Python :: 3.12
16
- Classifier: Programming Language :: Python :: 3.13
17
- Classifier: Programming Language :: Python :: 3.14
18
- Provides-Extra: dev
19
- Provides-Extra: docs
20
- Requires-Dist: contourpy (>=1.2)
21
- Requires-Dist: cycler (>=0.12)
22
- Requires-Dist: fonttools (>=4.53)
23
- Requires-Dist: graphviz (>=0.20)
24
- Requires-Dist: joblib (>=1.4)
25
- Requires-Dist: kiwisolver (>=1.4)
26
- Requires-Dist: matplotlib (>=3.9.1)
27
- Requires-Dist: myst-nb (>=1.1) ; extra == "docs"
28
- Requires-Dist: myst-parser (>=3.0) ; extra == "docs"
29
- Requires-Dist: networkx (>=3.3)
30
- Requires-Dist: numpy (>=2.0.0)
31
- Requires-Dist: packaging (>=24.1)
32
- Requires-Dist: pandas (>=2.2)
33
- Requires-Dist: pillow (>=10.4)
34
- Requires-Dist: pydata-sphinx-theme (>=0.15) ; extra == "docs"
35
- Requires-Dist: pyparsing (>=3.1)
36
- Requires-Dist: python-dateutil (>=2.9)
37
- Requires-Dist: pytz (>=2024.1)
38
- Requires-Dist: pyyaml (>=6.0.2)
39
- Requires-Dist: scikit-learn (>=1.5)
40
- Requires-Dist: scipy (>=1.14)
41
- Requires-Dist: six (>=1.16)
42
- Requires-Dist: sphinx (>=7.3) ; extra == "docs"
43
- Requires-Dist: sphinx-autoapi (>=3.1) ; extra == "docs"
44
- Requires-Dist: sphinx-copybutton (>=0.5) ; extra == "docs"
45
- Requires-Dist: sphinx-design (>=0.6) ; extra == "docs"
46
- Requires-Dist: threadpoolctl (>=3.5)
47
- Requires-Dist: tqdm (>=4.67)
48
- Requires-Dist: tzdata (>=2024.1)
49
- Project-URL: Homepage, https://github.com/Meta-Group/DPG
50
- Project-URL: Repository, https://github.com/Meta-Group/DPG
51
- Description-Content-Type: text/markdown
52
-
53
- # Decision Predicate Graph (DPG)
54
-
55
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
56
- [![Python Versions](https://img.shields.io/badge/python-3.10%20|%203.11%20|%203.12-blue.svg)](pyproject.toml)
57
- [![Build Status](https://github.com/Meta-Group/DPG/actions/workflows/tests.yml/badge.svg)](https://github.com/Meta-Group/DPG/actions/workflows/tests.yml)
58
- [![Release](https://github.com/Meta-Group/DPG/actions/workflows/release.yml/badge.svg)](https://github.com/Meta-Group/DPG/actions/workflows/release.yml)
59
- [![Documentation Status](https://readthedocs.org/projects/dpg/badge/?version=latest)](https://dpg.readthedocs.io/en/latest/)
60
-
61
- <p align="center">
62
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/DPG.png" alt="DPG logo" width="300">
63
- </p>
64
-
65
-
66
- DPG is a model-agnostic framework for global and local explanation of tree-based ensemble models.
67
- It turns ensemble decision logic into a graph so users can inspect predicates, transitions, predictions,
68
- and structural explanation diagnostics in a single representation.
69
-
70
- DPG is a graph structure that captures the tree-based ensemble model and learned dataset details,
71
- preserving the relations among features, logical decisions, and predictions towards emphasising
72
- insightful points. DPG enables graph-based evaluations and the identification of model decisions
73
- towards facilitating comparisons between features and their associated values while offering insights
74
- into the entire model. DPG provides descriptive metrics that enhance the understanding of the
75
- decisions inherent in the model, offering valuable insights.
76
- <p align="center">
77
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/image.png" width="600" />
78
- </p>
79
-
80
- ---
81
-
82
- ## At a glance
83
-
84
- - High-level API for global and local explanation with `DPGExplainer`
85
- - Graph-based metrics such as constraints, betweenness centrality, and local reaching centrality
86
- - Local path inspection, local-on-global visualization, and faithfulness diagnostics
87
- - Lightweight experiment runners and analysis utilities for sklearn datasets
88
-
89
- ## Quick links
90
-
91
- - Installation: [Installation](#installation)
92
- - High-level usage: [High-level usage](#high-level-usage)
93
- - Local explanations: [Local explanations](#local-explanations)
94
- - Faithfulness evaluation: [Faithfulness evaluation](#faithfulness-evaluation)
95
- - Visualizations: [https://dpg.readthedocs.io/en/latest/visualization.html](https://dpg.readthedocs.io/en/latest/visualization.html)
96
- - CLI usage and gallery examples: [CLI scripts](#cli-scripts)
97
- - Full docs: [https://dpg.readthedocs.io/](https://dpg.readthedocs.io/)
98
-
99
- ## How DPG works
100
- The concept behind DPG is to convert a generic tree-based ensemble model for classification into a graph, where:
101
- - Nodes represent predicates, i.e., the feature-value associations present in each node of every tree;
102
- - Edges denote the frequency with which these predicates are satisfied during the model training phase by the samples of the dataset.
103
-
104
- <p align="center">
105
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example.png" width="600" />
106
- </p>
107
-
108
- ## Metrics
109
- The graph-based nature of DPG provides significant enhancements in the direction of a complete mapping of the ensemble structure.
110
- | Property | Definition | Utility |
111
- |--------------|------------|---------|
112
- | _Constraints_ | The intervals of values for each feature obtained from all predicates connected by a path that culminates in a given class. | Calculate the classification boundary values of each feature associated with each class. |
113
- | _Betweenness centrality_ | Quantifies the fraction of all the shortest paths between every pair of nodes of the graph passing through the considered node. | Identify potential bottleneck nodes that correspond to crucial decisions. |
114
- | _Local reaching centrality_ | Quantifies the proportion of other nodes reachable from the local node through its outgoing edges. | Assess the importance of nodes similarly to feature importance, but enrich the information by encompassing the values associated with features across all decisions. |
115
- | _Community_ | A subset of nodes of the DPG which is characterised by dense interconnections between its elements and sparse connections with the other nodes of the DPG that do not belong to the community. | Understanding the characteristics of nodes to be assigned to a particular community class, identifying predominant predicates, and those that play a marginal role in the classification process. |
116
-
117
-
118
- |Constraints | Betweenness centrality | Local reaching centrality | Community|
119
- |------------|------------|--------------|--------------------|
120
- ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_constraints.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_bc.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_lrc.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_community.png) |
121
- |Constraints(Class 1) = val3 < F1 ≤ val1, F2 ≤ val2 | BC(F2 ≤ val2) = 4/24 | LRC(F1 ≤ val1) = 6 / 7 | Community(Class 1) = F1 ≤ val1, F2 ≤ val2 |
122
-
123
- ---
124
- ## Installation
125
-
126
- Install the package from PyPI:
127
-
128
- ```bash
129
- pip install dpg
130
- ```
131
-
132
- DPG requires Python 3.10+.
133
-
134
- If you want graph rendering, install the system Graphviz package as well so the
135
- `dot` executable is available on your `PATH`:
136
-
137
- - macOS (Homebrew): `brew install graphviz`
138
- - Ubuntu/Debian: `sudo apt-get install graphviz`
139
- - Windows (winget): `winget install Graphviz.Graphviz`
140
-
141
- For local development installs, editable mode, and documentation builds, see
142
- [docs/README.md](docs/README.md).
143
- ---
144
-
145
- ## Documentation
146
-
147
- For full documentation, visit [https://dpg.readthedocs.io/](https://dpg.readthedocs.io/).
148
-
149
- To build and serve documentation locally, see [docs/README.md](docs/README.md).
150
-
151
- Practitioner-focused benchmark material for `perc_var` and `decimal_threshold` is available in
152
- [tutorials/parameter_sensitivity_benchmark/README.md](tutorials/parameter_sensitivity_benchmark/README.md).
153
-
154
- ---
155
-
156
- ## High-level usage
157
-
158
- Here is a minimal high-level example:
159
-
160
- ```python
161
- import pandas as pd
162
- import numpy as np
163
- from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
164
- from dpg import DPGExplainer
165
-
166
- # Load dataset (last column assumed to be target)
167
- df = pd.read_csv("datasets/custom.csv", index_col=0)
168
- X = df.iloc[:, :-1]
169
- y = df.iloc[:, -1]
170
-
171
- # Train a classifier (supports RandomForest, GradientBoosting, AdaBoost, ExtraTree, and more)
172
- model = RandomForestClassifier(n_estimators=10, random_state=27)
173
- # or: model = GradientBoostingClassifier(n_estimators=10, random_state=27)
174
- model.fit(X, y)
175
-
176
- # Build the DPG and extract global explanations
177
- explainer = DPGExplainer(
178
- model=model,
179
- feature_names=X.columns,
180
- target_names=np.unique(y).astype(str).tolist(),
181
- )
182
- explanation = explainer.explain_global(X.values, communities=True)
183
-
184
- # Render the graph to disk
185
- explainer.plot("dpg_output", explanation, save_dir="datasets", export_pdf=True)
186
- explainer.plot_communities("dpg_output", explanation, save_dir="datasets", export_pdf=True)
187
- ```
188
-
189
- ### Common workflows
190
-
191
- The high-level API is designed to return structured outputs so downstream tools can use them directly.
192
-
193
- - `DPGExplainer.fit(X)`: builds the DPG structure
194
- - `DPGExplainer.explain_global(X=None, communities=False, community_threshold=0.2)`: returns a `DPGExplanation`
195
- - `DPGExplainer.explain_local(sample, sample_id=0, X=None, validate_graph=True)`: returns a `DPGLocalExplanation`
196
- - `DPGExplainer.local_path_dataframe(local_explanation)`: flattens local paths into a tabular view
197
- - `DPGExplainer.plot(...)`: renders the standard DPG
198
- - `DPGExplainer.plot_communities(...)`: renders a community-colored DPG
199
- - `DPGExplainer.plot_local_on_dpg(...)`: overlays one sample's local paths on the fitted DPG
200
- - `DPGExplainer.evaluate_faithfulness(...)`: summarizes local explanation fidelity and structural faithfulness over a dataset
201
-
202
- `DPGExplanation` includes `dot`, `graph`, `nodes`, `node_metrics`, `edge_metrics`, `class_boundaries`, and optional `communities`.
203
-
204
- ### Local explanations
205
-
206
- DPG also supports sample-level explanations on top of the fitted global graph.
207
-
208
- #### Graph construction modes
209
-
210
- You can control how the graph is built through `dpg.graph_construction.mode`:
211
-
212
- ```python
213
- from dpg import DPGExplainer
214
-
215
- explainer = DPGExplainer(
216
- model=model,
217
- feature_names=X.columns.tolist(),
218
- target_names=class_names,
219
- dpg_config={
220
- "dpg": {
221
- "default": {
222
- "perc_var": 1e-9,
223
- "decimal_threshold": 6,
224
- "n_jobs": -1,
225
- },
226
- "graph_construction": {
227
- "mode": "execution_trace", # or "aggregated_transitions"
228
- "context_order": 1, # 1, an integer > 1, or "auto"
229
- },
230
- }
231
- },
232
- )
233
- ```
234
-
235
- - `"aggregated_transitions"`: current default behavior; filters path variants first, then discovers the DPG.
236
- - `"execution_trace"`: builds directly from raw traces and filters edges instead of whole-path variants when `perc_var > 0`.
237
-
238
- `context_order` controls predicate identity in `execution_trace` mode. `1`
239
- preserves the legacy graph exactly; an order greater than one uses the last k
240
- executed predicates as context and keeps one sink per class. `"auto"` resolves
241
- the smallest order for which every pooled graph path is supported by an
242
- observed execution trace, including recombinations that only appear after
243
- multiple hops. Orders greater than one require `mode: "execution_trace"`.
244
- `context_order="auto"` also requires `execution_trace`; in
245
- `aggregated_transitions` mode context resolution is not applied.
246
-
247
- A phantom path is a multi-hop path assembled by pooling edges from different
248
- tree executions even though no single execution produced it. DPG-k removes
249
- these recombinations at the resolved context order while retaining one graph.
250
- The effective order and each node's context are available through
251
- `get_context_order()` and `get_node_context(node)`.
252
-
253
- **Regression scope (0.3.0):** `context_order` builds without raising for
254
- regressors (regression leaves, labeled `"Pred <value>"`, are treated as
255
- terminal sinks just like class leaves), but "one sink per output" is not a
256
- defined guarantee for regression the way it is for classification: two
257
- leaves collapse into the same sink only when their rounded values coincide,
258
- which is an artifact of label rounding, not a modeled invariant. A
259
- principled regression sink policy is deferred past 0.3.0. `class_boundaries`
260
- and `communities` remain classifier-only; calling `explain_global`
261
- with `communities=True` on a regressor raises a clear `ValueError`.
262
-
263
- #### Minimal local workflow
264
-
265
- ```python
266
- from sklearn.datasets import load_iris
267
- from sklearn.ensemble import RandomForestClassifier
268
- from dpg import DPGExplainer
269
- import numpy as np
270
-
271
- X, y = load_iris(return_X_y=True, as_frame=True)
272
- model = RandomForestClassifier(n_estimators=5, random_state=42).fit(X, y)
273
-
274
- explainer = DPGExplainer(
275
- model=model,
276
- feature_names=X.columns.tolist(),
277
- target_names=np.unique(y).astype(str).tolist(),
278
- )
279
- explainer.fit(X.values)
280
-
281
- local = explainer.explain_local(sample=X.iloc[0].values, sample_id=0)
282
-
283
- print(local.majority_vote)
284
- print(local.class_votes)
285
- print(local.sample_confidence)
286
-
287
- df_local = explainer.local_path_dataframe(local)
288
- print(df_local.head())
289
- ```
290
-
291
- `local.tree_paths[*].labels` stay in DPG label format such as `"sepal width (cm) <= 3.0"` and `"Class 0"`.
292
- For easier aggregation, `local.class_votes` and `local.majority_vote` use normalized class names such as `"0"` instead of `"Class 0"`.
293
-
294
- #### Local plotting
295
-
296
- ```python
297
- explainer.plot_local_on_dpg(
298
- "iris_local_sample0",
299
- local_explanation=local,
300
- true_class_label=str(y.iloc[0]),
301
- save_dir="results/",
302
- theme="dpg",
303
- palette="olive",
304
- layout_template="vertical",
305
- show=False,
306
- )
307
- ```
308
-
309
- A runnable example is available at [examples/local_explanation_iris.py](examples/local_explanation_iris.py).
310
-
311
- #### Faithfulness evaluation
312
-
313
- You can also evaluate local explanations against the fitted black-box model:
314
-
315
- ```python
316
- details = explainer.evaluate_faithfulness(
317
- X_test,
318
- y_true=y_test,
319
- return_details=True,
320
- )
321
-
322
- print(details["faithfulness_score"])
323
- print(details["output_fidelity"])
324
- print(details["mean_trace_coverage_score"])
325
- print(details["mean_recombination_rate"])
326
- ```
327
-
328
- This reports:
329
- - `output_fidelity`: agreement between the local explanation and the black-box model
330
- - structural metrics such as trace coverage and recombination
331
- - semantic metrics such as evidence margin
332
- - a composite `faithfulness_score`
333
-
334
- Important:
335
- - the composite score is a heuristic summary, not a calibrated probability
336
- - `output_fidelity` is model agreement, not ground-truth correctness
337
- - `local_accuracy` is only reported when `y_true` is provided
338
- - structural faithfulness here means recovering the executed decision traces used by the model
339
-
340
- ### Runnable examples
341
-
342
- - [examples/quickstart_iris.py](examples/quickstart_iris.py)
343
- - [examples/local_explanation_iris.py](examples/local_explanation_iris.py)
344
- - [examples](examples)
345
-
346
- ## CLI scripts
347
- The library contains two different scripts to apply DPG:
348
- - `examples/run_dpg_standard.py`: with this script it is possible to test DPG on a standard classification dataset provided by `sklearn` such as `iris`, `digits`, `wine`, `breast cancer`, and `diabetes`.
349
- - `examples/run_dpg_custom.py`: with this script it is possible to apply DPG to your classification dataset, specifying the target class.
350
-
351
- ### Implementation notes
352
- The library also contains two other essential scripts:
353
- - `core.py` contains all the functions used to calculate and create the DPG and the metrics.
354
- - `visualizer.py` contains the functions used to manage the visualization of DPG.
355
-
356
- ### Output
357
- The DPG output, through `examples/run_dpg_standard.py` or `examples/run_dpg_custom.py`, produces several files:
358
- - the visualization of DPG in a dedicated environment, which can be zoomed and saved;
359
- - a `.txt` file containing the DPG metrics;
360
- - a `.csv` file containing the information about all the nodes of the DPG and their associated metrics;
361
- - a `.txt` file containing the Random Forest statistics (accuracy, confusion matrix, classification report)
362
-
363
- ### CLI parameter reference
364
- Usage: `dpg --dataset <dataset_name> --n_learners <integer_number> --pv <threshold_value> --t <integer_number> --model_name <str_model_name> --dir <save_dir_path> --plot --save_plot_dir <save_plot_dir_path> --attribute <attribute> --communities --clusters --threshold_clusters <float> --class_flag --seed <int>`
365
-
366
- After installing DPG, the `dpg` command is the packaged equivalent of
367
- `python examples/run_dpg_standard.py`.
368
- Where:
369
- - `dataset` is the name of the standard classification `sklearn` dataset to be analyzed;
370
- - `n_learners` is the number of base learners for the ensemble model;
371
- - `pv` is the threshold value indicating the desire to retain only those paths that occur with a frequency exceeding a specified proportion across the trees;
372
- - `t` is the decimal precision of each feature;
373
- - `model_name` is the name of the `sklearn` model chosen to perform classification. Supported models include:
374
- - `RandomForestClassifier` (default)
375
- - `GradientBoostingClassifier` (new!)
376
- - `RandomForestRegressor`
377
- - `GradientBoostingRegressor`
378
- - `ExtraTreesClassifier`
379
- - `AdaBoostClassifier`
380
- - `AdaBoostRegressor`
381
- - `BaggingClassifier`
382
- - `dir` is the path of the directory to save the files;
383
- - `plot` is a store_true variable which can be added to plot the DPG;
384
- - `save_plot_dir` is the path of the directory to save the plot image;
385
- - `attribute` is the specific node metric which can be visualized on the DPG;
386
- - `communities` is a store_true variable which can be added to visualize communities on the DPG;
387
- - `clusters` is a store_true variable which can be added to visualize clusters on the DPG;
388
- - `threshold_clusters` is the threshold used to detect ambiguous nodes in clusters;
389
- - `class_flag` is a store_true variable which can be added to highlight class nodes;
390
- - `seed` controls the random split.
391
-
392
- Disclaimer: `attribute`, `communities`, and `clusters` are mutually exclusive: DPG supports just one visualization mode at a time.
393
-
394
- The usage of `examples/run_dpg_custom.py` is similar, but it requires another parameter:
395
- - `target_column`, which is the name of the column to be used as the target variable;
396
- - while `ds` is the path of the directory where the dataset is.
397
-
398
- ### Example `examples/run_dpg_standard.py`
399
- Some examples can be appreciated in the `examples` folder: https://github.com/Meta-Group/DPG/tree/main/examples
400
-
401
- In particular, the following DPG is obtained by transforming a Random Forest with 5 base learners, trained on Iris dataset.
402
- The used command is `dpg --dataset iris --n_learners 5 --pv 0.001 --t 2 --dir examples --plot --save_plot_dir examples`.
403
- <p align="center">
404
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/iris_bl5_perc0.001_dec2.png" width="800" />
405
- </p>
406
-
407
- The following visualizations are obtained using the same parameters as the previous example, but they show two different metrics: _Community_ and _Betweenness centrality_.
408
- The used command for showing communities is `dpg --dataset iris --n_learners 5 --pv 0.001 --t 2 --dir examples --plot --save_plot_dir examples --communities`.
409
- <p align="center">
410
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/iris_bl5_perc0.001_dec2_communities.png" width="800" />
411
- </p>
412
-
413
- The used command for showing a specific property is `dpg --dataset iris --n_learners 5 --pv 0.001 --t 2 --dir examples --plot --save_plot_dir examples --attribute "Betweenness centrality" --class_flag`.
414
- <p align="center">
415
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/iris_bl5_perc0.001_dec2_Betweennesscentrality.png" width="800" />
416
- </p>
417
-
418
- ***
419
- ## Citation
420
- If you use this for research, please cite. Here is an example BibTeX entry:
421
-
422
- ```bibtex
423
- @inproceedings{arrighi2024dpg,
424
- title={Decision Predicate Graphs: Enhancing Interpretability in Tree Ensembles},
425
- author={Arrighi, Leonardo and Pennella, Luca and Marques Tavares, Gabriel and Barbon Junior, Sylvio},
426
- booktitle={World Conference on Explainable Artificial Intelligence},
427
- pages={311--332},
428
- year={2024},
429
- isbn = {978-3-031-63797-1},
430
- doi = {10.1007/978-3-031-63797-1_16},
431
- publisher = {Springer Nature Switzerland},
432
- }
433
-
dpg-0.3.4/README.md DELETED
@@ -1,380 +0,0 @@
1
- # Decision Predicate Graph (DPG)
2
-
3
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
4
- [![Python Versions](https://img.shields.io/badge/python-3.10%20|%203.11%20|%203.12-blue.svg)](pyproject.toml)
5
- [![Build Status](https://github.com/Meta-Group/DPG/actions/workflows/tests.yml/badge.svg)](https://github.com/Meta-Group/DPG/actions/workflows/tests.yml)
6
- [![Release](https://github.com/Meta-Group/DPG/actions/workflows/release.yml/badge.svg)](https://github.com/Meta-Group/DPG/actions/workflows/release.yml)
7
- [![Documentation Status](https://readthedocs.org/projects/dpg/badge/?version=latest)](https://dpg.readthedocs.io/en/latest/)
8
-
9
- <p align="center">
10
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/DPG.png" alt="DPG logo" width="300">
11
- </p>
12
-
13
-
14
- DPG is a model-agnostic framework for global and local explanation of tree-based ensemble models.
15
- It turns ensemble decision logic into a graph so users can inspect predicates, transitions, predictions,
16
- and structural explanation diagnostics in a single representation.
17
-
18
- DPG is a graph structure that captures the tree-based ensemble model and learned dataset details,
19
- preserving the relations among features, logical decisions, and predictions towards emphasising
20
- insightful points. DPG enables graph-based evaluations and the identification of model decisions
21
- towards facilitating comparisons between features and their associated values while offering insights
22
- into the entire model. DPG provides descriptive metrics that enhance the understanding of the
23
- decisions inherent in the model, offering valuable insights.
24
- <p align="center">
25
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/image.png" width="600" />
26
- </p>
27
-
28
- ---
29
-
30
- ## At a glance
31
-
32
- - High-level API for global and local explanation with `DPGExplainer`
33
- - Graph-based metrics such as constraints, betweenness centrality, and local reaching centrality
34
- - Local path inspection, local-on-global visualization, and faithfulness diagnostics
35
- - Lightweight experiment runners and analysis utilities for sklearn datasets
36
-
37
- ## Quick links
38
-
39
- - Installation: [Installation](#installation)
40
- - High-level usage: [High-level usage](#high-level-usage)
41
- - Local explanations: [Local explanations](#local-explanations)
42
- - Faithfulness evaluation: [Faithfulness evaluation](#faithfulness-evaluation)
43
- - Visualizations: [https://dpg.readthedocs.io/en/latest/visualization.html](https://dpg.readthedocs.io/en/latest/visualization.html)
44
- - CLI usage and gallery examples: [CLI scripts](#cli-scripts)
45
- - Full docs: [https://dpg.readthedocs.io/](https://dpg.readthedocs.io/)
46
-
47
- ## How DPG works
48
- The concept behind DPG is to convert a generic tree-based ensemble model for classification into a graph, where:
49
- - Nodes represent predicates, i.e., the feature-value associations present in each node of every tree;
50
- - Edges denote the frequency with which these predicates are satisfied during the model training phase by the samples of the dataset.
51
-
52
- <p align="center">
53
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example.png" width="600" />
54
- </p>
55
-
56
- ## Metrics
57
- The graph-based nature of DPG provides significant enhancements in the direction of a complete mapping of the ensemble structure.
58
- | Property | Definition | Utility |
59
- |--------------|------------|---------|
60
- | _Constraints_ | The intervals of values for each feature obtained from all predicates connected by a path that culminates in a given class. | Calculate the classification boundary values of each feature associated with each class. |
61
- | _Betweenness centrality_ | Quantifies the fraction of all the shortest paths between every pair of nodes of the graph passing through the considered node. | Identify potential bottleneck nodes that correspond to crucial decisions. |
62
- | _Local reaching centrality_ | Quantifies the proportion of other nodes reachable from the local node through its outgoing edges. | Assess the importance of nodes similarly to feature importance, but enrich the information by encompassing the values associated with features across all decisions. |
63
- | _Community_ | A subset of nodes of the DPG which is characterised by dense interconnections between its elements and sparse connections with the other nodes of the DPG that do not belong to the community. | Understanding the characteristics of nodes to be assigned to a particular community class, identifying predominant predicates, and those that play a marginal role in the classification process. |
64
-
65
-
66
- |Constraints | Betweenness centrality | Local reaching centrality | Community|
67
- |------------|------------|--------------|--------------------|
68
- ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_constraints.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_bc.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_lrc.png) | ![](https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/example_community.png) |
69
- |Constraints(Class 1) = val3 < F1 ≤ val1, F2 ≤ val2 | BC(F2 ≤ val2) = 4/24 | LRC(F1 ≤ val1) = 6 / 7 | Community(Class 1) = F1 ≤ val1, F2 ≤ val2 |
70
-
71
- ---
72
- ## Installation
73
-
74
- Install the package from PyPI:
75
-
76
- ```bash
77
- pip install dpg
78
- ```
79
-
80
- DPG requires Python 3.10+.
81
-
82
- If you want graph rendering, install the system Graphviz package as well so the
83
- `dot` executable is available on your `PATH`:
84
-
85
- - macOS (Homebrew): `brew install graphviz`
86
- - Ubuntu/Debian: `sudo apt-get install graphviz`
87
- - Windows (winget): `winget install Graphviz.Graphviz`
88
-
89
- For local development installs, editable mode, and documentation builds, see
90
- [docs/README.md](docs/README.md).
91
- ---
92
-
93
- ## Documentation
94
-
95
- For full documentation, visit [https://dpg.readthedocs.io/](https://dpg.readthedocs.io/).
96
-
97
- To build and serve documentation locally, see [docs/README.md](docs/README.md).
98
-
99
- Practitioner-focused benchmark material for `perc_var` and `decimal_threshold` is available in
100
- [tutorials/parameter_sensitivity_benchmark/README.md](tutorials/parameter_sensitivity_benchmark/README.md).
101
-
102
- ---
103
-
104
- ## High-level usage
105
-
106
- Here is a minimal high-level example:
107
-
108
- ```python
109
- import pandas as pd
110
- import numpy as np
111
- from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
112
- from dpg import DPGExplainer
113
-
114
- # Load dataset (last column assumed to be target)
115
- df = pd.read_csv("datasets/custom.csv", index_col=0)
116
- X = df.iloc[:, :-1]
117
- y = df.iloc[:, -1]
118
-
119
- # Train a classifier (supports RandomForest, GradientBoosting, AdaBoost, ExtraTree, and more)
120
- model = RandomForestClassifier(n_estimators=10, random_state=27)
121
- # or: model = GradientBoostingClassifier(n_estimators=10, random_state=27)
122
- model.fit(X, y)
123
-
124
- # Build the DPG and extract global explanations
125
- explainer = DPGExplainer(
126
- model=model,
127
- feature_names=X.columns,
128
- target_names=np.unique(y).astype(str).tolist(),
129
- )
130
- explanation = explainer.explain_global(X.values, communities=True)
131
-
132
- # Render the graph to disk
133
- explainer.plot("dpg_output", explanation, save_dir="datasets", export_pdf=True)
134
- explainer.plot_communities("dpg_output", explanation, save_dir="datasets", export_pdf=True)
135
- ```
136
-
137
- ### Common workflows
138
-
139
- The high-level API is designed to return structured outputs so downstream tools can use them directly.
140
-
141
- - `DPGExplainer.fit(X)`: builds the DPG structure
142
- - `DPGExplainer.explain_global(X=None, communities=False, community_threshold=0.2)`: returns a `DPGExplanation`
143
- - `DPGExplainer.explain_local(sample, sample_id=0, X=None, validate_graph=True)`: returns a `DPGLocalExplanation`
144
- - `DPGExplainer.local_path_dataframe(local_explanation)`: flattens local paths into a tabular view
145
- - `DPGExplainer.plot(...)`: renders the standard DPG
146
- - `DPGExplainer.plot_communities(...)`: renders a community-colored DPG
147
- - `DPGExplainer.plot_local_on_dpg(...)`: overlays one sample's local paths on the fitted DPG
148
- - `DPGExplainer.evaluate_faithfulness(...)`: summarizes local explanation fidelity and structural faithfulness over a dataset
149
-
150
- `DPGExplanation` includes `dot`, `graph`, `nodes`, `node_metrics`, `edge_metrics`, `class_boundaries`, and optional `communities`.
151
-
152
- ### Local explanations
153
-
154
- DPG also supports sample-level explanations on top of the fitted global graph.
155
-
156
- #### Graph construction modes
157
-
158
- You can control how the graph is built through `dpg.graph_construction.mode`:
159
-
160
- ```python
161
- from dpg import DPGExplainer
162
-
163
- explainer = DPGExplainer(
164
- model=model,
165
- feature_names=X.columns.tolist(),
166
- target_names=class_names,
167
- dpg_config={
168
- "dpg": {
169
- "default": {
170
- "perc_var": 1e-9,
171
- "decimal_threshold": 6,
172
- "n_jobs": -1,
173
- },
174
- "graph_construction": {
175
- "mode": "execution_trace", # or "aggregated_transitions"
176
- "context_order": 1, # 1, an integer > 1, or "auto"
177
- },
178
- }
179
- },
180
- )
181
- ```
182
-
183
- - `"aggregated_transitions"`: current default behavior; filters path variants first, then discovers the DPG.
184
- - `"execution_trace"`: builds directly from raw traces and filters edges instead of whole-path variants when `perc_var > 0`.
185
-
186
- `context_order` controls predicate identity in `execution_trace` mode. `1`
187
- preserves the legacy graph exactly; an order greater than one uses the last k
188
- executed predicates as context and keeps one sink per class. `"auto"` resolves
189
- the smallest order for which every pooled graph path is supported by an
190
- observed execution trace, including recombinations that only appear after
191
- multiple hops. Orders greater than one require `mode: "execution_trace"`.
192
- `context_order="auto"` also requires `execution_trace`; in
193
- `aggregated_transitions` mode context resolution is not applied.
194
-
195
- A phantom path is a multi-hop path assembled by pooling edges from different
196
- tree executions even though no single execution produced it. DPG-k removes
197
- these recombinations at the resolved context order while retaining one graph.
198
- The effective order and each node's context are available through
199
- `get_context_order()` and `get_node_context(node)`.
200
-
201
- **Regression scope (0.3.0):** `context_order` builds without raising for
202
- regressors (regression leaves, labeled `"Pred <value>"`, are treated as
203
- terminal sinks just like class leaves), but "one sink per output" is not a
204
- defined guarantee for regression the way it is for classification: two
205
- leaves collapse into the same sink only when their rounded values coincide,
206
- which is an artifact of label rounding, not a modeled invariant. A
207
- principled regression sink policy is deferred past 0.3.0. `class_boundaries`
208
- and `communities` remain classifier-only; calling `explain_global`
209
- with `communities=True` on a regressor raises a clear `ValueError`.
210
-
211
- #### Minimal local workflow
212
-
213
- ```python
214
- from sklearn.datasets import load_iris
215
- from sklearn.ensemble import RandomForestClassifier
216
- from dpg import DPGExplainer
217
- import numpy as np
218
-
219
- X, y = load_iris(return_X_y=True, as_frame=True)
220
- model = RandomForestClassifier(n_estimators=5, random_state=42).fit(X, y)
221
-
222
- explainer = DPGExplainer(
223
- model=model,
224
- feature_names=X.columns.tolist(),
225
- target_names=np.unique(y).astype(str).tolist(),
226
- )
227
- explainer.fit(X.values)
228
-
229
- local = explainer.explain_local(sample=X.iloc[0].values, sample_id=0)
230
-
231
- print(local.majority_vote)
232
- print(local.class_votes)
233
- print(local.sample_confidence)
234
-
235
- df_local = explainer.local_path_dataframe(local)
236
- print(df_local.head())
237
- ```
238
-
239
- `local.tree_paths[*].labels` stay in DPG label format such as `"sepal width (cm) <= 3.0"` and `"Class 0"`.
240
- For easier aggregation, `local.class_votes` and `local.majority_vote` use normalized class names such as `"0"` instead of `"Class 0"`.
241
-
242
- #### Local plotting
243
-
244
- ```python
245
- explainer.plot_local_on_dpg(
246
- "iris_local_sample0",
247
- local_explanation=local,
248
- true_class_label=str(y.iloc[0]),
249
- save_dir="results/",
250
- theme="dpg",
251
- palette="olive",
252
- layout_template="vertical",
253
- show=False,
254
- )
255
- ```
256
-
257
- A runnable example is available at [examples/local_explanation_iris.py](examples/local_explanation_iris.py).
258
-
259
- #### Faithfulness evaluation
260
-
261
- You can also evaluate local explanations against the fitted black-box model:
262
-
263
- ```python
264
- details = explainer.evaluate_faithfulness(
265
- X_test,
266
- y_true=y_test,
267
- return_details=True,
268
- )
269
-
270
- print(details["faithfulness_score"])
271
- print(details["output_fidelity"])
272
- print(details["mean_trace_coverage_score"])
273
- print(details["mean_recombination_rate"])
274
- ```
275
-
276
- This reports:
277
- - `output_fidelity`: agreement between the local explanation and the black-box model
278
- - structural metrics such as trace coverage and recombination
279
- - semantic metrics such as evidence margin
280
- - a composite `faithfulness_score`
281
-
282
- Important:
283
- - the composite score is a heuristic summary, not a calibrated probability
284
- - `output_fidelity` is model agreement, not ground-truth correctness
285
- - `local_accuracy` is only reported when `y_true` is provided
286
- - structural faithfulness here means recovering the executed decision traces used by the model
287
-
288
- ### Runnable examples
289
-
290
- - [examples/quickstart_iris.py](examples/quickstart_iris.py)
291
- - [examples/local_explanation_iris.py](examples/local_explanation_iris.py)
292
- - [examples](examples)
293
-
294
- ## CLI scripts
295
- The library contains two different scripts to apply DPG:
296
- - `examples/run_dpg_standard.py`: with this script it is possible to test DPG on a standard classification dataset provided by `sklearn` such as `iris`, `digits`, `wine`, `breast cancer`, and `diabetes`.
297
- - `examples/run_dpg_custom.py`: with this script it is possible to apply DPG to your classification dataset, specifying the target class.
298
-
299
- ### Implementation notes
300
- The library also contains two other essential scripts:
301
- - `core.py` contains all the functions used to calculate and create the DPG and the metrics.
302
- - `visualizer.py` contains the functions used to manage the visualization of DPG.
303
-
304
- ### Output
305
- The DPG output, through `examples/run_dpg_standard.py` or `examples/run_dpg_custom.py`, produces several files:
306
- - the visualization of DPG in a dedicated environment, which can be zoomed and saved;
307
- - a `.txt` file containing the DPG metrics;
308
- - a `.csv` file containing the information about all the nodes of the DPG and their associated metrics;
309
- - a `.txt` file containing the Random Forest statistics (accuracy, confusion matrix, classification report)
310
-
311
- ### CLI parameter reference
312
- Usage: `dpg --dataset <dataset_name> --n_learners <integer_number> --pv <threshold_value> --t <integer_number> --model_name <str_model_name> --dir <save_dir_path> --plot --save_plot_dir <save_plot_dir_path> --attribute <attribute> --communities --clusters --threshold_clusters <float> --class_flag --seed <int>`
313
-
314
- After installing DPG, the `dpg` command is the packaged equivalent of
315
- `python examples/run_dpg_standard.py`.
316
- Where:
317
- - `dataset` is the name of the standard classification `sklearn` dataset to be analyzed;
318
- - `n_learners` is the number of base learners for the ensemble model;
319
- - `pv` is the threshold value indicating the desire to retain only those paths that occur with a frequency exceeding a specified proportion across the trees;
320
- - `t` is the decimal precision of each feature;
321
- - `model_name` is the name of the `sklearn` model chosen to perform classification. Supported models include:
322
- - `RandomForestClassifier` (default)
323
- - `GradientBoostingClassifier` (new!)
324
- - `RandomForestRegressor`
325
- - `GradientBoostingRegressor`
326
- - `ExtraTreesClassifier`
327
- - `AdaBoostClassifier`
328
- - `AdaBoostRegressor`
329
- - `BaggingClassifier`
330
- - `dir` is the path of the directory to save the files;
331
- - `plot` is a store_true variable which can be added to plot the DPG;
332
- - `save_plot_dir` is the path of the directory to save the plot image;
333
- - `attribute` is the specific node metric which can be visualized on the DPG;
334
- - `communities` is a store_true variable which can be added to visualize communities on the DPG;
335
- - `clusters` is a store_true variable which can be added to visualize clusters on the DPG;
336
- - `threshold_clusters` is the threshold used to detect ambiguous nodes in clusters;
337
- - `class_flag` is a store_true variable which can be added to highlight class nodes;
338
- - `seed` controls the random split.
339
-
340
- Disclaimer: `attribute`, `communities`, and `clusters` are mutually exclusive: DPG supports just one visualization mode at a time.
341
-
342
- The usage of `examples/run_dpg_custom.py` is similar, but it requires another parameter:
343
- - `target_column`, which is the name of the column to be used as the target variable;
344
- - while `ds` is the path of the directory where the dataset is.
345
-
346
- ### Example `examples/run_dpg_standard.py`
347
- Some examples can be appreciated in the `examples` folder: https://github.com/Meta-Group/DPG/tree/main/examples
348
-
349
- In particular, the following DPG is obtained by transforming a Random Forest with 5 base learners, trained on Iris dataset.
350
- The used command is `dpg --dataset iris --n_learners 5 --pv 0.001 --t 2 --dir examples --plot --save_plot_dir examples`.
351
- <p align="center">
352
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/iris_bl5_perc0.001_dec2.png" width="800" />
353
- </p>
354
-
355
- The following visualizations are obtained using the same parameters as the previous example, but they show two different metrics: _Community_ and _Betweenness centrality_.
356
- The used command for showing communities is `dpg --dataset iris --n_learners 5 --pv 0.001 --t 2 --dir examples --plot --save_plot_dir examples --communities`.
357
- <p align="center">
358
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/iris_bl5_perc0.001_dec2_communities.png" width="800" />
359
- </p>
360
-
361
- The used command for showing a specific property is `dpg --dataset iris --n_learners 5 --pv 0.001 --t 2 --dir examples --plot --save_plot_dir examples --attribute "Betweenness centrality" --class_flag`.
362
- <p align="center">
363
- <img src="https://raw.githubusercontent.com/Meta-Group/DPG/main/dpg_image_examples/iris_bl5_perc0.001_dec2_Betweennesscentrality.png" width="800" />
364
- </p>
365
-
366
- ***
367
- ## Citation
368
- If you use this for research, please cite. Here is an example BibTeX entry:
369
-
370
- ```bibtex
371
- @inproceedings{arrighi2024dpg,
372
- title={Decision Predicate Graphs: Enhancing Interpretability in Tree Ensembles},
373
- author={Arrighi, Leonardo and Pennella, Luca and Marques Tavares, Gabriel and Barbon Junior, Sylvio},
374
- booktitle={World Conference on Explainable Artificial Intelligence},
375
- pages={311--332},
376
- year={2024},
377
- isbn = {978-3-031-63797-1},
378
- doi = {10.1007/978-3-031-63797-1_16},
379
- publisher = {Springer Nature Switzerland},
380
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes