How to use the alibi.explainers.anchor_base.AnchorBaseBeam.anchor_beam function in alibi

To help you get started, we’ve selected a few alibi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SeldonIO / alibi / alibi / explainers / anchor_tabular.py View on Github external
max_anchor_size
            Maximum number of features in anchor
        desired_label
            Label to use as true label for the instance to be explained

        Returns
        -------
        explanation
            Dictionary containing the anchor explaining the instance with additional metadata
        """
        # build sampling function and ...
        # ... mapping = (feature column, flag for categorical/ordinal feature, feature value or bin value)
        sample_fn, mapping = self.get_sample_fn(X, desired_label=desired_label)

        # get anchors and add metadata
        exp = AnchorBaseBeam.anchor_beam(sample_fn, delta=delta, epsilon=tau,
                                         batch_size=batch_size, desired_confidence=threshold,
                                         max_anchor_size=max_anchor_size, **kwargs)  # type: Any
        self.add_names_to_exp(exp, mapping)
        exp['instance'] = X
        exp['prediction'] = self.predict_fn(X.reshape(1, -1))[0]
        exp = AnchorExplanation('tabular', exp)

        # output explanation dictionary
        explanation = {}
        explanation['names'] = exp.names()
        explanation['precision'] = exp.precision()
        explanation['coverage'] = exp.coverage()
        explanation['raw'] = exp.exp_map

        explanation['meta'] = {}
        explanation['meta']['name'] = self.__class__.__name__
github SeldonIO / alibi / alibi / explainers / anchor_text.py View on Github external
# needed to set dtype of array later and ensure the full text is used
        total_len = 0
        for word in words:
            if use_unk:
                max_len = max(3, len(word))  # len('UNK') = 3
            else:
                self.neighbors.neighbors(word)
                similar_words = self.neighbors.n[word]
                max_len = 0
                for similar_word in similar_words:
                    max_len = max(max_len, len(similar_word[0]))
            total_len += max_len + 1
        data_type = '
github SeldonIO / alibi / alibi / explainers / anchor_image.py View on Github external
Margin between lower confidence bound and minimum precision of upper bound
        batch_size
            Batch size used for sampling
        p_sample
            Probability for a pixel to be represented by the average value of its superpixel

        Returns
        -------
        explanation
            Dictionary containing the anchor explaining the instance with additional metadata
        """
        # build sampling function and segments
        segments, sample_fn = self.get_sample_fn(np.reshape(image, self.image_shape), p_sample=p_sample)

        # get anchors and add metadata
        exp = AnchorBaseBeam.anchor_beam(sample_fn, delta=delta,
                                         epsilon=tau, batch_size=batch_size,
                                         desired_confidence=threshold, **kwargs)  # type: Any
        exp['instance'] = image
        exp['prediction'] = self.predict_fn(np.reshape(image, (1,) + self.image_shape))[0]

        # overlay image with anchor mask and do same for the examples
        anchor = AnchorImage.overlay_mask(image, segments, exp['feature'])
        cover_options = ['covered', 'covered_true', 'covered_false', 'uncovered_true', 'uncovered_false']
        for ex in exp['examples']:
            for opt in cover_options:
                tmp = [AnchorImage.overlay_mask(image, segments, list(np.where(ex[opt][i] == 1)[0]))
                       for i in range(ex[opt].shape[0])]
                ex[opt] = tmp

        exp = AnchorExplanation('image', exp)