-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster.go
94 lines (81 loc) · 1.52 KB
/
cluster.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
package qrpie
import (
"math"
"sort"
)
type cluster struct {
heaps [][]point
}
func NewCluster() *cluster {
return &cluster{
heaps: make([][]point, 0, 10),
}
}
func (c *cluster) add(p point) {
dis := math.MaxFloat64
for i, ps := range c.heaps {
for _, p1 := range ps {
if distant(p1, p) <= 2 {
c.heaps[i] = append(c.heaps[i], p)
return
}
if distant(p1, p) < dis {
dis = distant(p1, p)
}
}
}
c.heaps = append(c.heaps, []point{p})
}
func (c *cluster) Less(i, j int) bool {
if len(c.heaps[i]) < len(c.heaps[j]) {
return true
} else {
return false
}
}
func (c *cluster) Len() int {
return len(c.heaps)
}
func (c *cluster) Swap(i, j int) {
buf := c.heaps[i]
c.heaps[i] = c.heaps[j]
c.heaps[j] = buf
}
func (c *cluster) getMaxThreeHeapCenter() []point {
if len(c.heaps) < 3 {
return nil
}
centers := make([]point, 3, 3)
sort.Sort(c)
for i := 0; i < 3; i++ {
index := len(c.heaps) - i - 1
centers[i] = mean(c.heaps[index])
}
return centers
}
func (c *cluster) getLenth() int {
return len(c.heaps)
}
func (c *cluster) getVariance() float64 {
if c.getLenth() == 0 {
return 0
}
sum := 0
for _, ps := range c.heaps {
sum = sum + len(ps)
}
mean := float64(sum) / float64(c.getLenth())
v := 0.0
for _, ps := range c.heaps {
v = v + math.Pow(float64(len(ps))-mean, 2)
}
return v / float64(c.getLenth())
}
func mean(points []point) point {
sum := point{0, 0}
l := float64(len(points))
for _, p := range points {
sum = pointAdd(sum, p)
}
return point{sum.x / l, sum.y / l}
}