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.
K-Nearest Neighbors Algorithm
To use the K-Nearest Neighbors Algorithm, follow these steps:
- Open the Terminal/SSH and type
node. - Classify a data point relative to a labeled data set using the k-nearest neighbors algorithm.
- Map the
datato objects usingArray.prototype.map(). Each object contains the Euclidean distance of the element frompoint, calculated usingMath.hypot(),Object.keys(), and itslabel. - Use
Array.prototype.sort()andArray.prototype.slice()to get theknearest neighbors ofpoint. - Use
Array.prototype.reduce()in combination withObject.keys()andArray.prototype.indexOf()to find the most frequentlabelamong 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.