Counterfactual explanations

class optbinning.scorecard.Counterfactual(scorecard, special_missing=False, n_jobs=1, verbose=False)

Bases: optbinning.scorecard.counterfactual.base.BaseCounterfactual

Optimal counterfactual explanations given a scorecard model.

Parameters
  • scorecard (object) – A Scorecard instance.

  • special_missing (bool (default=False)) – Whether the special and missing bin are considered as valid counterfactual values.

  • n_jobs (int, optional (default=1)) – Number of cores to run the optimization solver.

  • verbose (bool (default=False)) – Enable verbose output.

display(show_only_changes=False, show_outcome=False)

Display the generatedcounterfactual explanations.

Parameters
  • show_only_changes (boolean (default=False)) – Whether to show only changes on feature values.

  • show_outcome (boolean (default=False)) – Whether to add a column with the scorecard outcome. If outcome_type is “binary” or “probability”, the estimated probability of the counterfactual is added.

Returns

counterfactuals – Counterfactual explanations.

Return type

pandas.DataFrame

fit(X)

Fit counterfactual. Compute problem data to generate counterfactual explanations.

Parameters

X (pandas.DataFrame (n_samples, n_features)) – Training vector, where n_samples is the number of samples.

Returns

self – Fitted counterfactual.

Return type

Counterfactual

generate(query, y, outcome_type, n_cf, method='weighted', objectives=None, max_changes=None, actionable_features=None, hard_constraints=None, soft_constraints=None, priority_tol=0.1, time_limit=10)

Generate counterfactual explanations given objectives and constraints.

Parameters
  • query (dict or pandas.DataFrame) – Input data points for which a single or multiple counterfactual explanations are to be generated.

  • y (int or float) – Desired outcome.

  • outcome_type (str) – Desired outcome type. Supported outcome types are “binary”, “probability” and “continuous”.

  • n_cf (int) – Number of counterfactuals to be generated.

  • method (str (default="weighted")) – Multi-objective optimization method. Supported methods are “weighted” and “hierarchical”.

  • objectives (dict or None (default=None)) – Objectives with their corresponding weights or priorities, depending on the method.

  • max_changes (int or None (default=None)) – Maximum number of features to be changed. If None, the maximum number of changes is half of the number of features.

  • actionable_features (array-like or None (default=None)) – List of actionable features. If None. all features are suitable to be changed.

  • hard_constraints (array-like or None (default=None)) – Constraint to be enforced when solving the underlying optimization problem.

  • soft_constraints (dict or None (default=None)) – Constraints to be moved to the objective function as a penalization term.

  • priority_tol (float, optional (default=0.1)) – Relative tolerance when solving the multi-objective optimization problem with method="hierarchical".

  • time_limit (int (default=10)) – The maximum time in seconds to run the optimization solver.

Returns

self – Generated counterfactuals.

Return type

Counterfactual

get_params(deep=True)

Get parameters for this estimator.

Parameters

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

dict

information(print_level=1)

Print overview information about the options settings and statistics.

Parameters

print_level (int (default=1)) – Level of details.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters

**params (dict) – Estimator parameters.

Returns

self – Estimator instance.

Return type

estimator instance

property status

The status of the underlying optimization solver.

Returns

status

Return type

str