-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocs.go
188 lines (163 loc) · 4.08 KB
/
procs.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package kmeaaaaans
import (
"fmt"
"math"
"math/rand"
"sort"
"gonum.org/v1/gonum/mat"
)
type InitAlgorithm int
const (
KmeansPlusPlus InitAlgorithm = iota + 1
Random
)
func InitAlgorithmFrom(str string) (InitAlgorithm, error) {
switch str {
case "kmeans++":
return KmeansPlusPlus, nil
case "random":
return Random, nil
default:
return 0, fmt.Errorf("invalid init algorithm: %s", str)
}
}
func calcRandomInitialCentroids(X *mat.Dense, nClusters uint) *mat.Dense {
_, nFeatures := X.Dims()
centroids := mat.NewDense(int(nClusters), nFeatures, nil)
for i := 0; i < int(nClusters); i++ {
for j := 0; j < int(nFeatures); j++ {
centroids.Set(i, j, math.Abs(rand.NormFloat64()))
}
}
return centroids
}
func calcKmeansPlusPlusInitialCentroids(X *mat.Dense, nClusters uint) *mat.Dense {
nSamples, featDim := X.Dims()
centroids := mat.NewDense(int(nClusters), featDim, nil)
centroids.SetRow(0, X.RawRowView(rand.Intn(int(nSamples))))
for i := 1; i < int(nClusters); i++ {
accDistances := make([]float64, nSamples)
for j := 0; j < int(nSamples); j++ {
minDinstance := math.MaxFloat64
for k := 0; k < i; k++ {
distance := calcL2Distance(X.RawRowView(j), centroids.RawRowView(k))
minDinstance = math.Min(minDinstance, distance)
}
if j == 0 {
accDistances[j] = minDinstance
} else {
accDistances[j] = accDistances[j-1] + minDinstance
}
}
r := accDistances[nSamples-1] * rand.Float64()
j := sort.Search(int(nSamples), func(i int) bool { return accDistances[i] >= r })
centroids.SetRow(i, X.RawRowView(j))
}
return centroids
}
func calcInitialCentroids(X *mat.Dense, nClusters uint, initAlgorithm InitAlgorithm) *mat.Dense {
switch initAlgorithm {
case KmeansPlusPlus:
return calcKmeansPlusPlusInitialCentroids(X, nClusters)
case Random:
return calcRandomInitialCentroids(X, nClusters)
default:
panic("invalid init algorithm")
}
}
func assignCluster(X *mat.Dense, centroids *mat.Dense, classes []uint, indices []uint, calcDistance func(X, Y []float64) float64) float64 {
nClusters, _ := centroids.Dims()
inertia := 0.0
for _, i := range indices {
minDist := math.MaxFloat64
minClass := uint(0)
for j := 0; j < int(nClusters); j++ {
dist := calcDistance(X.RawRowView(int(i)), centroids.RawRowView(j))
if dist < minDist {
minDist = dist
minClass = uint(j)
}
}
inertia += minDist
classes[i] = minClass
}
return inertia
}
func accumulateSamples(X *mat.Dense, nextCentroids *mat.Dense, nSamplesInCluster []uint, classes []uint, indices []uint) {
nextCentroids.Zero()
for i := 0; i < len(nSamplesInCluster); i++ {
nSamplesInCluster[i] = 0
}
for _, i := range indices {
cluster := int(classes[i])
nSamplesInCluster[cluster]++
centroidData := nextCentroids.RawRowView(cluster)
featData := X.RawRowView(int(i))
for j := 0; j < len(featData); j++ {
centroidData[j] += featData[j]
}
}
}
func calcL2Distance(X, Y []float64) float64 {
acc := 0.0
i := 0
for ; i < len(X)%4; i++ {
diff := X[i] - Y[i]
acc += diff * diff
}
for ; i < len(X); i += 4 {
diff0 := X[i] - Y[i]
diff1 := X[i+1] - Y[i+1]
diff2 := X[i+2] - Y[i+2]
diff3 := X[i+3] - Y[i+3]
acc += diff0*diff0 + diff1*diff1 + diff2*diff2 + diff3*diff3
}
return math.Sqrt(acc)
}
func calcError(X, Y *mat.Dense) float64 {
return calcL2Distance(X.RawMatrix().Data, Y.RawMatrix().Data) / mat.Norm(X, 2)
}
func matPrint(X mat.Matrix) {
fa := mat.Formatted(X, mat.Prefix(""), mat.Squeeze())
fmt.Printf("%v\n", fa)
}
func makeSequence(max uint) []uint {
seq := make([]uint, max)
for i := range seq {
seq[i] = uint(i)
}
return seq
}
func minUint(a, b uint) uint {
if a < b {
return a
}
return b
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func maxUint(a, b uint) uint {
if a < b {
return b
}
return a
}
func maxInt(a, b int) int {
if a < b {
return b
}
return a
}
func makeChunks(seq []uint, chunkSize uint) [][]uint {
chunks := make([][]uint, 0)
for i := uint(0); i < uint(len(seq)); i += chunkSize {
beg := i
end := minUint(i+chunkSize, uint(len(seq)))
chunks = append(chunks, seq[beg:end])
}
return chunks
}