Kmeans Clustering

groups of points colored depending on which cluster it has been categorized into by the kmeans algorithm

Description

The KMeans clustering algorithm. Read more about it here

Quickstart

  1. const data = [{ x: 0, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 0 }, { x: 1, y: 1 }];
  2. const options = {
  3. k: 3,
  4. maxIter: 4,
  5. threshold: 0.5,
  6. };
  7. // Initialize the magicFeature
  8. const kmeans = ml5.kmeans(data, options, clustersCalculated);
  9. // When the model is loaded
  10. function clustersCalculated() {
  11. console.log('Points Clustered!');
  12. console.log(kmeans.dataset);
  13. }

Usage

Initialize

  1. const kmeans = ml5.kmeans(data, ?options, ?callback);

Parameters

  • data: REQUIRED. JSON object | Data URL. Can be a CSV or JSON dataset. Your data might look like:

    • csv:

      1. x1, y1
      2. 1, 2
      3. 3, 4
      4. 5, 6
    • json:

      1. [{ x: 0, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 0 }, { x: 1, y: 1 }]
  • options: OPTIONAL. Sets the options including:

    • k: the number of clusters
    • maxIter: Max number of iterations to try before forcing convergence.
    • threshold: Threshold for updated centriod distance before declaring convergence.
  • callback: OPTIONAL. A callback function that is called once the kmeans clusters have been calculated.

Properties


.config

Object: object containing the configuration of the kmeans



.dataset

Array: an array of objects containing the original data where each object is a “row” of data with a property called centroid indicating which cluster this point belongs to.



.dataTensor

Tensor: an tensorflow tensor representing the .dataset property



.centroids

Tensor: an tensorflow tensor representing the .centroids


Methods

  • The ml5.kmeans() calculates the kmeans clusters of the input data. See usage above.

Examples

p5.js

p5 web editor

plain javascript

  • coming soon

d3.js

Demo

No demos yet - contribute one today!

Tutorials

No tutorials yet - contribute one today!

Acknowledgements

Contributors:

Credits:

  • Paper Reference | Website URL | Github Repo | Book reference | etc

Source Code