{"id":23236737,"url":"https://github.com/pratikpc/tf-kmeans","last_synced_at":"2025-10-09T18:12:46.122Z","repository":{"id":57376899,"uuid":"255870566","full_name":"pratikpc/tf-kmeans","owner":"pratikpc","description":"A Tensorflow.JS Library for Calculating KMeans","archived":false,"fork":false,"pushed_at":"2020-04-23T20:15:18.000Z","size":16,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-01T19:07:42.407Z","etag":null,"topics":["kmeans","kmeans-clustering","npm-package","tensorflow","tensorflowjs","tf","tfjs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/tf-kmeans","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pratikpc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-15T09:38:47.000Z","updated_at":"2024-09-11T06:39:26.000Z","dependencies_parsed_at":"2022-09-26T17:00:35.277Z","dependency_job_id":null,"html_url":"https://github.com/pratikpc/tf-kmeans","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pratikpc/tf-kmeans","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratikpc%2Ftf-kmeans","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratikpc%2Ftf-kmeans/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratikpc%2Ftf-kmeans/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratikpc%2Ftf-kmeans/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pratikpc","download_url":"https://codeload.github.com/pratikpc/tf-kmeans/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratikpc%2Ftf-kmeans/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268660071,"owners_count":24286009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kmeans","kmeans-clustering","npm-package","tensorflow","tensorflowjs","tf","tfjs"],"created_at":"2024-12-19T04:12:36.236Z","updated_at":"2025-10-09T18:12:41.103Z","avatar_url":"https://github.com/pratikpc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [TF-KMeans](https://github.com/pratikpc/TF-KMeans)\n\n## Description\n\nA Simple JavaScript Library to make it easy for people to use KMeans algorithms with Tensorflow JS.\n\nThe library was born out of another project in which except KMeans, our code completely depended on TF.JS\n\nAs such, moving to TF.JS helped standardise our code base substantially and reduce dependency on other libraries\n\n## [Sample Code](samples/index.js)\n\n~~~javascript\n\tconst KMeans = require(\"tf-kmeans\");\n\tconst tf = require(\"@tensorflow/tfjs\");\n\tconst kmeans = new KMeans.default({\n\t\tk: 2,\n\t\tmaxIter: 30,\n\t\tdistanceFunction: KMeans.default.EuclideanDistance\n\t});\n\tconst dataset = tf.tensor([[2, 2, 2], [5, 5, 5], [3, 3, 3], [4, 4, 4], [7, 8, 7]]);\n\tconst predictions = kmeans.Train(\n\t\tdataset\n\t);\n\n\tconsole.log(\"Assigned To \", predictions.arraySync());\n\tconsole.log(\"Centroids Used are \", kmeans.Centroids().arraySync());\n\tconsole.log(\"Prediction for Given Value is\");\n\tkmeans.Predict(tf.tensor([2, 3, 2])).print();\n~~~\n\nYou can use the Asynchronous TrainAsync if you want to use an asynchronous callback function\n\n~~~javascript\n\tconst kmeans = new KMeans.default({\n\t\tk: 3,\n\t\tmaxIter: 30,\n\t\tdistanceFunction: KMeans.default.EuclideanDistance\n\t});\n\tconst dataset = tf.tensor([[2, 2, 2], [5, 5, 5], [3, 3, 3], [4, 4, 4], [7, 8, 7]]);\n\n\tconsole.log(\"\\n\\nAsync Test\");\n\tconst predictions = await kmeans.TrainAsync(\n\t\tdataset,\n\t\t// Called At End of Every Iteration\n\t\t// This function is Asynchronous\n\t\tasync(iter, centroid, preds)=\u003e{\n\t\t\tconsole.log(\"===\");\n\t\t\tconsole.log(\"Iteration Count\", iter);\n\t\t\tconsole.log(\"Centroid \", await centroid.array());\n\t\t\tconsole.log(\"Prediction \", await preds.array());\n\t\t\tconsole.log(\"===\");\n\t\t\t// You could instead use TFVIS for Plotting Here\n\t\t}\n\t);\n~~~\n\n## Functions\n\n1. ***`Constructor`***\n\n    Takes 3 Optional parameters\n    1. k:-                Number of Clusters\n    2. maxIter:-          Max Iterations\n    3. distanceFunction:- The Distance function Used\n\t                      Currently only Eucledian Distance Provided\n\n2. ***`Train`***\n\n    Takes Dataset as Parameter\n\n\tPerforms Training on This Dataset\n\n\tSync callback function is _optional_\n\n3. ***`TrainAsync`***\n\n    Takes Dataset as Parameter\n\n\tPerforms Training on This Dataset\n\n\tAlso takes _async_ callback function called at the end of every iteration\n\n4. ***`Centroids`***\n\n\tReturns the Centroids found for the dataset on which KMeans was Trained\n\n5. ***`Predict`***\n\n\tPerforms Predictions on the data Provided as Input\n\n## PEER DEPENDENCIES\n1. [TensorFlow.JS](https://www.tensorflow.org/js \"tfjs\")\n\n## Typings\nAs the code is originally written in TypeScript, Type Support is provided out of the box\n\n## Contact Me\nYou could contact me [via LinkedIn](https://www.linkedin.com/in/pratik-chowdhury-889bb2183/ \"via LinkedIn\")\nYou could file issues or add features via Pull Requests on GitHub\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpratikpc%2Ftf-kmeans","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpratikpc%2Ftf-kmeans","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpratikpc%2Ftf-kmeans/lists"}