用 Go 编写的 k-means 聚类算法实现
Go to file
2018-05-26 07:53:26 +02:00
examples Omit error handling in examples 2018-05-26 07:53:26 +02:00
.gitignore Initial commit 2018-05-26 06:00:20 +02:00
.travis.yml Go 1.3 is required 2018-05-26 06:42:40 +02:00
cluster.go Initial import 2018-05-26 06:17:36 +02:00
draw.go Initial import 2018-05-26 06:17:36 +02:00
kmeans.gif Updated README and added animation 2018-05-26 06:38:24 +02:00
kmeans.go Initialize Kmeans settings with either New or NewWithOptions 2018-05-26 07:26:26 +02:00
LICENSE Initial commit 2018-05-26 06:00:20 +02:00
point.go Be more precise what a data point is 2018-05-26 07:39:00 +02:00
README.md Enable syntax highlighting in README 2018-05-26 07:51:58 +02:00

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.

kmeans animation

Example

import "github.com/muesli/kmeans"

km := kmeans.New()

// set up "random" data points (float64 values 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)
}

Development

GoDoc Build Status Go ReportCard