Implementing K-Nearest Neighbors in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the K-Nearest Neighbors algorithm, a powerful tool for classification of data points. By implementing this algorithm in JavaScript, we will be able to classify new data points based on their proximity to existing labeled data points, making it a valuable tool for a variety of applications, such as recommendation systems and image recognition. Through this lab, we will gain a deeper understanding of how machine learning algorithms work and how they can be implemented in real-world scenarios.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/BasicConceptsGroup -.-> javascript/obj_manip("`Object Manipulation`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} javascript/data_types -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} javascript/arith_ops -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} javascript/comp_ops -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} javascript/cond_stmts -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} javascript/array_methods -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} javascript/obj_manip -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} javascript/higher_funcs -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} javascript/spread_rest -.-> lab-28461{{"`Implementing K-Nearest Neighbors in JavaScript`"}} end

K-Nearest Neighbors Algorithm

To use the K-Nearest Neighbors Algorithm, follow these steps:

  1. Open the Terminal/SSH and type node.
  2. Classify a data point relative to a labeled data set using the k-nearest neighbors algorithm.
  3. Map the data to objects using Array.prototype.map(). Each object contains the Euclidean distance of the element from point, calculated using Math.hypot(), Object.keys(), and its label.
  4. Use Array.prototype.sort() and Array.prototype.slice() to get the k nearest neighbors of point.
  5. Use Array.prototype.reduce() in combination with Object.keys() and Array.prototype.indexOf() to find the most frequent label among them.

Here's an example code that implements the K-Nearest Neighbors Algorithm:

const kNearestNeighbors = (data, labels, point, k = 3) => {
  const kNearest = data
    .map((el, i) => ({
      dist: Math.hypot(...Object.keys(el).map((key) => point[key] - el[key])),
      label: labels[i]
    }))
    .sort((a, b) => a.dist - b.dist)
    .slice(0, k);

  return kNearest.reduce(
    (acc, { label }, i) => {
      acc.classCounts[label] =
        Object.keys(acc.classCounts).indexOf(label) !== -1
          ? acc.classCounts[label] + 1
          : 1;
      if (acc.classCounts[label] > acc.topClassCount) {
        acc.topClassCount = acc.classCounts[label];
        acc.topClass = label;
      }
      return acc;
    },
    {
      classCounts: {},
      topClass: kNearest[0].label,
      topClassCount: 0
    }
  ).topClass;
};

Here's how to use the code:

const data = [
  [0, 0],
  [0, 1],
  [1, 3],
  [2, 0]
];
const labels = [0, 1, 1, 0];

kNearestNeighbors(data, labels, [1, 2], 2); // 1
kNearestNeighbors(data, labels, [1, 0], 2); // 0

Summary

Congratulations! You have completed the K-Nearest Neighbors lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like