class_docstr: A pure Python (slow) implementation of Tree SHAP.
      
class_docstr_tok: ["A","pure","Python","(","slow",")","implementation","of","Tree","SHAP","."]
code: def shap_values(self, X, tree_limit=-1, **kwargs):
      # shortcut using the C++ version of Tree SHAP in XGBoost and LightGBM
      # these are about 10x faster than the numba jit'd implementation below...
      if self.model_type == "xgboost":
          import xgboost
  
          if not str(type(X)).endswith("xgboost.core.DMatrix'>"):
              X = xgboost.DMatrix(X)
          if tree_limit == -1:
              tree_limit = 0
          return self.trees.predict(
              X, ntree_limit=tree_limit, pred_contribs=True
          )
      elif self.model_type == "lightgbm":
          return self.trees.predict(
              X, num_iteration=tree_limit, pred_contrib=True
          )
  
      # convert dataframes
      if isinstance(X, (pd.Series, pd.DataFrame)):
          X = X.values
  
      assert isinstance(X, np.ndarray), "Unknown instance type: " + str(
          type(X)
      )
      assert (
          len(X.shape) == 1 or len(X.shape) == 2
      ), "Instance must have 1 or 2 dimensions!"
  
      n_outputs = self.trees[0].values.shape[1]
  
      # single instance
      if len(X.shape) == 1:
          phi = np.zeros(X.shape[0] + 1, n_outputs)
          x_missing = np.zeros(X.shape[0], dtype=bool)
          for t in self.trees:
              self.tree_shap(t, X, x_missing, phi)
          phi /= len(self.trees)
  
          if n_outputs == 1:
              return phi[:, 0]
          else:
              return [phi[:, i] for i in range(n_outputs)]
  
      elif len(X.shape) == 2:
          phi = np.zeros((X.shape[0], X.shape[1] + 1, n_outputs))
          x_missing = np.zeros(X.shape[1], dtype=bool)
          for i in range(X.shape[0]):
              for t in self.trees:
                  self.tree_shap(t, X[i, :], x_missing, phi[i, :, :])
          phi /= len(self.trees)
  
          if n_outputs == 1:
              return phi[:, :, 0]
          else:
              return [phi[:, :, i] for i in range(n_outputs)]
  
file_path: shap/shap/explainers/pytree.py
imports: import numpy
  import pandas
  from ..utils._exceptions import ExplainerError
label_desc: Use image node_id 2 for calling the TreeExplainer obj's underlying member method code with example usage: obj.shap_values(X, tree_limit) and returns: self, self, phi, unknown, phi, unknown