code: def find_neighbors(selection, dataset, model, mode, n_neighbors=10):
      """
      For each instance, select neighbors based on 3 criteria:
  
      1. First pick top N closest neighbors (L1 Norm + st. dev normalization)
      2. Filter neighbors whose model output is too different from instance (see condition below)
      3. Filter neighbors whose distance is too big compared to a certain threshold
  
      Parameters
      ----------
      selection : list
          Indices of rows to be displayed on the stability plot
      dataset : DataFrame
          Entire dataset used to identify neighbors
      model : model object
          ML model
      mode : str
          "classification" or "regression"
      n_neighbors : int, optional
          Top N neighbors initially allowed, by default 10
  
      Returns
      -------
      all_neighbors : list of 2D arrays
          Wrap all instances with corresponding neighbors in a list with length (#instances).
          Each array has shape (#neighbors, #features) where #neighbors includes the instance itself.
      """
      instances = dataset.loc[selection].values
  
      all_neighbors = np.empty((0, instances.shape[1] + 1), float)
      """Filter 1 : Pick top N closest neighbors"""
      for instance in instances:
          c = _compute_similarities(instance, dataset.values)
          # Pick indices of the closest neighbors (and include instance itself)
          neighbors_indices = np.argsort(c)[: n_neighbors + 1]
          # Return instance with its neighbors
          neighbors = dataset.values[neighbors_indices]
          # Add distance column
          neighbors = np.append(
              neighbors,
              c[neighbors_indices].reshape(n_neighbors + 1, 1),
              axis=1,
          )
          all_neighbors = np.append(all_neighbors, neighbors, axis=0)
  
      # Calculate predictions for all instances and corresponding neighbors
      if mode == "regression":
          # For XGB it is necessary to add columns in df, otherwise columns mismatch
          predictions = model.predict(
              pd.DataFrame(
                  all_neighbors[:, :-1], columns=dataset.columns
              )
          )
      elif mode == "classification":
          predictions = model.predict_proba(
              pd.DataFrame(
                  all_neighbors[:, :-1], columns=dataset.columns
              )
          )[:, 1]
  
      # Add prediction column
      all_neighbors = np.append(
          all_neighbors,
          predictions.reshape(all_neighbors.shape[0], 1),
          axis=1,
      )
      # Split back into original chunks (1 chunck = instance + neighbors)
      all_neighbors = np.split(all_neighbors, instances.shape[0])
  
      """Filter 2 : neighbors with similar blackbox output"""
      # Remove points if prediction is far away from instance prediction
      if mode == "regression":
          # Trick : use enumerate to allow the modifcation directly on the iterator
          for i, neighbors in enumerate(all_neighbors):
              all_neighbors[i] = neighbors[
                  abs(neighbors[:, -1] - neighbors[0, -1])
                  < 0.1 * abs(neighbors[0, -1])
              ]
      elif mode == "classification":
          for i, neighbors in enumerate(all_neighbors):
              all_neighbors[i] = neighbors[
                  abs(neighbors[:, -1] - neighbors[0, -1]) < 0.1
              ]
  
      """Filter 3 : neighbors below a distance threshold"""
      # Remove points if distance is bigger than radius
      radius = _get_radius(dataset.values, n_neighbors)
  
      for i, neighbors in enumerate(all_neighbors):
          # -2 indicates the distance column
          all_neighbors[i] = neighbors[neighbors[:, -2] < radius]
      return all_neighbors
  
file_path: shapash/shapash/utils/explanation_metrics.py
imports: import numpy
  import pandas
  from sklearn.preprocessing import normalize
label_desc: Use image node_id 5 for calling a global function with example usage: find_neighbors(selection, dataset, model, mode, n_neighbors) and returns: all_neighbors