@jzx/knn-classifier (0.1.5)

Published 2025-01-19 11:34:30 +00:00 by devlifestyle

Installation

@jzx:registry=
npm install @jzx/knn-classifier@0.1.5
"@jzx/knn-classifier": "0.1.5"

About this package

KNN Classifier

This package provides a utility for creating a classifier using the K-Nearest Neighbors algorithm.

This package is different from the other packages in this repository in that it doesn't provide a model with weights, but rather a utility for constructing a KNN model using activations from another model or any other tensors you can associate with a class/label.

Usage example

via NPM
const tf = require('@tensorflow/tfjs');
const mobilenetModule = require('@tensorflow-models/mobilenet');
const knnClassifier = require('@tensorflow-models/knn-classifier');

// Create the classifier.
const classifier = knnClassifier.create();

// Load mobilenet.
const mobilenet = await mobilenetModule.load();

// Add MobileNet activations to the model repeatedly for all classes.
const img0 = tf.browser.fromPixels(document.getElementById('class0'));
const logits0 = mobilenet.infer(img0, true);
classifier.addExample(logits0, 0);

const img1 = tf.browser.fromPixels(document.getElementById('class1'));
const logits1 = mobilenet.infer(img1, true);
classifier.addExample(logits1, 1);

// Make a prediction.
const x = tf.browser.fromPixels(document.getElementById('test'));
const xlogits = mobilenet.infer(x, true);
console.log('Predictions:');
console.log(classifier.predictClass(xlogits));

API

Creating a classifier

knnClassifier is the module name, which is automatically included when you use the

Dependencies

Dependencies

ID Version
@jzx/knn-classifier workspace:^
@tensorflow-models/mobilenet 2.1.0
@tensorflow/tfjs-backend-webgl ^4.22.0

Development Dependencies

ID Version
@rollup/plugin-node-resolve ^11.1.0
@rollup/plugin-typescript 3.1.1
@tensorflow/tfjs-backend-cpu ^4.22.0
@tensorflow/tfjs-core ^4.22.0
@types/jasmine ^3.6.3
babel-core ~6.26.0
babel-plugin-transform-runtime ~6.23.0
jasmine ^3.6.4
jasmine-core ~3.1.0
rimraf ~2.6.2
rollup ~2.38.0
rollup-plugin-uglify ~3.0.0
ts-node ~5.0.0
tslint ~5.18.0
typescript ~5.1.6

Peer Dependencies

ID Version
@tensorflow/tfjs-backend-cpu ^4.22.0
@tensorflow/tfjs-core ^4.22.0

Keywords

tensorflow tensorflow.js knn-classifier
Details
npm
2025-01-19 11:34:30 +00:00
2
devlifestyle
ISC
latest
17 KiB
Assets (1)
Versions (5) View all
0.1.5 2025-01-19
0.1.4 2025-01-19
0.1.3 2025-01-19
0.1.2 2025-01-19
0.1.1 2025-01-18