コード例 #1
0
ファイル: smart_predictor.py プロジェクト: kokizzu/shapash
 def filter(self):
     """
     The filter method is an important method which allows to summarize the local explainability
     by using the user defined mask_params parameters which correspond to its use case.
     """
     mask = [init_mask(self.summary['contrib_sorted'], True)]
     if self.mask_params["features_to_hide"] is not None:
         mask.append(
             hide_contributions(self.summary['var_dict'],
                                features_list=self.check_features_name(
                                    self.mask_params["features_to_hide"])))
     if self.mask_params["threshold"] is not None:
         mask.append(
             cap_contributions(self.summary['contrib_sorted'],
                               threshold=self.mask_params["threshold"]))
     if self.mask_params["positive"] is not None:
         mask.append(
             sign_contributions(self.summary['contrib_sorted'],
                                positive=self.mask_params["positive"]))
     self.mask = combine_masks(mask)
     if self.mask_params["max_contrib"] is not None:
         self.mask = cutoff_contributions(mask=self.mask,
                                          k=self.mask_params["max_contrib"])
     self.masked_contributions = compute_masked_contributions(
         self.summary['contrib_sorted'], self.mask)
コード例 #2
0
 def test_cutoff_contributions_3(self):
     """
     Unit test cutoff contributions 3
     """
     dataframe = pd.DataFrame({
         'val1': [False, False, False],
         'val2': [False, False, True],
         'val3': [False, True, False],
         'val4': [False, False, True]
     })
     output = cutoff_contributions(dataframe)
     expected = pd.DataFrame({
         'val1': [False, False, False],
         'val2': [False, False, True],
         'val3': [False, True, False],
         'val4': [False, False, True]
     })
     pd.testing.assert_frame_equal(output, expected)
コード例 #3
0
    def cutoff_contributions(self, dataframe, max_contrib):
        """
        The function cutoff_contributions computes a mask on a sorted contribution matrix.
        It outputs True everywhere the contribution is in the top-k,
        k being defined as an option by the user.

        Parameters
        ----------
        dataframe : pd.Dataframe
            DataFrame is a sorted shapley matrix.
        max_contrib: int
            The k most important contributions to keep.

        Returns
        -------
        pd.Dataframe
            Mask indicating where contributions should be considered.
        """
        return cutoff_contributions(dataframe, max_contrib)