Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions mlxtend/evaluate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@

__all__ = [
"scoring",
"accuracy_score",
"confusion_matrix",
"mcnemar",
"mcnemar_table",
"mcnemar_tables",
"mcnemar",
"lift_score",
"bootstrap",
"permutation_test",
"BootstrapOutOfBag",
"bootstrap_point632_score",
"cochrans_q",
"paired_ttest_resampled",
"paired_ttest_kfold_cv",
"BootstrapOutOfBag",
"permutation_test",
"combined_ftest_5x2cv",
"ftest",
"paired_ttest_5x2cv",
"paired_ttest_kfold_cv",
"paired_ttest_resampled",
"bias_variance_decomp",
"feature_importance_permutation",
"cochrans_q",
"GroupTimeSeriesSplit",
"create_counterfactual",
"RandomHoldoutSplit",
"PredefinedHoldoutSplit",
"ftest",
"combined_ftest_5x2cv",
"proportion_difference",
"bias_variance_decomp",
"accuracy_score",
"create_counterfactual",
"GroupTimeSeriesSplit",
]
48 changes: 31 additions & 17 deletions mlxtend/evaluate/counterfactual.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# License: BSD 3 clause


import warnings

import numpy as np
Expand All @@ -17,8 +18,10 @@ def create_counterfactual(
model,
X_dataset,
y_desired_proba=None,
lammbda=0.1,
lammbda=10,
random_seed=None,
fixed_features=None,
method="Nelder-Mead",
):
"""
Implementation of the counterfactual method by Wachter et al. 2017
Expand All @@ -27,8 +30,8 @@ def create_counterfactual(

- Wachter, S., Mittelstadt, B., & Russell, C. (2017).
Counterfactual explanations without opening the black box:
Automated decisions and the GDPR. Harv. JL & Tech., 31, 841.,
https://arxiv.org/abs/1711.00399
Automated decisions and the GDPR. Harv. JL & Tech., 31, 841.,
https://arxiv.org/abs/1711.00399

Parameters
----------
Expand Down Expand Up @@ -79,42 +82,53 @@ class probability for `y_desired`.
)
else:
use_proba = False

if y_desired_proba is None:
# class label

y_to_be_annealed_to = y_desired
else:
# class proba corresponding to class label y_desired
y_to_be_annealed_to = y_desired_proba

# start with random counterfactual
y_to_be_annealed_to = y_desired_proba
all_indices = np.arange(x_reference.shape[0])
if fixed_features is not None:
varying_indices = np.array([i for i in all_indices if i not in fixed_features])
else:
varying_indices = all_indices
rng = np.random.RandomState(random_seed)
x_counterfact = X_dataset[rng.randint(X_dataset.shape[0])]
initial_x = X_dataset[rng.randint(X_dataset.shape[0])].copy()

if fixed_features is not None:
initial_x[list(fixed_features)] = x_reference[list(fixed_features)]
x0 = initial_x[varying_indices]

# compute median absolute deviation

mad = np.abs(np.median(X_dataset, axis=0) - x_reference)

def dist(x_reference, x_counterfact):
numerator = np.abs(x_reference - x_counterfact)
return np.sum(numerator / mad)
return np.sum(numerator / (mad + 1e-8))

def loss(varying_values, lammbda):
current_x = x_reference.copy()
current_x[varying_indices] = varying_values

def loss(x_counterfact, lammbda):
if use_proba:
y_predict = model.predict_proba(x_counterfact.reshape(1, -1)).flatten()[
y_predict = model.predict_proba(current_x.reshape(1, -1)).flatten()[
y_desired
]
else:
y_predict = model.predict(x_counterfact.reshape(1, -1))

y_predict = model.predict(current_x.reshape(1, -1))
diff = lammbda * (y_predict - y_to_be_annealed_to) ** 2

return diff + dist(x_reference, x_counterfact)
return diff + dist(x_reference, current_x)

res = minimize(loss, x_counterfact, args=(lammbda), method="Nelder-Mead")
res = minimize(loss, x0, args=(lammbda,), method=method)

if not res["success"]:
warnings.warn(res["message"])
final_counterfactual = x_reference.copy()
final_counterfactual[varying_indices] = res["x"]

x_counterfact = res["x"]

return x_counterfact
return final_counterfactual
37 changes: 34 additions & 3 deletions mlxtend/evaluate/tests/test_counterfactual.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# License: BSD 3 clause


import numpy as np
from sklearn.linear_model import LogisticRegression

Expand Down Expand Up @@ -32,9 +33,6 @@ def test__medium_lambda():

assert np.argmax(clf.predict_proba(x_ref.reshape(1, -1))) == 0
assert np.argmax(clf.predict_proba(res.reshape(1, -1))) == 2
assert (
round((clf.predict_proba(0.65 >= res.reshape(1, -1))).flatten()[-1], 2) <= 0.69
)


def test__small_lambda():
Expand Down Expand Up @@ -118,3 +116,36 @@ def test__clf_with_no_proba_pass():

assert clf.predict(x_ref.reshape(1, -1)) == 0
assert clf.predict(res.reshape(1, -1)) == 2


def test_fixed_features():
X, y = iris_data()
clf = LogisticRegression(max_iter=1000)
clf.fit(X, y)
x_ref = X[15]
res = create_counterfactual(
x_reference=x_ref,
y_desired=2,
model=clf,
X_dataset=X,
fixed_features=[0, 1],
random_seed=123,
)
assert np.isclose(res[0], x_ref[0])
assert np.isclose(res[1], x_ref[1])


def test_different_methods():
X, y = iris_data()
clf = LogisticRegression(max_iter=1000)
clf.fit(X, y)
x_ref = X[15]
res = create_counterfactual(
x_reference=x_ref,
y_desired=2,
model=clf,
X_dataset=X,
method="BFGS",
random_seed=123,
)
assert np.argmax(clf.predict_proba(res.reshape(1, -1))) == 2
Loading