class_docstr: Computes SHAP values via an optimized exact enumeration.
  
  This works well for standard Shapley value maskers for models with less than ~15 features that vary
  from the background per sample. It also works well for Owen values from hclustering structured
  maskers when there are less than ~100 features that vary from the background per sample. This
  explainer minimizes the number of function evaluations needed by ordering the masking sets to
  minimize sequential differences. This is done using gray codes for standard Shapley values
  and a greedy sorting method for hclustering structured maskers.
class_docstr_tok: ["Computes","SHAP","values","via","an","optimized","exact","enumeration",".","This","works","well","for","standard","Shapley","value","maskers","for","models","with","less","than","~15","features","that","vary","from","the","background","per","sample",".","It","also","works","well","for","Owen","values","from","hclustering","structured","maskers","when","there","are","less","than","~100","features","that","vary","from","the","background","per","sample",".","This","explainer","minimizes","the","number","of","function","evaluations","needed","by","ordering","the","masking","sets","to","minimize","sequential","differences",".","This","is","done","using","gray","codes","for","standard","Shapley","values","and","a","greedy","sorting","method","for","hclustering","structured","maskers","."]
docstr: Build an explainers.Exact object 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.TabularPartitions(data) object.
  
  link : function
      The link function used to map between the output units of the model and the SHAP value units. By
      default it is shap.links.identity, but shap.links.logit can be useful so that expectations are
      computed in probability units while explanations remain in the (more naturally additive) log-odds
      units. For more details on how link functions work see any overview of link functions for generalized
      linear models.
  
  linearize_link : bool
      If we use a non-linear link function to take expectations then models that are additive with respect to that
      link function for a single background sample will no longer be additive when using a background masker with
      many samples. This for example means that a linear logistic regression model would have interaction effects
      that arise from the non-linear changes in expectation averaging. To retain the additively of the model with
      still respecting the link function we linearize the link function by default.
docstr_tok: ["Build","an","explainers.Exact","object","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.TabularPartitions","(","data",")","object",".","link",":","function","The","link","function","used","to","map","between","the","output","units","of","the","model","and","the","SHAP","value","units",".","By","default","it","is","shap.links.identity",",","but","shap.links.logit","can","be","useful","so","that","expectations","are","computed","in","probability","units","while","explanations","remain","in","the","(","more","naturally","additive",")","log-odds","units",".","For","more","details","on","how","link","functions","work","see","any","overview","of","link","functions","for","generalized","linear","models",".","linearize_link",":","bool","If","we","use","a","non-linear","link","function","to","take","expectations","then","models","that","are","additive","with","respect","to","that","link","function","for","a","single","background","sample","will","no","longer","be","additive","when","using","a","background","masker","with","many","samples",".","This","for","example","means","that","a","linear","logistic","regression","model","would","have","interaction","effects","that","arise","from","the","non-linear","changes","in","expectation","averaging",".","To","retain","the","additively","of","the","model","with","still","respecting","the","link","function","we","linearize","the","link","function","by","default","."]
code: def __init__(
      self,
      model,
      masker,
      link=links.identity,
      linearize_link=True,
      feature_names=None,
  ):
      """Build an explainers.Exact object 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.TabularPartitions(data) object.
  
      link : function
          The link function used to map between the output units of the model and the SHAP value units. By
          default it is shap.links.identity, but shap.links.logit can be useful so that expectations are
          computed in probability units while explanations remain in the (more naturally additive) log-odds
          units. For more details on how link functions work see any overview of link functions for generalized
          linear models.
  
      linearize_link : bool
          If we use a non-linear link function to take expectations then models that are additive with respect to that
          link function for a single background sample will no longer be additive when using a background masker with
          many samples. This for example means that a linear logistic regression model would have interaction effects
          that arise from the non-linear changes in expectation averaging. To retain the additively of the model with
          still respecting the link function we linearize the link function by default.
      """  # TODO link to the link linearization paper when done
      super().__init__(
          model,
          masker,
          link=link,
          linearize_link=linearize_link,
          feature_names=feature_names,
      )
  
      self.model = Model(model)
  
      if getattr(masker, "clustering", None) is not None:
          (
              self._partition_masks,
              self._partition_masks_inds,
          ) = partition_masks(masker.clustering)
          self._partition_delta_indexes = partition_delta_indexes(
              masker.clustering, self._partition_masks
          )
  
      self._gray_code_cache = {}
  
file_path: shap/shap/explainers/_exact.py
imports: import logging
  import numpy
  from numba import njit
  from ..None import links
  from ..models import Model
  from ..utils import MaskedModel, delta_minimization_order, make_masks, shapley_coefficients
  from ._explainer import Explainer
label_desc: Use image node_id 1 to create a new ExactExplainer object from inherited base classes: Explainer with example: obj = ExactExplainer(model, masker, link, linearize_link, feature_names)