k_means_cluster 0.2.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

k-means-cluster #

A very simple implementation of k-means clustering.

Usage #

A clustering session typically involves:

  • Setting a distance measurement to use.
distanceMeasure = DistanceType.squaredEuclidian; // default
  • Creating a List of Instances. This is generally done by mapping a list of whatever data structures is available.
// For example, data might be a List<String> such
// that each String represents an individual instance.
List<Instance> instances = data.map((datum) {
  List<num> coordinates = ...;
  String id = ...;
  return Instance(coordinates, id: id); 
  • Creating a List of Clusters. This can be done manually (e.g. create a set of randomly placed clusters). A convenience function initialClusters exists that takes in the list of Instances already created and randomly generates clusters from the instances such that instances more distant to the previous cluster are more likely to seed the next cluster.
List<Cluster> clusters = initialClusters(3, instances, seed: 0);
  • Running the algorithm using the kmeans function. This is a side-effect heavy function that iteratively shifts the clusters towards the mean position of the associated instances and reassigns instances to the nearest cluster.
kmeans(clusters: clusters, instances: instances);
  • Inspecting the instances property of each cluster.
clusters.forEach((cluster) {
  cluster.instances.forEach((instance) {
    print("  - $instance");

Please see the associated wiki for more details and examples.

Please file feature requests and bugs at the issue tracker.

Changelog #

0.2.1 #

  • Dart 2 ready

0.1.0 #

  • Initial version.
  • Basic knn-clustering.
  • Distance measures: square Euclidian, city block.


import 'dart:io';
import 'package:k_means_cluster/k_means_cluster.dart';

main() async {
  // Load the data from iris.csv; ignore header-line;
  // each line representes an instance of an iris.
  List<String> lines =
      (await File("iris_data/iris.csv").readAsLines()).sublist(1);

  // Set the distance measure; this can be any function of the form
  // num f(List<num> a, List<num> b): a and b contain the coordinates
  // of two instances; f returns a numerical distance between the
  // points.
  distanceMeasure = DistanceType.squaredEuclidian;

  // Create the list of instances.
  List<Instance> instances = lines.map((String line) {
    List<String> datum = line.split(",");

    // The first four columns contain the coordinates.
    List<num> location =
        datum.sublist(0, 4).map((String x) => num.parse(x)).toList();

    // The fifth column contains the species.
    String id = datum[4];

    return Instance(location, id: id);

  // Randomly create the initial clusters.
  List<Cluster> clusters = initialClusters(3, instances, seed: 0);

  // Run the algorithm.
  var info = kmeans(clusters: clusters, instances: instances);

  // See the final cluster results.
  clusters.forEach((cluster) {
    cluster.instances.forEach((iris) {
      print("  - $iris");

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  k_means_cluster: ^0.2.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:k_means_cluster/k_means_cluster.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Health suggestions

Fix lib/src/k_means_cluster_base.dart. (-0.50 points)

Analysis of lib/src/k_means_cluster_base.dart reported 1 hint:

line 146 col 9: DO use curly braces for all flow control structures.

Maintenance suggestions

Package is getting outdated. (-19.73 points)

The package was last published 62 weeks ago.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0