Implementation:Scikit learn Scikit learn NeighborsBase
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Nearest Neighbors |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for providing the base class and mixin infrastructure for all nearest neighbor estimators provided by scikit-learn.
Description
NeighborsBase is the abstract base class for all nearest neighbor estimators in scikit-learn. It defines common parameters such as n_neighbors, radius, algorithm, leaf_size, metric, and p. The module also provides the mixin classes KNeighborsMixin and RadiusNeighborsMixin that implement the core neighbor-querying methods, as well as utility functions like _get_weights for computing sample weights from distances.
Usage
Use NeighborsBase and its mixins when building custom nearest neighbor estimators or when understanding the internal architecture of scikit-learn's neighbors module. End users typically do not instantiate this class directly but instead use concrete subclasses such as KNeighborsClassifier or KNeighborsRegressor.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/neighbors/_base.py
Signature
class NeighborsBase(MultiOutputMixin, BaseEstimator, metaclass=ABCMeta):
def __init__(
self,
n_neighbors=None,
radius=None,
algorithm="auto",
leaf_size=30,
metric="minkowski",
p=2,
metric_params=None,
n_jobs=None,
):
Import
from sklearn.neighbors._base import NeighborsBase
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| n_neighbors | int or None | No | Number of neighbors to use by default for kneighbors queries |
| radius | float or None | No | Range of parameter space for radius-based neighbors queries |
| algorithm | str | No | Algorithm used to compute nearest neighbors: 'auto', 'ball_tree', 'kd_tree', or 'brute' |
| leaf_size | int | No | Leaf size passed to BallTree or KDTree (default=30) |
| metric | str or callable | No | Distance metric to use (default='minkowski') |
| p | float | No | Power parameter for the Minkowski metric (default=2) |
| metric_params | dict or None | No | Additional keyword arguments for the metric function |
| n_jobs | int or None | No | Number of parallel jobs to run for neighbors search |
Outputs
| Name | Type | Description |
|---|---|---|
| effective_metric_ | str | The metric actually used for distance computation after fitting |
| effective_metric_params_ | dict | The parameters for the effective metric |
| n_features_in_ | int | Number of features seen during fit |
| n_samples_fit_ | int | Number of samples in the fitted data |
Usage Examples
Basic Usage
from sklearn.neighbors import KNeighborsClassifier
# NeighborsBase is abstract; use a concrete subclass
clf = KNeighborsClassifier(n_neighbors=5, algorithm="auto", metric="minkowski", p=2)
clf.fit([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
print(clf.predict([[1.5, 1.5]]))