$ npm i ml-knn
new KNN(dataset, labels[, options])
Instantiates the KNN algorithm.
dataset- A matrix (2D array) of the dataset.
labels- An array of labels (one for each sample in the dataset).
options- Object with the options for the algorithm.
k- number of nearest neighbors (Default: number of labels + 1).
distance- distance function for the algorithm (Default: euclidean distance).
var dataset =0 0 00 1 11 1 02 2 21 2 22 1 2;var predictions = 0 0 0 1 1 1;var knn = dataset predictions;
Predict the values of the dataset.
newDataset- A matrix that contains the dataset.
var dataset = 0 0 0 2 2 2;var ans = knn;
Returns an object representing the model. This function is automatically called if
JSON.stringify(knn) is used.
Be aware that the serialized model takes about 1.3 times the size of the input dataset (it actually is the dataset in a tree structure). Stringification can fail if the resulting string is too large.
Loads a model previously exported by
knn.toJSON(). If a custom distance function was provided, it must be passed again.