mirror of
https://github.com/muesli/kmeans.git
synced 2025-09-26 19:51:27 +08:00
Use random data points in the simple example and README
This commit is contained in:
19
README.md
19
README.md
@@ -15,25 +15,22 @@ to the cluster with the nearest mean, serving as a prototype of the cluster.
|
||||
```go
|
||||
import "github.com/muesli/kmeans"
|
||||
|
||||
km := kmeans.New()
|
||||
|
||||
// set up "random" data points (float64 values between 0.0 and 1.0)
|
||||
// set up a random two-dimensional data set (float64 values between 0.0 and 1.0)
|
||||
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,
|
||||
})
|
||||
}
|
||||
for x := 0; x < 1024; x++ {
|
||||
d = append(d, kmeans.Point{
|
||||
rand.Float64(),
|
||||
rand.Float64(),
|
||||
})
|
||||
}
|
||||
|
||||
// Partition the data points into 16 clusters
|
||||
km := kmeans.New()
|
||||
clusters, err := km.Partition(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)
|
||||
fmt.Printf("Matching data points: %+v\n\n", c.Points)
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -2,26 +2,28 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/muesli/kmeans"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// set up "random" data points (float64 values between 0.0 and 1.0)
|
||||
km := kmeans.New()
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// set up a random two-dimensional data set (float64 values between 0.0 and 1.0)
|
||||
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,
|
||||
})
|
||||
}
|
||||
for x := 0; x < 1024; x++ {
|
||||
d = append(d, kmeans.Point{
|
||||
rand.Float64(),
|
||||
rand.Float64(),
|
||||
})
|
||||
}
|
||||
fmt.Printf("%d data points\n", len(d))
|
||||
|
||||
// Partition the data points into 4 clusters
|
||||
clusters, _ := km.Partition(d, 4)
|
||||
// Partition the data points into 7 clusters
|
||||
km := kmeans.New()
|
||||
clusters, _ := km.Partition(d, 7)
|
||||
|
||||
for i, c := range clusters {
|
||||
fmt.Printf("Cluster: %d\n", i)
|
||||
|
Reference in New Issue
Block a user