docstr: This takes a background dataset (usually a subsample of the training set) as an input along with several
  user specified options and initialises a `KernelShap` explainer. The runtime of the algorithm depends on the
  number of samples in this dataset and on the number of features in the dataset. To reduce the size of the
  dataset, the `summarise_background` option and `n_background_samples` should be used. To reduce the feature
  dimensionality, encoded categorical variables can be treated as one during the feature perturbation process;
  this decreases the effective feature dimensionality, can reduce the variance of the shap values estimation and
  reduces slightly the number of calls to the predictor. Further runtime savings can be achieved by changing the
  `nsamples` parameter in the call to explain. Runtime reduction comes with an accuracy trade-off, so it is better
  to experiment with a runtime reduction method and understand results stability before using the system.
  
  Parameters
  -----------
  background_data
      Data used to estimate feature contributions and baseline values for force plots. The rows of the
      background data should represent samples and the columns features.
  summarise_background
      A large background dataset impacts the runtime and memory footprint of the algorithm. By setting
      this argument to ``True``, only `n_background_samples` from the provided data are selected. If
      group_names or groups arguments are specified, the algorithm assumes that the data contains categorical
      variables so the records are selected uniformly at random. Otherwise, `shap.kmeans` (a wrapper
      around `sklearn` k-means implementation) is used for selection. If set to ``'auto'``, a default of
      `KERNEL_SHAP_BACKGROUND_THRESHOLD` samples is selected.
  n_background_samples
      The number of samples to keep in the background dataset if ``summarise_background=True``.
  groups:
      A list containing sub-lists specifying the indices of features belonging to the same group.
  group_names:
      If specified, this array is used to treat groups of features as one during feature perturbation.
      This feature can be useful, for example, to treat encoded categorical variables as one and can
      result in computational savings (this may require adjusting the `nsamples` parameter).
  weights:
      A sequence or array of weights. This is used only if grouping is specified and assigns a weight
      to each point in the dataset.
  **kwargs:
      Expected keyword arguments include `keep_index` (bool) and should be used if a data frame containing an
      index column is passed to the algorithm.
code: def fit(
      self,  # type: ignore
      background_data: Union[
          np.ndarray, sparse.spmatrix, pd.DataFrame, shap_utils.Data
      ],
      summarise_background: Union[bool, str] = False,
      n_background_samples: int = KERNEL_SHAP_BACKGROUND_THRESHOLD,
      group_names: Union[Tuple[str], List[str], None] = None,
      groups: Optional[List[Union[Tuple[int], List[int]]]] = None,
      weights: Union[
          Union[List[float], Tuple[float]], np.ndarray, None
      ] = None,
      **kwargs,
  ) -> "KernelShap":
      """
      This takes a background dataset (usually a subsample of the training set) as an input along with several
      user specified options and initialises a `KernelShap` explainer. The runtime of the algorithm depends on the
      number of samples in this dataset and on the number of features in the dataset. To reduce the size of the
      dataset, the `summarise_background` option and `n_background_samples` should be used. To reduce the feature
      dimensionality, encoded categorical variables can be treated as one during the feature perturbation process;
      this decreases the effective feature dimensionality, can reduce the variance of the shap values estimation and
      reduces slightly the number of calls to the predictor. Further runtime savings can be achieved by changing the
      `nsamples` parameter in the call to explain. Runtime reduction comes with an accuracy trade-off, so it is better
      to experiment with a runtime reduction method and understand results stability before using the system.
  
      Parameters
      -----------
      background_data
          Data used to estimate feature contributions and baseline values for force plots. The rows of the
          background data should represent samples and the columns features.
      summarise_background
          A large background dataset impacts the runtime and memory footprint of the algorithm. By setting
          this argument to ``True``, only `n_background_samples` from the provided data are selected. If
          group_names or groups arguments are specified, the algorithm assumes that the data contains categorical
          variables so the records are selected uniformly at random. Otherwise, `shap.kmeans` (a wrapper
          around `sklearn` k-means implementation) is used for selection. If set to ``'auto'``, a default of
          `KERNEL_SHAP_BACKGROUND_THRESHOLD` samples is selected.
      n_background_samples
          The number of samples to keep in the background dataset if ``summarise_background=True``.
      groups:
          A list containing sub-lists specifying the indices of features belonging to the same group.
      group_names:
          If specified, this array is used to treat groups of features as one during feature perturbation.
          This feature can be useful, for example, to treat encoded categorical variables as one and can
          result in computational savings (this may require adjusting the `nsamples` parameter).
      weights:
          A sequence or array of weights. This is used only if grouping is specified and assigns a weight
          to each point in the dataset.
      **kwargs:
          Expected keyword arguments include `keep_index` (bool) and should be used if a data frame containing an
          index column is passed to the algorithm.
      """
      np.random.seed(self.seed)
  
      self._fitted = True
      # user has specified variable groups
      use_groups = groups is not None or group_names is not None
      self.use_groups = use_groups
  
      if summarise_background:
          if isinstance(summarise_background, str):
              if not isinstance(background_data, shap_utils.Data):
                  n_samples = background_data.shape[0]
              else:
                  n_samples = background_data.data.shape[0]
              n_background_samples = min(
                  n_samples, KERNEL_SHAP_BACKGROUND_THRESHOLD
              )
          background_data = self._summarise_background(
              background_data, n_background_samples
          )
  
      # check user inputs to provide warnings if input is incorrect
      self._check_inputs(background_data, group_names, groups, weights)
      if self.create_group_names and groups:
          group_names = [
              "group_{}".format(i) for i in range(len(groups))
          ]
      # disable grouping or data weights if inputs are not correct
      if self.ignore_weights:
          weights = None
      if not self.use_groups:
          group_names, groups = None, None
      else:
          self.feature_names = group_names  # type: ignore[assignment]
  
      # perform grouping if requested by the user
      self.background_data = self._get_data(
          background_data, group_names, groups, weights, **kwargs
      )
      self._explainer = self._init_shap_object()
      self.expected_value = self._explainer.expected_value
      if not self._explainer.vector_out:
          logger.warning(
              "Predictor returned a scalar value. Ensure the output represents a probability or decision score as "
              "opposed to a classification label!"
          )
  
      # update metadata
      params = {
          "groups": groups,
          "group_names": group_names,
          "weights": weights,
          "kwargs": kwargs,
          "summarise_background": self.summarise_background,
          "grouped": self.use_groups,
          "transpose": self.transposed,
      }
      self._update_metadata(params, params=True)
      return self
  
imports: import copy
  import logging
  from functools import partial
  from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
  import numpy
  import pandas
  import shap
  import shap.utils._legacy
  from scipy import sparse
  from scipy.special import expit
  from shap import KernelExplainer
  from alibi.api.defaults import DEFAULT_DATA_KERNEL_SHAP, DEFAULT_DATA_TREE_SHAP, DEFAULT_META_KERNEL_SHAP, DEFAULT_META_TREE_SHAP
  from alibi.api.interfaces import Explainer, Explanation, FitMixin
  from alibi.utils.wrappers import methdispatch
label_desc: Use image node_id 10 for calling the KernelShap obj's underlying member method code with example usage: obj.fit(background_data, summarise_background, n_background_samples, group_names, groups, weights) and returns: self