KNNClassifier: K-Nearest Neighbors Classifier

Illustration of hands making the signs of rock, paper, and scissors on top of different labels

Description

“In pattern recognition, the k-nearest neighbors algorithm (k-NN) is a non-parametric method used for classification and regression.[1] In both cases, the input consists of the k closest training examples in the feature space.” - Wikipedia

This class allows you to create a classifier using the K-Nearest Neighbors algorithm. It’s a little different from other classes in this library, because it doesn’t provide a model with weights, but rather a utility for constructing a KNN model using outputs from another model or any other data that could be classified.

For example, you could get features of an image by calling FeatureExtractor.infer(), and feed the features to KNNClassifier to classify an image.

You can also collect any kind of data, construct them into an array of numbers and feed them to KNNClassifier. Check out this example that uses KNNClassifier to classify data from PoseNet model.

Quickstart

  1. // Create a KNN classifier
  2. const knnClassifier = ml5.KNNClassifier();
  3. // Create a featureExtractor that can extract features of an image
  4. const featureExtractor = ml5.featureExtractor('MobileNet', modelReady);
  5. // Get the features of an image
  6. const features = featureExtractor.infer(myImg);
  7. // Add an example with a label to the KNN Classifier
  8. knnClassifier.addExample(features, label);
  9. // Use KNN Classifier to classify these features
  10. knnClassifier.classify(features, (err, result) => {
  11. console.log(result); // result.label is the predicted label
  12. });

Usage

Initialize

  1. const knnClassifier = ml5.KNNClassifier();

Parameters

  • no inputs needed

Properties

Methods


.addExample()

Adding an example to a class.

  1. knnClassifier.addExample(example, indexOrLabel);

📥 Inputs

  • example: Required. An example to add to the dataset, usually an activation from another model
  • indexOrLabel: Required. String | Number. The class index(number) or label(string) of the example.

📤 Outputs

  • n/a


.classify()

Classify an new input.

  1. knnClassifier.classify(input, ?callback);
  2. // OR
  3. knnClassifier.classify(input, ?k, ?callback);

📥 Inputs

  • input: REQUIRED. An example to make a prediction on, could be an activation from another model or an array of numbers.
  • k: Optional. Number. The K value to use in K-nearest neighbors. The algorithm will first find the K nearest examples from those it was previously shown, and then choose the class that appears the most as the final prediction for the input example. Defaults to 3. If examples < k, k = examples.
  • callback: Optional. Function. A function to be called once the input has been classified. If no callback is provided, it will return a promise that will be resolved once the model has classified the new input.

📤 Outputs

  • Object: It returns an object with a top classIndex and label, confidences mapping all class indices to their confidence, and confidencesByLabel mapping all classes’ confidence by label.


.clearLabel()

Clears the specified label.

  1. knnClassifier.clearLabel(indexOrLabel);

📥 Inputs

  • input: REQUIRED. The class index or label, a number or a string.

📤 Outputs

  • n/a


.clearAllLabels()

Clear all examples in all labels

  1. knnClassifier.clearAllLabels();

📥 Inputs

  • n/a

📤 Outputs

  • n/a


.getCountByLabel()

Get the example count for each label. It returns an object that maps class label to example count for each class.

  1. knnClassifier.getCountByLabel();

📥 Inputs

  • n/a

📤 Outputs

  • n/a


.getCount()

Get the example count for each class. It returns an object that maps class index to example count for each class.

  1. knnClassifier.getCount();

📥 Inputs

  • n/a

📤 Outputs

  • n/a


.getNumLabels()

It returns the total number of labels.

  1. knnClassifier.getNumLabels();

📥 Inputs

  • n/a

📤 Outputs

  • n/a


.save()

Download the whole dataset as a JSON file. It’s useful for saving state.

  1. knnClassifier.save(?fileName);

📥 Inputs

  • fileName: Optional. The name of the JSON file that will be downloaded. e.g. “myKNN” or “myKNN.json”. If no fileName is provided, the default file name is “myKNN.json”.

📤 Outputs

  • Saved file


.load()

Load a dataset from a JSON file. It’s useful for restoring state.

  1. knnClassifier.load(path, ?callback);

📥 Inputs

  • path: The path for a valid JSON file.
  • callback: Optional. A function to run once the dataset has been loaded. If no callback is provided, it will return a promise that will be resolved once the dataset has loaded.

📤 Outputs

  • n/a

Examples

p5.js

p5 web editor

plain javascript

Demo

No demos yet - contribute one today!

Tutorials

KNN Classification Part 1 via CodingTrain

KNN Classification Part 2 via CodingTrain

KNN Classification Part 3 via CodingTrain

Acknowledgements

Contributors:

  • Yining Shi & Cristobal Valenzuela

Credits:

  • Paper Reference | Website URL | Github Repo | Book reference | etc

Source Code

/src/KNNClassifier/