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

kmeans-ts

v1.0.4

Published

A fast, efficient k-means clustering implementation in TypeScript

Downloads

427

Readme

K-Means-TS

💹 K-means and k-means++ clustering implementation. A Typescript rewrite of Skmeans-JS

Quick Start

Installation

npm i kmeans-ts

Importation

import kmeans from "kmeans-ts";

If you want to access the interfaces or utilities within the package, use

import { KMeans, Vectors, Utils } from "kmeans-ts";

Implementation

var input_data: Array<Array<number>> = [
	[1, 12, 14, 4, 25, 35, 22, 3, 14, 5, 51, 2, 23, 24, 15],
	[7, 34, 15, 34, 17, 11, 34, 2, 35, 18, 52, 34, 33, 21],
	[5, 19, 35, 17, 35, 18, 12, 45, 23, 56, 23, 45, 16, 3]
];
var output: Array<Array<number>> = kmeans(input_data, 3, "kmeans");

Returns

{
	"iterations": 1,
	"k": 3,
	"indexes": [2,1,0],
	"centroids": [
		[5,19,35,17,35,18,12,45,23,56,23,45,16,3,0],
		[7,34,15,34,17,11,34,2,35,18,52,34,33,21,0],
		[1,12,14,4,25,35,22,3,14,5,51,2,23,24,15]
	]
}

Functionality & Params

| Param | Description | Sample Type | Required | | ------------ | ------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------- | -------- | | Input Data | Array of values to be clustered. Can be multi-dimensional | Array<number>, Array<Array<number>> | Yes | | K | Num clusters | number | Yes | | Centroids | Initializes centroids. Kmeans for random, Kmeans++ for the K-means++ algorithm. Will attempt to find them if not provided. | String | Optional | | Iterations | Max num of iterations. Default is 10000 | number | Optional |

Returns the following object:

| Return value | Description | Sample type | | ------------ | ----------------------------------------------- | ---------------------- | | Iterations | Num iterations undergone | number | | K | Num clusters | number | | Centroids | Centroid values for each cluster | Array<number> | | Indexes | Index of centroid for each value of input array | Array<Array<number>> |

Further Examples

// K-means w/ 4 clusters & random centroid initialization
var kmeans: KMeans = kmeans(input_data, 4, "kmeans");

// K-means w/ 3 clusters & initial centroids included
var kmeans: KMeans = kmeans(input_data, 3, [
	[3, 1, 5],
	[7, 2, 6],
	[3, 8, 6]
]);

// K-means++ w/ 5 clusters
var kmeans: KMeans = kmeans(input_data, 5, "kmeans++");

// K-means w/ 7 clusters, random centroids, and 15 max iterations
var kmeans: KMeans = kmeans(input_data, 7, null, 15);

K-Means-TS can be seen in MTG-Meta-TS

Development Setup

Simply clone the repository, then if you would like to generate a new ts-config run

--ts-config init

This will create a tsconfig.json file. If you are using VSCode, enter Ctrl-Shift-B and then tsc:watch, which will auto-compile TS to JS. You can also use tsc <filename> to compile from ts to js.

This project uses tsdx for compilation and minification. You can run that with npm start

To test this project, you can navigate to /example and run the testing ground with either ts-node testing_ground.ts, or by compiling it to JS and then running it in the terminal with node testing_ground.js

Alternatively, you can install the awesome VSCode extension Code Runner, which is very convenient

Contributing

  1. Fork K-Means-TS here
  2. Create a feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Meta

Adapted from @Solzimer's Skmeans-JS by @GoldinGuy

Distributed under the MIT license. See LICENSE for more information.