Skip to content

Commit

Permalink
sklearn counterfactuals: Add option 'auto' for automatically choosing…
Browse files Browse the repository at this point in the history
… a suitable optimization method - 'auto' is now the default in all high level API methods
  • Loading branch information
andreArtelt committed May 7, 2020
1 parent 988840b commit bc6c0b4
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ceml/sklearn/isolationforest.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,7 @@ def isolationforest_generate_counterfactual(model, x, y_target, features_whiteli
"""
cf = IsolationForestCounterfactual(model)

if optimizer == "auto":
optimizer = "nelder-mead"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict)
3 changes: 3 additions & 0 deletions ceml/sklearn/knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,7 @@ def knn_generate_counterfactual(model, x, y_target, features_whitelist=None, dis
"""
cf = KnnCounterfactual(model, dist)

if optimizer == "auto":
optimizer = "nelder-mead"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict, done)
3 changes: 3 additions & 0 deletions ceml/sklearn/lda.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,7 @@ def lda_generate_counterfactual(model, x, y_target, features_whitelist=None, reg
"""
cf = LdaCounterfactual(model)

if optimizer == "auto":
optimizer = "mp"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict, done)
3 changes: 3 additions & 0 deletions ceml/sklearn/linearregression.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,7 @@ def linearregression_generate_counterfactual(model, x, y_target, features_whitel
"""
cf = LinearRegressionCounterfactual(model)

if optimizer == "auto":
optimizer = "mp"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict, done)
6 changes: 4 additions & 2 deletions ceml/sklearn/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .pipeline import pipeline_generate_counterfactual


def generate_counterfactual(model, x, y_target, features_whitelist=None, dist="l2", regularization="l1", C=1.0, optimizer="nelder-mead", return_as_dict=True, done=None):
def generate_counterfactual(model, x, y_target, features_whitelist=None, dist="l2", regularization="l1", C=1.0, optimizer="auto", return_as_dict=True, done=None):
"""Computes a counterfactual of a given input `x`.
Parameters
Expand Down Expand Up @@ -72,9 +72,11 @@ def generate_counterfactual(model, x, y_target, features_whitelist=None, dist="l
Name/Identifier of the optimizer that is used for computing the counterfactual.
See :func:`ceml.optimizer.optimizer.desc_to_optim` for details.
Use "auto" if you do not know what optimizer to use - a suitable optimizer is chosen automatically.
As an alternative, we can use any (custom) optimizer that is derived from the :class:`ceml.optim.optimizer.Optimizer` class.
The default is "nelder-mead".
The default is "auto".
return_as_dict : `boolean`, optional
If True, returns the counterfactual, its prediction and the needed changes to the input as dictionary.
If False, the results are returned as a triple.
Expand Down
5 changes: 5 additions & 0 deletions ceml/sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ def pipeline_generate_counterfactual(model, x, y_target, features_whitelist=None
Name/Identifier of the optimizer that is used for computing the counterfactual.
See :func:`ceml.optim.optimizer.prepare_optim` for details.
Use "auto" if you do not know what optimizer to use - a suitable optimizer is chosen automatically.
As an alternative, we can use any (custom) optimizer that is derived from the :class:`ceml.optim.optimizer.Optimizer` class.
The default is "nelder-mead".
Expand Down Expand Up @@ -284,4 +286,7 @@ def pipeline_generate_counterfactual(model, x, y_target, features_whitelist=None
"""
cf = PipelineCounterfactual(model)

if optimizer == "auto":
optimizer = "nelder-mead"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict, done)
3 changes: 3 additions & 0 deletions ceml/sklearn/randomforest.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,7 @@ def randomforest_generate_counterfactual(model, x, y_target, features_whitelist=
"""
cf = RandomForestCounterfactual(model)

if optimizer == "auto":
optimizer = "nelder-mead"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict)
3 changes: 3 additions & 0 deletions ceml/sklearn/softmaxregression.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,7 @@ def softmaxregression_generate_counterfactual(model, x, y_target, features_white
"""
cf = SoftmaxCounterfactual(model)

if optimizer == "auto":
optimizer = "mp"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict, done)

0 comments on commit bc6c0b4

Please sign in to comment.