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',
'KID','KID','KID','KID','KID','KID'];
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 :-