Skip to main content

A simple Python package Optimal Counterfactual Explanations in Tree Ensembles

Project description

Optimal Counterfactual Explanations in Tree Ensembles

Logo

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)
print("CP : ",cp_explanation)

Expected output:

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 Explanation:
Age              : 39.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

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 ⏳ Upcoming Implementation planned.

Legend: ✅ available · ⏳ upcoming

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oceanpy-2.0.0.tar.gz (58.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oceanpy-2.0.0-py3-none-any.whl (43.0 kB view details)

Uploaded Python 3

File details

Details for the file oceanpy-2.0.0.tar.gz.

File metadata

  • Download URL: oceanpy-2.0.0.tar.gz
  • Upload date:
  • Size: 58.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for oceanpy-2.0.0.tar.gz
Algorithm Hash digest
SHA256 a01b7b017d60742bc1de006f5fd9c4ee1b57f6ec87b1dc96131efba709461e88
MD5 85994796c0b09fb1d5247d7383add5ff
BLAKE2b-256 8e8c58c471bb11d9e5db4580841eb13419f2472fe739ce4e0fc1cd09b6e29964

See more details on using hashes here.

Provenance

The following attestation bundles were made for oceanpy-2.0.0.tar.gz:

Publisher: publish.yml on vidalt/OCEAN

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file oceanpy-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: oceanpy-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 43.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for oceanpy-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 51a100aabdab253aecf15990916709c8deb52c140eab836d6a86f06a7d16278e
MD5 69e71a51637a8e0c0614409a87325e4f
BLAKE2b-256 3f3b6a6b0a0766385906b57b157521728308121e6c2af209ed754eed33bbc9d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for oceanpy-2.0.0-py3-none-any.whl:

Publisher: publish.yml on vidalt/OCEAN

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page