mirror of
https://github.com/muesli/kmeans.git
synced 2025-10-04 07:16:29 +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
|
```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)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -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)
|
||||||
|
Reference in New Issue
Block a user