class_docstr: Computes SHAP values for generalized additive models.
  
  This assumes that the model only has first-order effects. Extending this to
  second- and third-order effects is future work (if you apply this to those models right now
  you will get incorrect answers that fail additivity).
docstr: Build an Additive explainer for the given model using the given masker object.
  
  Parameters
  ----------
  model : function
      A callable python object that executes the model given a set of input data samples.
  
  masker : function or numpy.array or pandas.DataFrame
      A callable python object used to "mask" out hidden features of the form `masker(mask, *fargs)`.
      It takes a single a binary mask and an input sample and returns a matrix of masked samples. These
      masked samples are evaluated using the model function and the outputs are then averaged.
      As a shortcut for the standard masking used by SHAP you can pass a background data matrix
      instead of a function and that matrix will be used for masking. To use a clustering
      game structure you can pass a shap.maskers.Tabular(data, hclustering="correlation") object, but
      note that this structure information has no effect on the explanations of additive models.
docstr_tok: ["Build","an","Additive","explainer","for","the","given","model","using","the","given","masker","object",".","Parameters","--","--","--","--","--","model",":","function","A","callable","python","object","that","executes","the","model","given","a","set","of","input","data","samples",".","masker",":","function","or","numpy.array","or","pandas.DataFrame","A","callable","python","object","used","to","``","mask","''","out","hidden","features","of","the","form","`","masker","(","mask",",","*","fargs",")","`",".","It","takes","a","single","a","binary","mask","and","an","input","sample","and","returns","a","matrix","of","masked","samples",".","These","masked","samples","are","evaluated","using","the","model","function","and","the","outputs","are","then","averaged",".","As","a","shortcut","for","the","standard","masking","used","by","SHAP","you","can","pass","a","background","data","matrix","instead","of","a","function","and","that","matrix","will","be","used","for","masking",".","To","use","a","clustering","game","structure","you","can","pass","a","shap.maskers.Tabular","(","data",",","hclustering=","''","correlation","''",")","object",",","but","note","that","this","structure","information","has","no","effect","on","the","explanations","of","additive","models","."]
code: def __init__(
      self,
      model,
      masker,
      link=None,
      feature_names=None,
      linearize_link=True,
  ):
      """Build an Additive explainer for the given model using the given masker object.
  
      Parameters
      ----------
      model : function
          A callable python object that executes the model given a set of input data samples.
  
      masker : function or numpy.array or pandas.DataFrame
          A callable python object used to "mask" out hidden features of the form `masker(mask, *fargs)`.
          It takes a single a binary mask and an input sample and returns a matrix of masked samples. These
          masked samples are evaluated using the model function and the outputs are then averaged.
          As a shortcut for the standard masking used by SHAP you can pass a background data matrix
          instead of a function and that matrix will be used for masking. To use a clustering
          game structure you can pass a shap.maskers.Tabular(data, hclustering=\"correlation\") object, but
          note that this structure information has no effect on the explanations of additive models.
      """
      super().__init__(
          model,
          masker,
          feature_names=feature_names,
          linearize_link=linearize_link,
      )
  
      if safe_isinstance(
          model, "interpret.glassbox.ExplainableBoostingClassifier"
      ):
          self.model = model.decision_function
  
          if self.masker is None:
              self._expected_value = model.intercept_
              # num_features = len(model.additive_terms_)
  
              # fm = MaskedModel(self.model, self.masker, self.link, np.zeros(num_features))
              # masks = np.ones((1, num_features), dtype=bool)
              # outputs = fm(masks)
              # self.model(np.zeros(num_features))
              # self._zero_offset = self.model(np.zeros(num_features))#model.intercept_#outputs[0]
              # self._input_offsets = np.zeros(num_features) #* self._zero_offset
              raise NotImplementedError(
                  "Masker not given and we don't yet support pulling the distribution centering directly from the EBM model!"
              )
              return
  
      # here we need to compute the offsets ourselves because we can't pull them directly from a model we know about
      assert safe_isinstance(
          self.masker, "shap.maskers.Independent"
      ), "The Additive explainer only supports the Tabular masker at the moment!"
  
      # pre-compute per-feature offsets
      fm = MaskedModel(
          self.model,
          self.masker,
          self.link,
          self.linearize_link,
          np.zeros(self.masker.shape[1]),
      )
      masks = np.ones(
          (self.masker.shape[1] + 1, self.masker.shape[1]), dtype=bool
      )
      for i in range(1, self.masker.shape[1] + 1):
          masks[i, i - 1] = False
      outputs = fm(masks)
      self._zero_offset = outputs[0]
      self._input_offsets = np.zeros(masker.shape[1])
      for i in range(1, self.masker.shape[1] + 1):
          self._input_offsets[i - 1] = outputs[i] - self._zero_offset
  
      self._expected_value = (
          self._input_offsets.sum() + self._zero_offset
      )
  
file_path: shap/shap/explainers/_additive.py
imports: import numpy
  from ..utils import MaskedModel, safe_isinstance
  from ._explainer import Explainer
label_desc: Use image node_id 1 to create a new AdditiveExplainer object from inherited base classes: Explainer with example: obj = AdditiveExplainer(model, masker, link, feature_names, linearize_link)