GithubHelp home page GithubHelp logo

knn's Introduction

knn

NPM version npm 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:

var train_dataset = [
  [0, 0, 0],
  [0, 1, 1],
  [1, 1, 0],
  [2, 2, 2],
  [1, 2, 2],
  [2, 1, 2],
];
var train_labels = [0, 0, 0, 1, 1, 1];
var knn = new KNN(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:

var test_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],
];

var ans = 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 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.

KNN.load(model[, distance])

Loads a model previously exported by knn.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

knn's People

Contributors

jeffersonh44 avatar lpatiny avatar maasencioh avatar targos avatar wadjih-bencheikh18 avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.