code: def _sample_knn(
      x: np.ndarray, X_train: np.ndarray, nb_samples: int = 10
  ) -> np.ndarray:
      """Samples data points from a training set around instance x using k-nearest neighbours.
  
      Parameters
      ----------
      x
          Central instance for sampling.
      X_train
          Training set.
      nb_samples
          Number of samples to generate.
  
      Returns
      -------
      Sampled vectors.
  
      """
      x = x.reshape(x.shape[0], -1)
      nb_instances = x.shape[0]
      X_sampled = []
      for i in range(nb_instances):
          X_train = X_train.reshape(X_train.shape[0], -1)
          X_stack = np.stack(
              [x[i] for _ in range(X_train.shape[0])], axis=0
          )
          X_stack = X_stack.reshape(X_stack.shape[0], -1)
  
          nbrs = NearestNeighbors(
              n_neighbors=nb_samples, algorithm="ball_tree"
          ).fit(X_train)
          distances, indices = nbrs.kneighbors(X_stack)
          distances, indices = distances[0], indices[0]
  
          X_sampled_tmp = X_train[indices]
          X_sampled.append(X_sampled_tmp)
  
      return np.asarray(X_sampled)
  
file_path: alibi/alibi/confidence/model_linearity.py
imports: import logging
  import string
  from time import time
  from typing import Callable, List, Optional, Tuple, Union
  import numpy
  from numpy.linalg import norm
  from sklearn.neighbors import NearestNeighbors
label_desc: Use image node_id 4 for calling a global function with example usage: _sample_knn(x, X_train, nb_samples) and returns: np