class_docstr: Base class for nearest neighbors estimators.
code: def _fit(self, X):
      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:
              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, BallTree):
          self._fit_X = X.data
          self._tree = X
          self._fit_method = "ball_tree"
          return self
  
      elif isinstance(X, KDTree):
          self._fit_X = X.data
          self._tree = X
          self._fit_method = "kd_tree"
          return self
  
      X = check_array(X, accept_sparse="csr")
  
      n_samples = X.shape[0]
      if n_samples == 0:
          raise ValueError("n_samples must be greater than 0")
  
      if issparse(X):
          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"]
          ):
              raise ValueError(
                  "metric '%s' not valid for sparse input"
                  % 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"
              else:
                  self._fit_method = "ball_tree"
          else:
              self._fit_method = "brute"
  
      if self._fit_method == "ball_tree":
          self._tree = BallTree(
              X,
              self.leaf_size,
              metric=self.effective_metric_,
              **self.effective_metric_params_,
          )
      elif self._fit_method == "kd_tree":
          self._tree = KDTree(
              X,
              self.leaf_size,
              metric=self.effective_metric_,
              **self.effective_metric_params_,
          )
      elif self._fit_method == "brute":
          self._tree = None
      else:
          raise ValueError(
              "algorithm = '%s' not recognized" % self.algorithm
          )
  
      if self.n_neighbors is not None:
          if self.n_neighbors <= 0:
              raise ValueError(
                  "Expected n_neighbors > 0. Got %d" % self.n_neighbors
              )
  
      return self
  
imports: import warnings
  from abc import ABCMeta, abstractmethod
  import numpy
  from scipy.sparse import csr_matrix, issparse
  from .ball_tree import BallTree
  from .kd_tree import KDTree
  from ..base import BaseEstimator
  from ..metrics import pairwise_distances
  from ..metrics.pairwise import PAIRWISE_DISTANCE_FUNCTIONS
  from ..utils import check_X_y, check_array, _get_n_jobs, gen_even_slices
  from ..utils.fixes import argpartition
  from ..utils.multiclass import check_classification_targets
  from ..externals import six
  from ..externals.joblib import Parallel, delayed
  from ..exceptions import NotFittedError
  from ..exceptions import DataConversionWarning
label_desc: Use image node_id 3 for calling the NeighborsBase obj's underlying member method code with example usage: obj._fit(X) and returns: self, self, self, self, self