---
title: "KNeighborsRegressor in JavaScript with @kanaries/ml"
description: "Predict numeric targets from nearby examples with the KNeighborsRegressor JavaScript and TypeScript implementation in @kanaries/ml."
canonical_url: "https://ml.kanaries.net/docs/apis/neighbors/kneighborsRegressor"
markdown_url: "https://ml.kanaries.net/docs/apis/neighbors/kneighborsRegressor.md"
---
# KNeighborsRegressor in JavaScript

## Algorithm overview

KNeighborsRegressor predicts a continuous value by finding nearby training samples and averaging their targets. It is useful as a non-parametric baseline when similar examples should have similar outcomes.

## JavaScript implementation

`@kanaries/ml` provides `Neighbors.KNeighborsRegressor` for browser and Node.js workflows. It supports uniform or distance weighting and the distance metrics exposed by `Metrics.Distance`.

## Interactive K-neighbors regression playground

Tune `k`, change the sample density, and add observations to see the local average move. Every prediction comes from a live distance-weighted `Neighbors.KNeighborsRegressor`.

> The HTML version includes an interactive K-Neighbors Regression playground powered by @kanaries/ml. You can change the dataset, noise, and model controls; add observations; and inspect live predictions plus train and holdout metrics. The runnable guide and API reference continue below. [Open the HTML page](https://ml.kanaries.net/docs/apis/neighbors/kneighborsRegressor).

## Quick start example

```ts
import { Neighbors } from '@kanaries/ml';

const reg = new Neighbors.KNeighborsRegressor({ nNeighbors: 2, weights: 'distance' });
reg.fit([[0], [1], [3]], [0, 1, 3]);
const pred = reg.predict([[2]]);
console.log(pred);
```

## Detailed API reference

```ts
new Neighbors.KNeighborsRegressor()
new Neighbors.KNeighborsRegressor(nNeighbors: number, weights?: 'uniform' | 'distance', metric?: Distance.IDistanceType, p?: number)
new Neighbors.KNeighborsRegressor(props?: {
  nNeighbors?: number;
  weights?: 'uniform' | 'distance';
  metric?: Distance.IDistanceType;
  p?: number;
})
```

Methods:

- `fit(trainX: number[][], trainY: number[]): void`
- `predict(testX: number[][]): number[]`

`metric` defaults to `'euclidean'`, `nNeighbors` defaults to `5`, and `p` is used by Minkowski distance.
