Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/knnPublic

A k-nearest neighboor classifier algorithm.

License

NotificationsYou must be signed in to change notification settings

mljs/knn

Repository files navigation

NPM versionnpm download

A General purpose k-nearest neighbor classifier algorithm based on the k-d tree Javascript library develop by Ubilabs:

Installation

$ npm i ml-knn

API

new KNN(dataset, labels[, options])

Instantiates the KNN algorithm.

Arguments:

  • 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.

Options:

  • k - number of nearest neighbors (Default: number of labels + 1).
  • distance - distance function for the algorithm (Default: euclidean distance).

Example:

vartrain_dataset=[[0,0,0],[0,1,1],[1,1,0],[2,2,2],[1,2,2],[2,1,2],];vartrain_labels=[0,0,0,1,1,1];varknn=newKNN(train_dataset,train_labels,{k:2});// consider 2 nearest neighbors

predict(newDataset)

Predict the values of the dataset.

Arguments:

  • newDataset - A matrix that contains the dataset.

Example:

vartest_dataset=[[0.9,0.9,0.9],[1.1,1.1,1.1],[1.1,1.1,1.2],[1.2,1.2,1.2],];varans=knn.predict(test_dataset);console.log(ans);// classification result:// ans = [ 0, 0, 1, 1 ]// Based on the training data, the first two points of the test dataset are classified as "0" (type 0, perhaps),// the third and fourth data points are classified as "1".

toJSON()

Returns an object representing the model. This function is automatically called ifJSON.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.

KNN.load(model[, distance])

Loads a model previously exported byknn.toJSON(). If a custom distance function was provided, it must be passed again.

External links

Check this cool blog post for a detailed example:https://hackernoon.com/machine-learning-with-javascript-part-2-da994c17d483

License

MIT

About

A k-nearest neighboor classifier algorithm.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp