KNNClassifier: K-Nearest Neighbors Classifier
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
// Create a KNN classifier
const knnClassifier = ml5.KNNClassifier();
// Create a featureExtractor that can extract features of an image
const featureExtractor = ml5.featureExtractor('MobileNet', modelReady);
// Get the features of an image
const features = featureExtractor.infer(myImg);
// Add an example with a label to the KNN Classifier
knnClassifier.addExample(features, label);
// Use KNN Classifier to classify these features
knnClassifier.classify(features, (err, result) => {
console.log(result); // result.label is the predicted label
});
Usage
Initialize
const knnClassifier = ml5.KNNClassifier();
Parameters
- no inputs needed
Properties
Methods
.addExample()
Adding an example to a class.
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.
knnClassifier.classify(input, ?callback);
// OR
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.
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
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.
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.
knnClassifier.getCount();
📥 Inputs
- n/a
📤 Outputs
- n/a
.getNumLabels()
It returns the total number of labels.
knnClassifier.getNumLabels();
📥 Inputs
- n/a
📤 Outputs
- n/a
.save()
Download the whole dataset as a JSON file. It’s useful for saving state.
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.
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
- KNNClassification_PoseNet
- KNNClassification_Video
- KNNClassification_VideoSound
- KNNClassification_VideoSquare
p5 web editor
- KNNClassification_PoseNet
- KNNClassification_Video
- KNNClassification_VideoSound
- KNNClassification_VideoSquare
plain javascript
- KNNClassification_PoseNet
- KNNClassification_Video
- KNNClassification_VideoSound
- KNNClassification_VideoSquare
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