Update top JS snippets
This commit is contained in:
@@ -2,12 +2,12 @@
|
||||
title: kNearestNeighbors
|
||||
tags: algorithm,array,advanced
|
||||
firstSeen: 2020-12-28T16:31:43+02:00
|
||||
lastUpdated: 2020-12-29T16:31:16+02:00
|
||||
lastUpdated: 2021-10-13T19:29:39+02:00
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
- Use `Array.prototype.map()` to map the `data` to objects containing the euclidean distance of each element from `point`, calculated using `Math.hypot()`, `Object.keys()` and its `label`.
|
||||
- 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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user