Using RecourseBench#
This is the canonical, user-facing API. Import the package as rb and use the
named namespaces to construct components, or run() to
execute a whole experiment from a config.
import recourse_bench as rb
metrics = rb.run(config) # run an experiment from a config dict
model = rb.models.linear(seed=7) # construct components by name
method = rb.methods.wachter(target_model=model, seed=7, desired_class=1)
data = rb.datasets.credit()
Named namespaces#
Each registered component is available as an attribute of a namespace, under its registry name. The attribute is the component class — call it to construct an instance. The namespaces are populated dynamically from the registry, so they always reflect the currently registered components.
Namespace |
Base class |
Contains |
|---|---|---|
|
Tabular datasets, e.g. |
|
|
Pipeline steps, e.g. |
|
|
Target classifiers, e.g. |
|
|
Recourse methods, e.g. |
|
|
Evaluation metrics, e.g. |
Constructor arguments match the corresponding base class (see Extending the framework
for the full argument and method signatures). The lists below are a snapshot;
call rb.list_datasets(), rb.list_methods(), etc. for the authoritative,
up-to-date set. To iterate over components by name (e.g. for a sweep), use
getattr:
for name in ["wachter", "dice", "gs"]:
method = getattr(rb.methods, name)(target_model=model, seed=7)
Datasets — rb.datasets#
Available: adult, adult_cfrl, adult_cfvae, adult_cogs,
boston_housing, breast_cancer, compas, compas_carla,
compas_clue, credit, credit_cchvae, diabetes, german,
german_roar, german_sns, hepatitis, news_popularity,
synthetic_face, toydata (variants suffixed with a method name carry the
features/metadata that method expects).
Construct:
data = rb.datasets.credit()— no required arguments; the raw dataframe and feature metadata are loaded from the bundled offline data.Output: a
DatasetObjectin its mutable state. Preprocessing steps mutate it; once frozen the read interface isdata.get(target=False)(feature columns as aDataFrame),data.get(target=True)(the label column),len(data),data[idx], anddata.attr(name)for feature metadata (type, mutability, actionability).
Preprocessors — rb.preprocessors#
Available: balance, encode, finalize, reorder, scale,
split (a typical pipeline runs balance → encode → scale → split →
finalize).
Construct:
step = rb.preprocessors.scale(scaling="normalize")—seedplus step-specific options.Input → output:
step.transform(dataset)takes a mutableDatasetObjectand returns the transformed dataset, or a tuple of datasets for steps that split (split→ trainset, testset).
Models — rb.models#
Available: linear, mlp, mlp_bayesian, randomforest,
sklearn_logistic_regression.
Construct:
model = rb.models.linear(seed=7, device="cpu").Inputs → outputs:
model.fit(trainset)trains on a frozenDatasetObject;model.predict(testset)/model.predict_proba(testset)return a(n_rows, n_classes)torch.Tensorof logits / probabilities;model.get_prediction(X, proba=...)predicts on a featureDataFrame. Differentiable models also supportmodel(X)/model.forward(X)on a feature tensor.
Methods — rb.methods#
Available: apas, arg_ensembling, cchvae, cemsp, cfrl,
cfvae, claproar, clue, cogs, cols, cruds, cvas_proj,
dice, diverse_dist, face, feature_tweak, gravitational,
gs, larr, mace, probe, proplace, rbr, revise,
roar, sns, toy, trex, wachter.
Construct:
method = rb.methods.wachter(target_model=model, seed=7, desired_class=1)— wraps a (to-be-)trainedmodel;desired_classsteers which class counterfactuals move toward (Noneflips a binary label). Extra keyword arguments are method-specific hyperparameters.Inputs → outputs:
method.fit(trainset)builds any auxiliary search structures;method.get_counterfactuals(factuals)takes a featureDataFrameand returns one with the same rows and columns, withNaNrows where no valid counterfactual was found. The inheritedmethod.predict(testset)runs that in batches and returns a frozen counterfactualDatasetObjectcarrying runtime, prediction, and target-label metadata (failed rows haveNaNfeatures and target-1).
Evaluations — rb.evaluations#
Available: constraints, distance, examples, knn, runtime,
validity, ynn.
Construct:
metric = rb.evaluations.validity()— metric-specific options (e.g. a reference set or distance norm).Input → output:
metric.evaluate(factuals, counterfactuals)takes the finalized factual dataset and the counterfactual dataset frommethod.predictand returns a single-rowDataFrameof named metrics.Experimentconcatenates these column-wise into the final metrics table.
Run an experiment#
For full control — including access to the trained model, the generated
counterfactuals, and run provenance — use the Experiment
class directly.