A simple Python package Optimal Counterfactual Explanations in Tree Ensembles
Project description
Optimal Counterfactual Explanations in Tree Ensembles
ocean is a full package dedicated to counterfactual explanations for tree ensembles.
It builds on the paper Optimal Counterfactual Explanations in Tree Ensemble by Axel Parmentier and Thibaut Vidal in the Proceedings of the thirty-eighth International Conference on Machine Learning, 2021, in press. The article is available here.
Beyond the original MIP approach, ocean includes a new constraint programming (CP) method and will grow to cover additional formulations and heuristics.
Installation
You can install the package with the following command:
pip install oceanpy
Note : The MIP method requires the gurobi solver access. You can request for a free academic license here. Once you have installed gurobi, you can install the package with the command above. However, you can also use the CP method without gurobi.
Usage
The package provides multiple classes and functions to wrap the tree ensemble models from the scikit-learn library. A minimal example is provided below:
from sklearn.ensemble import RandomForestClassifier
from ocean import MixedIntegerProgramExplainer, ConstraintProgrammingExplainer
from ocean.datasets import load_adult
# Load the adult dataset
(data, target), mapper = load_adult()
# Select an instance to explain from the dataset
x = data.iloc[0].to_frame().T
# Train a random forest classifier
rf = RandomForestClassifier(n_estimators=10, max_depth=3, random_state=42)
rf.fit(data, target)
# Predict the class of the random instance
y = int(rf.predict(x).item())
# Explain the prediction using MIPEXplainer
mip_model = MixedIntegerProgramExplainer(rf, mapper=mapper)
x = x.to_numpy().flatten()
mip_explanation = mip_model.explain(x, y=1 - y, norm=1)
# Explain the prediction using CPEExplainer
cp_model = ConstraintProgrammingExplainer(rf, mapper=mapper)
x = x.to_numpy().flatten()
cp_explanation = cp_model.explain(x, y=1 - y, norm=1)
# Show the explanation
print("MIP: ",mip_explanation, "\n")
print("CP : ",cp_explanation)
Expected output:
MIP objective value: 5.0
MIP Explanation:
Age : 39.0
CapitalGain : 2174.0
CapitalLoss : 0
EducationNumber : 13.0
HoursPerWeek : 41.0
MaritalStatus : 3
NativeCountry : 0
Occupation : 1
Relationship : 0
Sex : 0
WorkClass : 6
CP objective value: 5.0
CP Explanation:
Age : 38.0
CapitalGain : 2174.0
CapitalLoss : 0.0
EducationNumber : 13.0
HoursPerWeek : 41.0
MaritalStatus : 3
NativeCountry : 0
Occupation : 1
Relationship : 0
Sex : 0
WorkClass : 6
See the examples folder for more usage examples.
Feature Preview & Roadmap
| Area | Status | Notes / References |
|---|---|---|
| MIP formulation | ✅ Done | Based on Parmentier & Vidal (2020/2021). |
| Constraint Programming (CP) | ✅ Done | Based on an upcoming paper. |
| MaxSAT formulation | ⏳ Upcoming | Planned addition to the toolbox. |
| Heuristics | ⏳ Upcoming | Fast approximate methods. |
| Other methods | ⏳ Upcoming | Additional formulations under exploration. |
| Random Forest support | ✅ Ready | Fully supported in ocean. |
| XGBoost support | ✅ Ready | Fully supported in ocean. |
Legend: ✅ available · ⏳ upcoming
Stargazers over time
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file oceanpy-2.0.1.tar.gz.
File metadata
- Download URL: oceanpy-2.0.1.tar.gz
- Upload date:
- Size: 66.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f09aa027b5bfb7966715a688655f67a1abd94d71f9c8c9dd6b37f27a2e6b7016
|
|
| MD5 |
a94e44215be93305cfc6e7cac24059e2
|
|
| BLAKE2b-256 |
2f7cd0148277eec57827fb6117c6444d170d4c47d6e6ad8ab28e1a4cfc6b264c
|
Provenance
The following attestation bundles were made for oceanpy-2.0.1.tar.gz:
Publisher:
publish.yml on vidalt/OCEAN
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
oceanpy-2.0.1.tar.gz -
Subject digest:
f09aa027b5bfb7966715a688655f67a1abd94d71f9c8c9dd6b37f27a2e6b7016 - Sigstore transparency entry: 610045105
- Sigstore integration time:
-
Permalink:
vidalt/OCEAN@a2f4f0092934314fc2c922734fc183c476ef65e3 -
Branch / Tag:
refs/tags/v2.0.1 - Owner: https://github.com/vidalt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a2f4f0092934314fc2c922734fc183c476ef65e3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file oceanpy-2.0.1-py3-none-any.whl.
File metadata
- Download URL: oceanpy-2.0.1-py3-none-any.whl
- Upload date:
- Size: 54.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
241f58d4decca22136e344415ba2a95631e61faac57bdda01bdd7e29453c269b
|
|
| MD5 |
da826b6f4ce9d11d1ebd5bfd4c9d587e
|
|
| BLAKE2b-256 |
edfc6ae3ae7470e5a72c6d4b61125981965e969bc3d743d413fc35fa6ff757e4
|
Provenance
The following attestation bundles were made for oceanpy-2.0.1-py3-none-any.whl:
Publisher:
publish.yml on vidalt/OCEAN
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
oceanpy-2.0.1-py3-none-any.whl -
Subject digest:
241f58d4decca22136e344415ba2a95631e61faac57bdda01bdd7e29453c269b - Sigstore transparency entry: 610045113
- Sigstore integration time:
-
Permalink:
vidalt/OCEAN@a2f4f0092934314fc2c922734fc183c476ef65e3 -
Branch / Tag:
refs/tags/v2.0.1 - Owner: https://github.com/vidalt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a2f4f0092934314fc2c922734fc183c476ef65e3 -
Trigger Event:
push
-
Statement type: