kmeans-ts
v1.0.4
Published
A fast, efficient k-means clustering implementation in TypeScript
Downloads
854
Maintainers
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
- Fork K-Means-TS here
- Create a feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
Meta
Adapted from @Solzimer's Skmeans-JS by @GoldinGuy
Distributed under the MIT license. See LICENSE for more information.