Giter Site home page Giter Site logo

pratikpc / tf-kmeans Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 3.0 16 KB

A Tensorflow.JS Library for Calculating KMeans

Home Page: https://www.npmjs.com/package/tf-kmeans

License: MIT License

TypeScript 100.00%
kmeans-clustering kmeans tf tensorflow tensorflowjs tfjs npm-package

tf-kmeans's Introduction

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

	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

tf-kmeans's People

Contributors

pratikpc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.