Use random data points in the simple example and README

This commit is contained in:
Christian Muehlhaeuser
2018-05-28 01:05:38 +02:00
parent 9ddd17af8f
commit ba34fac784
2 changed files with 21 additions and 22 deletions

View File

@@ -15,25 +15,22 @@ to the cluster with the nearest mean, serving as a prototype of the cluster.
```go ```go
import "github.com/muesli/kmeans" import "github.com/muesli/kmeans"
km := kmeans.New() // set up a random two-dimensional data set (float64 values between 0.0 and 1.0)
// set up "random" data points (float64 values between 0.0 and 1.0)
var d kmeans.Points var d kmeans.Points
for x := 0; x < 255; x += 4 { for x := 0; x < 1024; x++ {
for y := 0; y < 255; y += 4 { d = append(d, kmeans.Point{
d = append(d, kmeans.Point{ rand.Float64(),
float64(x) / 255.0, rand.Float64(),
float64(y) / 255.0, })
})
}
} }
// Partition the data points into 16 clusters // Partition the data points into 16 clusters
km := kmeans.New()
clusters, err := km.Partition(d, 16) clusters, err := km.Partition(d, 16)
for _, c := range clusters { 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("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) fmt.Printf("Matching data points: %+v\n\n", c.Points)
} }
``` ```

View File

@@ -2,26 +2,28 @@ package main
import ( import (
"fmt" "fmt"
"math/rand"
"time"
"github.com/muesli/kmeans" "github.com/muesli/kmeans"
) )
func main() { func main() {
// set up "random" data points (float64 values between 0.0 and 1.0) rand.Seed(time.Now().UnixNano())
km := kmeans.New()
// set up a random two-dimensional data set (float64 values between 0.0 and 1.0)
var d kmeans.Points var d kmeans.Points
for x := 0; x < 255; x += 4 { for x := 0; x < 1024; x++ {
for y := 0; y < 255; y += 4 { d = append(d, kmeans.Point{
d = append(d, kmeans.Point{ rand.Float64(),
float64(x) / 255.0, rand.Float64(),
float64(y) / 255.0, })
})
}
} }
fmt.Printf("%d data points\n", len(d)) fmt.Printf("%d data points\n", len(d))
// Partition the data points into 4 clusters // Partition the data points into 7 clusters
clusters, _ := km.Partition(d, 4) km := kmeans.New()
clusters, _ := km.Partition(d, 7)
for i, c := range clusters { for i, c := range clusters {
fmt.Printf("Cluster: %d\n", i) fmt.Printf("Cluster: %d\n", i)