Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn KNeighborsRegressor

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Regression
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for k-nearest neighbors regression provided by scikit-learn.

Description

KNeighborsRegressor predicts target values by local interpolation of the targets associated with the nearest neighbors in the training set. It supports uniform and distance-based weighting, as well as callable weight functions. The module also includes RadiusNeighborsRegressor which uses all neighbors within a fixed radius for prediction. Both regressors support multiple distance metrics and tree-based algorithms for efficient neighbor search.

Usage

Use KNeighborsRegressor when you need a non-parametric regression method that makes predictions based on the similarity of training samples. It is well-suited for problems where the relationship between features and targets is complex and locally smooth.

Code Reference

Source Location

Signature

class KNeighborsRegressor(KNeighborsMixin, RegressorMixin, 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 KNeighborsRegressor

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, DistanceMetric, 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
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

Usage Examples

Basic Usage

from sklearn.neighbors import KNeighborsRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

X, y = make_regression(n_samples=200, n_features=4, noise=0.1, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

reg = KNeighborsRegressor(n_neighbors=5, weights="distance")
reg.fit(X_train, y_train)
print(f"R2 score: {reg.score(X_test, y_test):.3f}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment