def mock_ks_explainer(request): """ Instantiates a KernelShap explainer with a mock predictor. """ pred_out_dim, link = request.param predictor = MockPredictor(out_dim=pred_out_dim, seed=0) explainer = KernelShap(predictor=predictor, seed=0) return explainer
def mock_ale_explainer(request): """ Instantiates an ALE explainer with a mock predictor. """ out_dim, out_type = request.param predictor = MockPredictor(out_dim=out_dim, out_type=out_type, seed=0) explainer = ALE(predictor) return explainer
def patch_transformer(monkeypatch, request): out_dim, out_type, sz = request.param transformer = ArgmaxTransformer(None) monkeypatch.setattr( transformer, "predictor", MockPredictor(out_dim, out_type=out_type, sz=sz, key=(out_dim, out_type, sz))) return transformer
def mock_kernel_shap_explainer(request): """ Instantiates a KernelShap explainer with a mock predictor. """ pred_out_dim, link, distributed_opts = request.param predictor = MockPredictor(out_dim=pred_out_dim, seed=0) explainer = KernelShap(predictor=predictor, link=link, distributed_opts=distributed_opts, seed=0) return explainer
def mock_tree_shap_explainer(monkeypatch, request): """ Instantiates a TreeShap explainer where both the `_explainer` and `model` atttributes are mocked Parameters ---------- request: Tuple containing number of outputs and model_output string (see TreeShap constructor). """ n_outs, model_output = request.param seed = 0 predictor = MockPredictor(out_dim=n_outs, out_type=model_output, seed=seed) tree_explainer = MockTreeExplainer(predictor, seed=seed) monkeypatch.setattr(shap, "TreeExplainer", tree_explainer) explainer = TreeShap(predictor, model_output=model_output) return explainer
def test_argmax_transformer(monkeypatch, out_dim, out_type): """ Test that the conversion of output probabilities to class labels works for a range of classifier output types. """ # setup a transformer with a mock predictor transformer = ArgmaxTransformer(None) monkeypatch.setattr( transformer, "predictor", MockPredictor( out_dim, out_type=out_type, ) ) # fake predictor input X = np.random.random(size=(50, 14)) result = transformer(X) # argmax transformer should do get rid of the feature dimension assert len(result.shape) == len(X.shape) - 1