npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tf-kmeans

v0.0.3

Published

A Library for Calculating K-Means using Tensorflow

Downloads

19

Readme

TF-KMeans

Description

A Simple JavaScript Library to make it easy for people to use KMeans algorithms with Tensorflow JS.

The library was born out of another project in which except KMeans, our code completely depended on TF.JS

As such, moving to TF.JS helped standardise our code base substantially and reduce dependency on other libraries

Sample Code

	const KMeans = require("tf-kmeans");
	const tf = require("@tensorflow/tfjs");
	const kmeans = new KMeans.default({
		k: 2,
		maxIter: 30,
		distanceFunction: KMeans.default.EuclideanDistance
	});
	const dataset = tf.tensor([[2, 2, 2], [5, 5, 5], [3, 3, 3], [4, 4, 4], [7, 8, 7]]);
	const predictions = kmeans.Train(
		dataset
	);

	console.log("Assigned To ", predictions.arraySync());
	console.log("Centroids Used are ", kmeans.Centroids().arraySync());
	console.log("Prediction for Given Value is");
	kmeans.Predict(tf.tensor([2, 3, 2])).print();

You can use the Asynchronous TrainAsync if you want to use an asynchronous callback function

	const kmeans = new KMeans.default({
		k: 3,
		maxIter: 30,
		distanceFunction: KMeans.default.EuclideanDistance
	});
	const dataset = tf.tensor([[2, 2, 2], [5, 5, 5], [3, 3, 3], [4, 4, 4], [7, 8, 7]]);

	console.log("\n\nAsync Test");
	const predictions = await kmeans.TrainAsync(
		dataset,
		// Called At End of Every Iteration
		// This function is Asynchronous
		async(iter, centroid, preds)=>{
			console.log("===");
			console.log("Iteration Count", iter);
			console.log("Centroid ", await centroid.array());
			console.log("Prediction ", await preds.array());
			console.log("===");
			// You could instead use TFVIS for Plotting Here
		}
	);

Functions

  1. Constructor

    Takes 3 Optional parameters

    1. k:- Number of Clusters
    2. maxIter:- Max Iterations
    3. distanceFunction:- The Distance function Used Currently only Eucledian Distance Provided
  2. Train

    Takes Dataset as Parameter

    Performs Training on This Dataset

    Sync callback function is optional

  3. TrainAsync

    Takes Dataset as Parameter

    Performs Training on This Dataset

    Also takes async callback function called at the end of every iteration

  4. Centroids

    Returns the Centroids found for the dataset on which KMeans was Trained

  5. Predict

    Performs Predictions on the data Provided as Input

PEER DEPENDENCIES

  1. TensorFlow.JS

Typings

As the code is originally written in TypeScript, Type Support is provided out of the box

Contact Me

You could contact me via LinkedIn You could file issues or add features via Pull Requests on GitHub