Implementation:Scikit learn Scikit learn KNeighborsClassifier
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Classification |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for k-nearest neighbors classification provided by scikit-learn.
Description
KNeighborsClassifier implements classification based on the k-nearest neighbors vote. A query point is classified by a majority vote of its nearest neighbors, with the class most common among its k nearest neighbors being assigned. It also includes RadiusNeighborsClassifier, which classifies based on neighbors within a fixed radius. Both classifiers support uniform and distance-based weighting as well as callable weight functions.
Usage
Use KNeighborsClassifier when you need a simple, non-parametric classifier that makes predictions based on the similarity of training samples. It is well-suited for problems with complex decision boundaries and moderate dataset sizes.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/neighbors/_classification.py
Signature
class KNeighborsClassifier(KNeighborsMixin, ClassifierMixin, NeighborsBase):
def __init__(
self,
n_neighbors=5,
*,
weights="uniform",
algorithm="auto",
leaf_size=30,
p=2,
metric="minkowski",
metric_params=None,
n_jobs=None,
):
Import
from sklearn.neighbors import KNeighborsClassifier
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| n_neighbors | int | No | Number of neighbors to use (default=5) |
| weights | str or callable | No | Weight function: 'uniform', 'distance', or a callable (default='uniform') |
| algorithm | str | No | Algorithm to compute nearest neighbors: 'auto', 'ball_tree', 'kd_tree', 'brute' |
| leaf_size | int | No | Leaf size for BallTree or KDTree (default=30) |
| p | float | No | Power parameter for Minkowski metric (default=2) |
| metric | str or callable | No | Distance metric (default='minkowski') |
| metric_params | dict or None | No | Additional keyword arguments for the metric function |
| n_jobs | int or None | No | Number of parallel jobs for neighbors search |
Outputs
| Name | Type | Description |
|---|---|---|
| classes_ | ndarray of shape (n_classes,) | Class labels known to the classifier |
| effective_metric_ | str | The metric used for distance computation |
| effective_metric_params_ | dict | 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 |
| outputs_2d_ | bool | Whether output of fit is 2d (multi-output) |
Usage Examples
Basic Usage
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
clf = KNeighborsClassifier(n_neighbors=5, weights="uniform")
clf.fit(X_train, y_train)
print(clf.score(X_test, y_test))