-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.go
118 lines (103 loc) · 3.17 KB
/
kmeans.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Package kmeans implements the k-means clustering algorithm
// See: https://en.wikipedia.org/wiki/K-means_clustering
package kmeans
import (
"fmt"
"math/rand"
)
// Kmeans configuration/option struct
type Kmeans struct {
// when a plotter is set, Plot gets called after each iteration
plotter Plotter
// deltaThreshold (in percent between 0.0 and 0.1) aborts processing if
// less than n% of data points shifted clusters in the last iteration
deltaThreshold float64
// iterationThreshold aborts processing when the specified amount of
// algorithm iterations was reached
iterationThreshold int
}
// The Plotter interface lets you implement your own plotters
type Plotter interface {
Plot(cc Clusters, iteration int) error
Plot2(cc Clusters, iteration int) error
}
// NewWithOptions returns a Kmeans configuration struct with custom settings
func NewWithOptions(deltaThreshold float64, plotter Plotter) (Kmeans, error) {
if deltaThreshold <= 0.0 || deltaThreshold >= 1.0 {
return Kmeans{}, fmt.Errorf("threshold is out of bounds (must be >0.0 and <1.0, in percent)")
}
return Kmeans{
plotter: plotter,
deltaThreshold: deltaThreshold,
iterationThreshold: 96,
}, nil
}
// New returns a Kmeans configuration struct with default settings
func NewK() Kmeans {
m, _ := NewWithOptions(0.01, nil)
return m
}
// Partition executes the k-means algorithm on the given dataset and
// partitions it into k clusters
func (m Kmeans) Partition(dataset Observations, k int, seed int64) (Clusters, error) {
if k > len(dataset) {
return Clusters{}, fmt.Errorf("the size of the data set must at least equal k")
}
cc, err := New(k, dataset, seed)
if err != nil {
return cc, err
}
// fmt.Println(cc)
points := make([]int, len(dataset))
changes := 1
for i := 0; changes > 0; i++ {
changes = 0
cc.Reset()
for p, point := range dataset {
ci := cc.Nearest(point)
cc[ci].Append(point)
if points[p] != ci {
points[p] = ci
changes++
}
}
// fmt.Println(len(cc))
for ci := 0; ci < len(cc); ci++ {
if len(cc[ci].Observations) == 0 {
// During the iterations, if any of the cluster centers has no
// data points associated with it, assign a random data point (HERE, why random?)
// to it.
// Also see: http://user.ceng.metu.edu.tr/~tcan/ceng465_f1314/Schedule/KMeansEmpty.html
var ri int
for {
// find a cluster with at least two data points, otherwise
// we're just emptying one cluster to fill another
ri = rand.Intn(len(dataset))
if len(cc[points[ri]].Observations) > 1 {
break
}
}
cc[ci].Append(dataset[ri])
points[ri] = ci
// Ensure that we always see at least one more iteration after
// randomly assigning a data point to a cluster
changes = len(dataset)
}
}
if changes > 0 { //&& (isi cc<min && >max)
cc.Recenter()
}
if m.plotter != nil {
err := m.plotter.Plot(cc, i)
if err != nil {
return nil, fmt.Errorf("failed to plot chart: %s", err)
}
}
if i == m.iterationThreshold ||
changes < int(float64(len(dataset))*m.deltaThreshold) {
// fmt.Println("Aborting:", changes, int(float64(len(dataset))*m.TerminationThreshold))
break
}
}
return cc, nil
}