Skip to main content

actionable-recourse is a python library for recourse verification and reporting.

Project description

actionable-recourse is a python library for recourse verification and reporting.

Recourse in Machine Learning?

Recourse is the ability of a person to change the prediction of a machine learning model by altering actionable input variables – e.g., income and n_credit_cards as opposed to age or alma_mater.

Recourse is an essential aspect of procedural fairness in consumer-facing applications of machine learning. When a consumer is denied a loan by a machine learning model, for example, they should be able to change the input variables of the model in a way that guarantees approval. Otherwise, this person will be denied the loan so long as the model is deployed, and stripped of the ability to influence a decision that affects their livelihood.

Verification & Reporting

This library provides protect consumers against this harm through verification and reporting. These tools can be used to answer questions like:

  • What can a person do to obtain a favorable prediction from a given model?
  • How many people can change their prediction?
  • How difficult for people to change their prediction?

Specific functionality includes:

  • Customize the set of feasible action for each input variable of an machine learning model.
  • Produce a list of actionable changes for a person to flip the prediction of a model.
  • Estimate the feasibility of recourse of a model on a population of interest.
  • Evaluate the difficulty of recourse of a model on a population of interest.

The tools are currently designed to support linear classification models, and will be extended to cover other kinds of models over time.


Installation

You can install the library via pip.

$ pip install actionable-recourse

Requirements

  • Python 3
  • Python-MIP or CPLEX

CPLEX

CPLEX is fast optimization solver with a Python API. It is commercial software, but free to download for students and faculty at accredited institutions. To obtain CPLEX:

  1. Register for IBM Academic Initiative
  2. Download the IBM ILOG CPLEX Optimization Studio from the software catalog
  3. Install the CPLEX Optimization Studio (on MacOS, run ./<cplex-path>/Contents/MacOS).
  4. Setup the CPLEX Python API as described here.

If you have problems installing CPLEX, please check the CPLEX user manual or the CPLEX forums.

Usage

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
import recourse as rs

# import data
url = 'https://raw.githubusercontent.com/ustunb/actionable-recourse/master/examples/paper/data/credit_processed.csv'
df = pd.read_csv(url)
y, X = df.iloc[:, 0], df.iloc[:, 1:]

# train a classifier
clf = LogisticRegression(max_iter = 1000)
clf.fit(X, y)
yhat = clf.predict(X)

# customize the set of actions
A = rs.ActionSet(X)  ## matrix of features. ActionSet will set bounds and step sizes by default

# specify immutable variables
A['Married'].mutable = False

# can only specify properties for multiple variables using a list
A[['Age_lt_25', 'Age_in_25_to_40', 'Age_in_40_to_59', 'Age_geq_60']].mutable = False

# education level
A['EducationLevel'].step_direction = 1  ## force conditional immutability.
A['EducationLevel'].step_size = 1  ## set step-size to a custom value.
A['EducationLevel'].step_type = "absolute"  ## force conditional immutability.
A['EducationLevel'].bounds = (0, 3)

A['TotalMonthsOverdue'].step_size = 1  ## set step-size to a custom value.
A['TotalMonthsOverdue'].step_type = "absolute"  ## discretize on absolute values of feature rather than percentile values
A['TotalMonthsOverdue'].bounds = (0, 100)  ## set bounds to a custom value.

## get model coefficients and align
A.align(clf)  ## tells `ActionSet` which directions each feature should move in to produce positive change.

# Get one individual
i = np.flatnonzero(yhat <= 0).astype(int)[0]

# build a flipset for one individual
fs = rs.Flipset(x = X.values[i], action_set = A, clf = clf)
fs.populate(enumeration_type = 'distinct_subsets', total_items = 10)
fs.to_latex()
fs.to_html()

# Run Recourse Audit on Training Data
auditor = rs.RecourseAuditor(A, coefficients = clf.coef_, intercept = clf.intercept_)
audit_df = auditor.audit(X)  ## matrix of features over which we will perform the audit.

## print mean feasibility and cost of recourse
print(audit_df['feasible'].mean())
print(audit_df['cost'].mean())
print_recourse_audit_report(X, audit_df, y)
# or produce additional information of cost of recourse by other variables
# print_recourse_audit_report(X, audit_df, y, group_by = ['y', 'Married', 'EducationLevel'])

Contributing

We're actively working to improve this package and make it more useful. If you come across bugs, have comments, or want to help, let us know. We welcome any and all contributions! For more info on how to contribute, check out these guidelines. Thank you community!

Resources

For more about recourse, check out our paper:

Actionable Recourse in Linear Classification

If you use this library in your research, we would appreciate a citation!

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

actionable-recourse-1.0.1.tar.gz (38.6 kB view details)

Uploaded Source

Built Distribution

actionable_recourse-1.0.1-py3-none-any.whl (46.4 kB view details)

Uploaded Python 3

File details

Details for the file actionable-recourse-1.0.1.tar.gz.

File metadata

  • Download URL: actionable-recourse-1.0.1.tar.gz
  • Upload date:
  • Size: 38.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.6

File hashes

Hashes for actionable-recourse-1.0.1.tar.gz
Algorithm Hash digest
SHA256 01c529ed9d0286c2d2dee54327efa8d5d06e56a2c40b4f14fed9f9c61002cc4c
MD5 202667c67d684a25e13b3751992c4f7a
BLAKE2b-256 173ccf94d1cdc652b4d68a6abe8be2abad4f6608ef6834afbeda91150a72528b

See more details on using hashes here.

File details

Details for the file actionable_recourse-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: actionable_recourse-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 46.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.6

File hashes

Hashes for actionable_recourse-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 80086d1b1312428756f0750b36fdc2a1d005d5b3719a8a4c33f1b792c9699def
MD5 5668202fe92180dfa29432b39e6c90ea
BLAKE2b-256 79977df47186ec58f13981aa4e89e8fdfd81c907c73112f42cafcb1d685d9b00

See more details on using hashes here.

Supported by

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