mirror of
https://github.com/muesli/kmeans.git
synced 2024-05-25 10:23:35 +08:00
1.2 KiB
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.
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)
}