class_docstr: Mixin for k-neighbors searches
class_docstr_tok: ["Mixin","for","k-neighbors","searches"]
docstr: Computes the (weighted) graph of k-Neighbors for points in X
  
  Parameters
  ----------
  X : array-like, shape (n_query, n_features),                 or (n_query, n_indexed) if metric == 'precomputed'
      The query point or points.
      If not provided, neighbors of each indexed point are returned.
      In this case, the query point is not considered its own neighbor.
  
  n_neighbors : int
      Number of neighbors for each sample.
      (default is value passed to the constructor).
  
  mode : {'connectivity', 'distance'}, optional
      Type of returned matrix: 'connectivity' will return the
      connectivity matrix with ones and zeros, in 'distance' the
      edges are Euclidean distance between points.
  
  Returns
  -------
  A : SparseTensor, shape = [n_samples, n_samples_fit]
      n_samples_fit is the number of samples in the fitted data
      A[i, j] is assigned the weight of edge that connects i to j.
  
  Examples
  --------
  >>> X = [[0], [3], [1]]
  >>> from mars.learn.neighbors import NearestNeighbors
  >>> neigh = NearestNeighbors(n_neighbors=2)
  >>> neigh.fit(X) # doctest: +ELLIPSIS
  NearestNeighbors(algorithm='auto', leaf_size=30, ...)
  >>> A = neigh.kneighbors_graph(X)
  >>> A.fetch().toarray()
  array([[1., 0., 1.],
         [0., 1., 1.],
         [1., 0., 1.]])
  
  See also
  --------
  NearestNeighbors.radius_neighbors_graph
docstr_tok: ["Computes","the","(","weighted",")","graph","of","k-Neighbors","for","points","in","X","Parameters","--","--","--","--","--","X",":","array-like",",","shape","(","n_query",",","n_features",")",",","or","(","n_query",",","n_indexed",")","if","metric","==","'precomputed'","The","query","point","or","points",".","If","not","provided",",","neighbors","of","each","indexed","point","are","returned",".","In","this","case",",","the","query","point","is","not","considered","its","own","neighbor",".","n_neighbors",":","int","Number","of","neighbors","for","each","sample",".","(","default","is","value","passed","to","the","constructor",")",".","mode",":","{","'connectivity","'",",","'distance","'","}",",","optional","Type","of","returned","matrix",":","'connectivity","'","will","return","the","connectivity","matrix","with","ones","and","zeros",",","in","'distance","'","the","edges","are","Euclidean","distance","between","points",".","Returns","--","--","--","-","A",":","SparseTensor",",","shape","=","[","n_samples",",","n_samples_fit","]","n_samples_fit","is","the","number","of","samples","in","the","fitted","data","A","[","i",",","j","]","is","assigned","the","weight","of","edge","that","connects","i","to","j",".","Examples","--","--","--","--",">",">",">","X","=","[","[","0","]",",","[","3","]",",","[","1","]","]",">",">",">","from","mars.learn.neighbors","import","NearestNeighbors",">",">",">","neigh","=","NearestNeighbors","(","n_neighbors=2",")",">",">",">","neigh.fit","(","X",")","#","doctest",":","+ELLIPSIS","NearestNeighbors","(","algorithm='auto","'",",","leaf_size=30",",","...",")",">",">",">","A","=","neigh.kneighbors_graph","(","X",")",">",">",">","A.fetch","(",")",".toarray","(",")","array","(","[","[","1.",",","0.",",","1",".","]",",","[","0.",",","1.",",","1",".","]",",","[","1.",",","0.",",","1",".","]","]",")","See","also","--","--","--","--","NearestNeighbors.radius_neighbors_graph"]
code: def kneighbors_graph(
      self,
      X=None,
      n_neighbors=None,
      mode="connectivity",
      session=None,
      run_kwargs=None,
  ):
      """Computes the (weighted) graph of k-Neighbors for points in X
  
          Parameters
          ----------
          X : array-like, shape (n_query, n_features), \
                  or (n_query, n_indexed) if metric == 'precomputed'
              The query point or points.
              If not provided, neighbors of each indexed point are returned.
              In this case, the query point is not considered its own neighbor.
  
          n_neighbors : int
              Number of neighbors for each sample.
              (default is value passed to the constructor).
  
          mode : {'connectivity', 'distance'}, optional
              Type of returned matrix: 'connectivity' will return the
              connectivity matrix with ones and zeros, in 'distance' the
              edges are Euclidean distance between points.
  
          Returns
          -------
          A : SparseTensor, shape = [n_samples, n_samples_fit]
              n_samples_fit is the number of samples in the fitted data
              A[i, j] is assigned the weight of edge that connects i to j.
  
          Examples
          --------
          >>> X = [[0], [3], [1]]
          >>> from mars.learn.neighbors import NearestNeighbors
          >>> neigh = NearestNeighbors(n_neighbors=2)
          >>> neigh.fit(X) # doctest: +ELLIPSIS
          NearestNeighbors(algorithm='auto', leaf_size=30, ...)
          >>> A = neigh.kneighbors_graph(X)
          >>> A.fetch().toarray()
          array([[1., 0., 1.],
                 [0., 1., 1.],
                 [1., 0., 1.]])
  
          See also
          --------
          NearestNeighbors.radius_neighbors_graph
          """
      check_is_fitted(self, ["_fit_method", "_fit_X"], all_or_any=any)
      if n_neighbors is None:
          n_neighbors = self.n_neighbors
  
      # kneighbors does the None handling.
      if X is not None:
          X = check_array(X, accept_sparse=True)
          n_samples1 = X.shape[0]
      else:
          n_samples1 = self._fit_X.shape[0]
  
      n_samples2 = self._fit_X.shape[0]
  
      if mode == "connectivity":
          A_data = None
          A_ind = self.kneighbors(X, n_neighbors, return_distance=False)
  
      elif mode == "distance":
          A_data, A_ind = self.kneighbors(
              X, n_neighbors, return_distance=True
          )
  
      else:
          raise ValueError(
              'Unsupported mode, must be one of "connectivity" '
              f'or "distance" but got {mode} instead'
          )
  
      op = KNeighborsGraph(
          a_data=A_data,
          a_ind=A_ind,
          n_neighbors=n_neighbors,
          sparse=True,
      )
      graph = op(A_data, A_ind, shape=(n_samples1, n_samples2))
      graph.execute(session=session, **(run_kwargs or dict()))
      return graph
  
imports: import warnings
  from abc import ABCMeta, abstractmethod
  import numpy
  from sklearn.base import BaseEstimator, MultiOutputMixin
  from ...None import tensor
  from ...tensor.reshape.reshape import _reshape
  from ..metrics.pairwise import PAIRWISE_DISTANCE_FUNCTIONS
  from ..metrics import pairwise_distances_topk
  from ..utils import check_array
  from ..utils.validation import check_is_fitted
  from ._ball_tree import create_ball_tree, ball_tree_query, SklearnBallTree
  from ._kd_tree import create_kd_tree, kd_tree_query, SklearnKDTree
  from ._faiss import build_faiss_index, faiss_query, METRIC_TO_FAISS_METRIC_TYPE
  from ._proxima import build_proxima_index, proxima_query, METRIC_TO_PROXIMA_METRIC_TYPE
  from ._kneighbors_graph import KNeighborsGraph
label_desc: Use image node_id 2 for calling the KNeighborsMixin obj's underlying member method code with example usage: obj.kneighbors_graph(X, n_neighbors, mode, session, run_kwargs) and returns: graph