Implementing K Nearest Neighbor in Node.js

Posted By :Asheesh Bhuria |31st July 2020

K Nearest Neighbor is a very simple yet effective classification algorithm. 


This blog will walk you through a basic Javascript program to predict whether a human is an adult or a kid based on their height and weight.


In this tutorial we will be using ml-knn


First, let’s define the training dataset


// Training dataset
let trainingDataset = [
   // Training dataset for ADULT LABEL
   [173, 80],
   [165, 70],
   [182, 89],
   [172, 65],
   [169, 80],
   [176, 76],
   // Training dataset for KID LABEL
   [120, 46],
   [105, 35],
   [140, 48],
   [95, 40],
   [121, 37],
   [120, 36]
// Labels for the training dataset
let labels = ['ADULT','ADULT','ADULT','ADULT','ADULT','ADULT',



Feature - The first feature is the height (in centimeters), and the second feature is the weight (in kilograms).


Now, we will create a new model based on the training dataset and the labels.


let knn = new KNN(trainingDataset, labels, {k: 3});


Notice, I have chosen the k as 3. This means that while predicting, the model will look for three nearest neighbors.


Finally we can test our model with a testing dataset.


let testingDataset = [[180,90], [40, 12]];
let predictions = knn.predict(testingDataset);
console.log(predictions) // [ 'ADULT', 'KID' ]


This was a very basic implementation of a KNN model. Please consider the following points when solving a real time problem with this algorithm :-

  • The training dataset should be large and diverse in order to predict data correctly
  • There is no defined rule for setting the value of K, but usually professionals prefer the square root of the size of the training dataset.
  • The number of features must be enough to predict the dataset correctly.

About Author

Asheesh Bhuria

Asheesh Bhuria is a software engineer. With his knowledge in new technologies he excels in MEAN Stack development.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us