kmeans/README.md
Christian Muehlhaeuser c8582d6269
Fixed simple example
2018-05-26 07:36:22 +02:00

1.2 KiB

kmeans

k-means clustering algorithm written in Go

What It Does

k-means clustering partitions an n-dimensional data set into k clusters, where each data point belongs to the cluster with the nearest mean, serving as a prototype of the cluster.

kmeans animation

Example

import "github.com/muesli/kmeans"

km := kmeans.New()

// set up a 2d grid with "random" data points between 0.0 and 1.0
d := []kmeans.Point{}
for x := 0; x < 255; x += 4 {
	for y := 0; y < 255; y += 4 {
		d = append(d, kmeans.Point{
			float64(x) / 255.0,
			float64(y) / 255.0,
		})
	}
}

// Partition the data points into 16 clusters
clusters, err := km.Run(d, 16)

for _, c := range clusters {
	fmt.Printf("Centered at x: %.2f y: %.2f\n", c.Center[0]*255.0, c.Center[1]*255.0)
	fmt.Printf("Points: %+v\n\n", c.Points)
}

Development

GoDoc Build Status Go ReportCard