k.n.n
v1.0.3
Published
This is a simple and easy-to-use implementation of the Machine Learning "k-Nearest Neighbors" algorithm
Downloads
9
Maintainers
Readme
k-Nearest Neighbors algorithm
This is an easy-to-use micro Node module that executes a kNN algorithm.
Current version: 1.0.3
Using
The module that you require in your code is the constructor of the kNN algorithm. This constructor takes one argument - the training data.
In order to launch the algorithm, use the launch function that takes 2 arguments - the k parameter and the unknown node - and returns an object with a type value and the percentage of the accuracy.
A node is a new piece of data in the (multiple-dimensional) graph, and it's defined by the "module.Node" object, who takes the attributes of the node and it's type (label) in the argument.
You can make multiple calculations by calling the "launch" function several times, with a different k or unknown-node.
Example
kNN = require("k.n.n");
var data = [ new kNN.Node({paramA: 1, paramB: 300, type: 'typeA'}), new kNN.Node({paramA: 3, paramB: 350, type: 'typeA'}), new kNN.Node({paramA: 6, paramB: 1200, type: 'typeB'}), new kNN.Node({paramA: 8, paramB: 900, type: 'typeB'}), new kNN.Node({paramA: 1, paramB: 1220, type: 'typeC'}), new kNN.Node({paramA: 2, paramB: 900, type: 'typeC'}) ];
var example = new kNN(data);
var results = example.launch(3, new kNN.Node({paramA:4, paramB:351, type:false}));
console.log(results.type + ": "+results.percentage+"%");