Giter Club home page Giter Club logo

k-means-ts's Introduction

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.

k-means-ts's People

Contributors

goldinguy avatar bramtweedegolf avatar

Watchers

James Cloos 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.