Files
kmeans/examples/simple/main.go
Christian Muehlhaeuser af45c949f7 Use kmeans.Points in examples
2018-05-26 11:32:25 +02:00

31 lines
616 B
Go

package main
import (
"fmt"
"github.com/muesli/kmeans"
)
func main() {
// set up "random" data points (float64 values between 0.0 and 1.0)
km := kmeans.New()
var d kmeans.Points
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,
})
}
}
fmt.Printf("%d data points\n", len(d))
// Partition the data points into 4 clusters
clusters, _ := km.Partition(d, 4)
for i, c := range clusters {
fmt.Printf("Cluster: %d\n", i)
fmt.Printf("Centered at x: %.2f y: %.2f\n", c.Center[0]*255.0, c.Center[1]*255.0)
}
}