Skip to content

Commit

Permalink
Storify k-nearest-neighbors
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed May 14, 2024
1 parent 2a81c26 commit df01172
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions content/snippets/js/s/k-nearest-neighbors.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
---
title: K-nearest neighbors
type: snippet
title: Classify data using the K-nearest neighbors algorithm in JavaScript
shortTitle: K-nearest neighbors
type: story
language: javascript
tags: [algorithm,array]
cover: building-blocks
dateModified: 2021-10-13
excerpt: Implement the K-nearest neighbors algorithm in JavaScript to classify a data point relative to a labelled data set.
dateModified: 2024-05-12
---

Classifies a data point relative to a labelled data set, using the [k-nearest neighbors](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) algorithm.
## Definition

- Use `Array.prototype.map()` to map the `data` to objects. Each object contains the euclidean distance of the element from `point`, calculated using `Math.hypot()`, `Object.keys()` and its `label`.
- Use `Array.prototype.sort()` and `Array.prototype.slice()` to get the `k` nearest neighbors of `point`.
- Use `Array.prototype.reduce()` in combination with `Object.keys()` and `Array.prototype.indexOf()` to find the most frequent `label` among them.
The [K-nearest neighbors](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) algorithm is a **simple, instance-based learning algorithm** used for classification and regression tasks. It classifies a data point based on the majority class of its `k` nearest neighbors in a feature space.

## Implementation

This implementation classifies a data point relative to a labelled data set, using the K-nearest neighbors algorithm. The `data` is expected to be an array of objects, where each object represents a data point with its features. The `labels` are an array of the corresponding classes for each data point.

1. Use `Array.prototype.map()` to map the `data` to objects. Each object contains the [**Euclidean distance**](/js/s/euclidean-distance) of the element from `point`, calculated using `Math.hypot()`, `Object.keys()` and its `label`.
2. Use `Array.prototype.sort()` and `Array.prototype.slice()` to get the `k` **nearest neighbors** of `point`.
3. Use `Array.prototype.reduce()` in combination with `Object.keys()` and `Array.prototype.indexOf()` to find the **most frequent** `label` among them.
4. Return the most frequent `label` as the **classification** of `point`.

```js
const kNearestNeighbors = (data, labels, point, k = 3) => {
Expand Down Expand Up @@ -49,3 +58,7 @@ const labels = [0, 1, 1, 0];
kNearestNeighbors(data, labels, [1, 2], 2); // 1
kNearestNeighbors(data, labels, [1, 0], 2); // 0
```

## Complexity

The **time complexity** of this algorithm is `O(n)` for each classification, where `n` is the number of data points. The **space complexity** is `O(n)` as well, since we are storing the distances and labels for each data point. The algorithm is **non-parametric**, meaning it does not make any assumptions about the underlying data distribution. It is also **lazy**, as it does not require a training phase, making it suitable for online learning scenarios.

0 comments on commit df01172

Please sign in to comment.