class_docstr: Base class for nearest neighbors estimators.
class_docstr_tok: ["Base","class","for","nearest","neighbors","estimators","."]
code: def _fit(self, X, session=None, run_kwargs=None):
      self._check_algorithm_metric()
      if self.metric_params is None:
          self.effective_metric_params_ = {}
      else:
          self.effective_metric_params_ = self.metric_params.copy()
  
      effective_p = self.effective_metric_params_.get("p", self.p)
      if self.metric in ["wminkowski", "minkowski"]:
          self.effective_metric_params_["p"] = effective_p
  
      self.effective_metric_ = self.metric
      # For minkowski distance, use more efficient methods where available
      if self.metric == "minkowski":
          p = self.effective_metric_params_.pop("p", 2)
          if p < 1:  # pragma: no cover
              raise ValueError(
                  "p must be greater than one for minkowski metric"
              )
          elif p == 1:
              self.effective_metric_ = "manhattan"
          elif p == 2:
              self.effective_metric_ = "euclidean"
          elif p == np.inf:
              self.effective_metric_ = "chebyshev"
          else:
              self.effective_metric_params_["p"] = p
  
      if isinstance(X, NeighborsBase):
          self._fit_X = X._fit_X
          self._tree = X._tree
          self._fit_method = X._fit_method
          return self
  
      elif isinstance(X, SklearnBallTree):
          self._fit_X = mt.tensor(X.data)
          self._tree = X
          self._fit_method = "ball_tree"
          return self
  
      elif isinstance(X, SklearnKDTree):
          self._fit_X = mt.tensor(X.data)
          self._tree = X
          self._fit_method = "kd_tree"
          return self
  
      X = check_array(X, accept_sparse=True)
  
      if np.isnan(X.size):
          # if X has unknown shape, execute it first
          X.execute(session=session, **(run_kwargs or dict()))
  
      if X.issparse():
          if self.algorithm not in ("auto", "brute"):
              warnings.warn(
                  "cannot use tree with sparse input: using brute force"
              )
          if self.effective_metric_ not in VALID_METRICS_SPARSE[
              "brute"
          ] and not callable(self.effective_metric_):
              raise ValueError(
                  "Metric '%s' not valid for sparse input. "
                  "Use sorted(sklearn.neighbors."
                  "VALID_METRICS_SPARSE['brute']) "
                  "to get valid options. "
                  "Metric can also be a callable function."
                  % (self.effective_metric_)
              )
          self._fit_X = X.copy()
          self._tree = None
          self._fit_method = "brute"
          return self
  
      self._fit_method = self.algorithm
      self._fit_X = X
  
      if self._fit_method == "auto":
          # A tree approach is better for small number of neighbors,
          # and KDTree is generally faster when available
          if (
              self.n_neighbors is None
              or self.n_neighbors < self._fit_X.shape[0] // 2
          ) and self.metric != "precomputed":
              if self.effective_metric_ in VALID_METRICS["kd_tree"]:
                  self._fit_method = "kd_tree"
              elif (
                  callable(self.effective_metric_)
                  or self.effective_metric_
                  in VALID_METRICS["ball_tree"]
              ):
                  self._fit_method = "ball_tree"
              else:
                  self._fit_method = "brute"
          else:
              self._fit_method = "brute"
  
      if self._fit_method == "ball_tree":
          self._tree = tree = create_ball_tree(
              X,
              self.leaf_size,
              metric=self.effective_metric_,
              **self.effective_metric_params_,
          )
          tree.execute(session=session, **(run_kwargs or dict()))
      elif self._fit_method == "kd_tree":
          self._tree = tree = create_kd_tree(
              X,
              self.leaf_size,
              metric=self.effective_metric_,
              **self.effective_metric_params_,
          )
          tree.execute(session=session, **(run_kwargs or dict()))
      elif self._fit_method == "brute":
          self._tree = None
      elif self._fit_method == "faiss":
          faiss_index = build_faiss_index(
              X,
              metric=self.effective_metric_,
              **self.effective_metric_params_,
          )
          faiss_index.execute(session=session, **(run_kwargs or dict()))
          self._faiss_index = faiss_index
      elif self._fit_method == "proxima":  # pragma: no cover
          proxima_metric = METRIC_TO_PROXIMA_METRIC_TYPE[
              self.effective_metric_
          ]
          proxima_index = build_proxima_index(
              X,
              distance_metric=proxima_metric,
              topk=self.n_neighbors,
              session=session,
              run_kwargs=run_kwargs,
              **self.effective_metric_params_,
          )
          self._proxima_index = proxima_index
      else:  # pragma: no cover
          raise ValueError(
              "algorithm = '%s' not recognized" % self.algorithm
          )
  
      if self.n_neighbors is not None:
          if self.n_neighbors <= 0:
              raise ValueError(
                  f"Expected n_neighbors > 0. Got {self.n_neighbors}"
              )
          else:
              if not np.issubdtype(type(self.n_neighbors), np.integer):
                  raise TypeError(
                      f"n_neighbors does not take {type(self.n_neighbors)} value, "
                      "enter integer value"
                  )
  
      return self
  
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 3 for calling the NeighborsBase obj's underlying member method code with example usage: obj._fit(X, session, run_kwargs) and returns: self, self, self, self, self