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
  • Loading branch information
andreArtelt committed May 7, 2020
1 parent 43e7490 commit 988840b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ceml/sklearn/counterfactual.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def compute_counterfactual(self, x, y_target, features_whitelist=None, regulariz
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 sutiable optimizer is chosen automatically.
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.
Expand Down
8 changes: 4 additions & 4 deletions ceml/sklearn/lda.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def solve(self, x_orig, y_target, regularization, features_whitelist, return_as_
return self.__build_result_dict(xcf, y_target, delta) if return_as_dict else xcf, y_target, delta


def lda_generate_counterfactual(model, x, y_target, features_whitelist=None, regularization="l1", C=1.0, optimizer="nelder-mead", return_as_dict=True, done=None):
def lda_generate_counterfactual(model, x, y_target, features_whitelist=None, regularization="l1", C=1.0, optimizer="mp", return_as_dict=True, done=None):
"""Computes a counterfactual of a given input `x`.
Parameters
Expand Down Expand Up @@ -195,11 +195,11 @@ def lda_generate_counterfactual(model, x, y_target, features_whitelist=None, reg
Name/Identifier of the optimizer that is used for computing the counterfactual.
See :func:`ceml.optim.optimizer.prepare_optim` for details.
As an alternative, we can use any (custom) optimizer that is derived from the :class:`ceml.optim.optimizer.Optimizer` class.
Linear discriminant analysis supports the use of mathematical programs for computing counterfactuals - set `optimizer` to "mp" for using a convex quadratic program for computing the counterfactual. Note that in this case the hyperparameter `C` is ignored.
The default is "nelder-mead".
As an alternative, we can use any (custom) optimizer that is derived from the :class:`ceml.optim.optimizer.Optimizer` class.
Linear discriminant analysis supports the use of mathematical programs for computing counterfactuals - set `optimizer` to "mp" for using a convex quadratic program for computing the counterfactual. Note that in this case the hyperparameter `C` is ignored.
The default is "mp".
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
8 changes: 4 additions & 4 deletions ceml/sklearn/linearregression.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def solve(self, x_orig, y_target, regularization, features_whitelist, return_as_
return self.__build_result_dict(xcf, y_target, delta) if return_as_dict else xcf, y_cf, delta


def linearregression_generate_counterfactual(model, x, y_target, features_whitelist=None, regularization="l1", C=1.0, optimizer="nelder-mead", return_as_dict=True, done=None):
def linearregression_generate_counterfactual(model, x, y_target, features_whitelist=None, regularization="l1", C=1.0, optimizer="mp", return_as_dict=True, done=None):
"""Computes a counterfactual of a given input `x`.
Parameters
Expand Down Expand Up @@ -170,11 +170,11 @@ def linearregression_generate_counterfactual(model, x, y_target, features_whitel
Name/Identifier of the optimizer that is used for computing the counterfactual.
See :func:`ceml.optim.optimizer.prepare_optim` for details.
As an alternative, we can use any (custom) optimizer that is derived from the :class:`ceml.optim.optimizer.Optimizer` class.
Linear regression supports the use of mathematical programs for computing counterfactuals - set `optimizer` to "mp" for using a convex quadratic program for computing the counterfactual. Note that in this case the hyperparameter `C` is ignored.
The default is "nelder-mead".
As an alternative, we can use any (custom) optimizer that is derived from the :class:`ceml.optim.optimizer.Optimizer` class.
Linear regression supports the use of mathematical programs for computing counterfactuals - set `optimizer` to "mp" for using a convex quadratic program for computing the counterfactual. Note that in this case the hyperparameter `C` is ignored.
The default is "mp".
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
12 changes: 10 additions & 2 deletions ceml/sklearn/lvq.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def solve(self, x_orig, y_target, regularization, features_whitelist, return_as_
return self.__build_result_dict(xcf, y_target, delta) if return_as_dict else xcf, y_target, delta


def lvq_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 lvq_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 @@ -393,7 +393,9 @@ def lvq_generate_counterfactual(model, x, y_target, features_whitelist=None, dis
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".
Use "auto" if you do not know what optimizer to use - a suitable optimizer is chosen automatically.
The default is "auto".
Learning vector quantization supports the use of mathematical programs for computing counterfactuals - set `optimizer` to "mp" for using a convex quadratic program (G(M)LVQ) or a DCQP (otherwise) for computing the counterfactual.
Note that in this case the hyperparameter `C` is ignored.
Expand All @@ -420,4 +422,10 @@ def lvq_generate_counterfactual(model, x, y_target, features_whitelist=None, dis
"""
cf = LvqCounterfactual(model, dist)

if optimizer == "auto": # Choose a suitable optimizer
if not(isinstance(model, sklearn_lvq.LgmlvqModel) or isinstance(model, sklearn_lvq.LmrslvqModel)):
optimizer = "mp"
else:
optimizer = "nelder-mead"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict)
12 changes: 10 additions & 2 deletions ceml/sklearn/naivebayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def solve(self, x_orig, y_target, regularization, features_whitelist, return_as_
return self.__build_result_dict(xcf, y_target, delta) if return_as_dict else xcf, y_target, delta


def gaussiannb_generate_counterfactual(model, x, y_target, features_whitelist=None, regularization="l1", C=1.0, optimizer="nelder-mead", return_as_dict=True, done=None):
def gaussiannb_generate_counterfactual(model, x, y_target, features_whitelist=None, 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 @@ -211,7 +211,9 @@ def gaussiannb_generate_counterfactual(model, x, y_target, features_whitelist=No
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".
Use "auto" if you do not know what optimizer to use - a suitable optimizer is chosen automatically.
The default is "auto".
Gaussian naive Bayes supports the use of mathematical programs for computing counterfactuals - set `optimizer` to "mp" for using a semi-definite program (binary classifier) or a DCQP (otherwise) for computing the counterfactual.
Note that in this case the hyperparameter `C` is ignored.
Expand All @@ -238,4 +240,10 @@ def gaussiannb_generate_counterfactual(model, x, y_target, features_whitelist=No
"""
cf = GaussianNbCounterfactual(model)

if optimizer == "auto":
if cf.mymodel.is_binary:
optimizer = "mp"
else:
optimizer = "nelder-mead"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict, done)
8 changes: 7 additions & 1 deletion ceml/sklearn/qda.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def solve(self, x_orig, y_target, regularization, features_whitelist, return_as_
return self.__build_result_dict(xcf, y_target, delta) if return_as_dict else xcf, y_target, delta


def qda_generate_counterfactual(model, x, y_target, features_whitelist=None, regularization="l1", C=1.0, optimizer="nelder-mead", return_as_dict=True, done=None):
def qda_generate_counterfactual(model, x, y_target, features_whitelist=None, 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 @@ -241,4 +241,10 @@ def qda_generate_counterfactual(model, x, y_target, features_whitelist=None, reg
"""
cf = QdaCounterfactual(model)

if optimizer == "auto":
if cf.mymodel.is_binary:
optimizer = "mp"
else:
optimizer = "nelder-mead"

return cf.compute_counterfactual(x, y_target, features_whitelist, regularization, C, optimizer, return_as_dict, done)
8 changes: 4 additions & 4 deletions ceml/sklearn/softmaxregression.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def solve(self, x_orig, y_target, regularization, features_whitelist, return_as_
return self.__build_result_dict(xcf, y_target, delta) if return_as_dict else xcf, y_target, delta


def softmaxregression_generate_counterfactual(model, x, y_target, features_whitelist=None, regularization="l1", C=1.0, optimizer="nelder-mead", return_as_dict=True, done=None):
def softmaxregression_generate_counterfactual(model, x, y_target, features_whitelist=None, regularization="l1", C=1.0, optimizer="mp", return_as_dict=True, done=None):
"""Computes a counterfactual of a given input `x`.
Parameters
Expand Down Expand Up @@ -202,11 +202,11 @@ def softmaxregression_generate_counterfactual(model, x, y_target, features_white
Name/Identifier of the optimizer that is used for computing the counterfactual.
See :func:`ceml.optim.optimizer.prepare_optim` for details.
As an alternative, we can use any (custom) optimizer that is derived from the :class:`ceml.optim.optimizer.Optimizer` class.
Softmax regression supports the use of mathematical programs for computing counterfactuals - set `optimizer` to "mp" for using a convex quadratic program for computing the counterfactual. Note that in this case the hyperparameter `C` is ignored.
The default is "nelder-mead".
As an alternative, we can use any (custom) optimizer that is derived from the :class:`ceml.optim.optimizer.Optimizer` class.
Softmax regression supports the use of mathematical programs for computing counterfactuals - set `optimizer` to "mp" for using a convex quadratic program for computing the counterfactual. Note that in this case the hyperparameter `C` is ignored.
The default is "mp".
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

0 comments on commit 988840b

Please sign in to comment.