actionable-recourse
is a python library to check recourse in linear classification models.
Recourse is the ability to flip the prediction of a ML model by changing actionable input variables (e.g., income
instead of age
).
Recourse is an important element of human-facing applications of ML.
In tasks such as lending, ML models should provide all individuals with an actionable way to change their prediction.
In other tasks, models should let individuals flip their predictions based on specific types of changes. A recidivism prediction model that includes age
, for example, should let a person who is predicted to recidivate with the ability to flip their prediction without having to alter age
.
The tools in this library let you check recourse without interfering in model development.
They can answer questions like:
- What can a person do to obtain a favorable outcome from a model?
- How many people can change their prediction?
- How difficult for people to change their prediction?
Functionality
- Specify a custom set of feasible actions for each input variable of an ML model.
- Generate actionable changes for a person to flip the prediction of a linear classifier.
- Determine the feasibility of recourse of a linear classifier on a population of interest.
- Evaluate the difficulty of recourse for a linear classifier on a population of interest.
import recourse as rs
import sys; sys.path.append('examples/paper')
from initialize import *
data, scaler = load_data()
# train a classifier
clf = LogisticRegression().fit(data['X_train'], data['y'])
yhat = clf.predict(X = data['X_train'])
# customize the set of actions
A = rs.ActionSet(X = data['X']) ## matrix of features. ActionSet will learn default bounds and step-size.
A['Age'].mutable = False ## forces "age" to be immutable
A['CriticalAccountOrLoansElsewhere'].step_direction = -1 ## force conditional immutability.
A['LoanDuration'].step_type ="absolute" ## discretize on absolute values of feature rather than percentile values
A['LoanDuration'].bounds = (1, 100) ## set bounds to a custom value.
A['LoanDuration'].step_size = 6 ## set step-size to a custom value.
## get model coefficients and align
w, b = undo_coefficient_scaling(clf, scaler = data['scaler'])
action_set.align(w) ## tells `ActionSet` which directions each feature should move in to produce positive change.
# Get one individual
predicted_neg = np.flatnonzero(yhat < 1)
U = data['X'].iloc[predicted_neg].values
# build a flipset for one individual
fs = rs.Flipset(x = U[0], action_set = A, coefficients = w, intercept = b)
fs.populate(enumeration_type = 'distinct_subsets', total_items = 10)
fs.to_latex()
fs.to_html()
# Run Recourse Audit on Training Data
auditor = rs.RecourseAuditor(action_set, coefficients = w, intercept = b)
audit_df = auditor.audit(X = data['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())
The latest release can be installed via pip by running:
$ pip install actionable-recourse
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:
- Register for IBM OnTheHub
- Download the IBM ILOG CPLEX Optimization Studio from the software catalog
- Install the CPLEX Optimization Studio.
- Setup the CPLEX Python API as described here.
If you have problems installing CPLEX, please check the CPLEX user manual or the CPLEX forums.
If you do not want to use CPLEX, you can also work with an open-source solver. In this case, complete the following steps before you install the library:
- Download and install CBC from Bintray
- Download and install
pyomo
andpyomo-extras
(instructions)
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!
- support for categorical variables in
ActionSet
- support for rule-based models such as decision lists and rule lists
- scikit-learn compatability
- integration into AI360 Fairness toolkit
For more about recourse and these tools, check out our paper:
Actionable Recourse in Linear Classification
inproceedings{ustun2019recourse,
title = {Actionable Recourse in Linear Classification},
author = {Ustun, Berk and Spangher, Alexander and Liu, Yang},
booktitle = {Proceedings of the Conference on Fairness, Accountability, and Transparency},
series = {FAT* '19},
year = {2019},
isbn = {978-1-4503-6125-5},
location = {Atlanta, GA, USA},
pages = {10--19},
numpages = {10},
url = {http://doi.acm.org/10.1145/3287560.3287566},
doi = {10.1145/3287560.3287566},
publisher = {ACM},
}