Skip to main content

A simple Python package Optimal Counterfactual Explanations in Tree Ensembles

Project description

Optimal Counterfactual Explanations in Tree Ensembles

Logo

This repository provides methods to generate optimal counterfactual explanations in tree ensembles. It is based 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.

Installation

This project requires the gurobi solver. You can request for a free academic license here. Once you have installed gurobi, you can install the package with the following command:

pip install oceanpy

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

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-0.2.1.tar.gz (56.0 kB view details)

Uploaded Source

Built Distribution

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

oceanpy-0.2.1-py3-none-any.whl (42.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: oceanpy-0.2.1.tar.gz
  • Upload date:
  • Size: 56.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for oceanpy-0.2.1.tar.gz
Algorithm Hash digest
SHA256 66d498e889924c3e02f354fd00a9bafeb024d38e37158ab676e8b9dda974611f
MD5 c3c63df1c8ad8d54cd910248d93e1b57
BLAKE2b-256 9816ceae57d23b2374f07c01a33f3045a258ca785cac1f72aa74c0ee301e965f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: oceanpy-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 42.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for oceanpy-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 51fa0a7e8a24c312222460cf859165bb75b4775e196bae4446122df706ad643d
MD5 a38b40a06aac87dee9100269fda9940e
BLAKE2b-256 77aecf2d64c26d1669469ec4dfb5a2c4e2b00acfa22fd5e098810dac4aecad61

See more details on using hashes here.

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