Clone wiki

k_means_cluster / Home

k-means-cluster

Home | Example | About

Welcome to k-means-cluster, a simple implementation of the k-means clustering algorithm for Dart!

Basic usage

Performing a k-means clustering is usually done using the following steps:

  • Choosing a distance measure.
distanceMeasure = DistanceType.squaredEuclidian;
  • Creating a List<Instance>. This is usually done by mapping from an arbitrary data source.
// for example, data could be a list of Strings such that
// each String represents a data point
List<Instance> instances = data.map((String datum) {
    List<num> coordinates = ...;
    String id = ...;

    return Instance(coordinates, id: id);
  }).toList();
  • Creating a List<Cluster>. This can be done manually (e.g. if we want to create a list of initially randomly positioned clusters). A convenience function initialClusters exists that produces a list of clusters based on the positions of a list of instances. (This function makes an effort to produce a good initial cluster spread by using the instance positions as potential cluster positions such that the probability that an instance will be used is proportional to the square distance from the previous cluster.)
// produce a list of 3 clusters based on the instances
// above; we can set the random generator's seed for
// reproducibility
List<Cluster> clusters = initialClusters(3, instances, seed: 0);
  • Run the algorithm. There is a side-effect heavy function kmeans available that iteratively shifts each cluster to the mean position of its associated instances and then associates each instance with the nearest cluster.
kmeans(clusters: clusters, instances: instances);
  • Inspect the instances property of each cluster.
for (final cluster in clusters) {
    print(cluster.id);
    for (final instance in cluster.instances) {
        print("  - $instance");
    }
}

Updated