code: def shap_neighbors(instance, x_encoded, contributions, mode):
      """
      For an instance and corresponding neighbors, calculate various
      metrics (described below) that are useful to evaluate local stability
  
      Parameters
      ----------
      instance : 2D array
          Instance + neighbours with corresponding features
      x_encoded : DataFrame
          Entire dataset used to identify neighbors
      contributions : DataFrame
          Calculated contribution values for the dataset
  
      Returns
      -------
      norm_shap_values : array
          Normalized SHAP values (with corresponding sign) of instance and its neighbors
      average_diff : array
          Variability (stddev / mean) of normalized SHAP values (using L1) across neighbors for each feature
      norm_abs_shap_values[0, :] : array
          Normalized absolute SHAP value of the instance
      """
      # Extract SHAP values for instance and neighbors
      # :-2 indicates that two columns are disregarded : distance to instance and model output
      ind = (
          pd.merge(
              x_encoded.reset_index(),
              pd.DataFrame(instance[:, :-2], columns=x_encoded.columns),
              how="inner",
          )
          .set_index(
              x_encoded.index.name
              if x_encoded.index.name is not None
              else "index"
          )
          .index
      )
      # If classification, select contrbutions of one class only
      if mode == "classification" and len(contributions) == 2:
          contributions = contributions[1]
      shap_values = contributions.loc[ind]
      # For neighbors comparison, the sign of SHAP values is taken into account
      norm_shap_values = normalize(shap_values, axis=1, norm="l1")
      # But not for the average impact of the features across the dataset
      norm_abs_shap_values = normalize(
          np.abs(shap_values), axis=1, norm="l1"
      )
      # Compute the average difference between the instance and its neighbors
      # And replace NaN with 0
      average_diff = np.divide(
          norm_shap_values.std(axis=0),
          norm_abs_shap_values.mean(axis=0),
          out=np.zeros(norm_abs_shap_values.shape[1]),
          where=norm_abs_shap_values.mean(axis=0) != 0,
      )
  
      return norm_shap_values, average_diff, norm_abs_shap_values[0, :]
  
file_path: shapash/shapash/utils/explanation_metrics.py
imports: import numpy
  import pandas
  from sklearn.preprocessing import normalize
label_desc: Use image node_id 6 for calling a global function with example usage: shap_neighbors(instance, x_encoded, contributions, mode) and returns: norm_shap_values,  average_diff,  norm_abs_shap_values