forked from bobhancock/goxmeans
/
km.go
555 lines (486 loc) · 18.2 KB
/
km.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
Package goxmeans implements a library for the xmeans algorithm.
See Dan Pelleg and Andrew Moore - X-means: Extending K-means with Efficient Estimation of the Number of Clusters.
*/
package goxmeans
import (
"bufio"
"errors"
"fmt"
"io"
"math"
"math/rand"
"os"
"strconv"
"strings"
"runtime"
"log"
// "code.google.com/p/gomatrix/matrix"
"github.com/bobhancock/gomatrix/matrix"
"goxmeans/matutil"
)
var numworkers = runtime.NumCPU()
//var numworkers = 1
// minimum returns the smallest int.
func minimum(x int, ys ...int) int {
for _, y := range ys {
if y < x {
x = y
}
}
return x
}
// Atof64 is shorthand for ParseFloat(s, 64)
func Atof64(s string) (f float64, err error) {
f64, err := strconv.ParseFloat(s, 64)
return float64(f64), err
}
type CentroidChooser interface {
ChooseCentroids(mat *matrix.DenseMatrix, k int) *matrix.DenseMatrix
}
// RandCentroids picks k uniformly distributed points from within the bounds of the dataset
type RandCentroids struct {}
// DataCentroids picks k distinct points from the dataset
type DataCentroids struct {}
// EllipseCentroids lays out the centroids along an elipse inscribed within the boundaries of the dataset
type EllipseCentroids struct {
frac float64 // must be btw 0 and 1, this will be what fraction of a truly inscribing ellipse this is
}
// Load loads a tab delimited text file of floats into a slice.
// Assume last column is the target.
// For now, we limit ourselves to two columns
func Load(fname string) (*matrix.DenseMatrix, error) {
datamatrix := matrix.Zeros(1, 1)
data := make([]float64, 2048)
idx := 0
fp, err := os.Open(fname)
if err != nil {
return datamatrix, err
}
defer fp.Close()
r := bufio.NewReader(fp)
linenum := 1
eof := false
for !eof {
var line string
var buf []byte
// line, err := r.ReadString('\n')
buf , _, err := r.ReadLine()
line = string(buf)
// fmt.Printf("linenum=%d buf=%v line=%v\n",linenum,buf, line)
if err == io.EOF {
err = nil
eof = true
break
} else if err != nil {
return datamatrix, errors.New(fmt.Sprintf("means.Load: reading linenum %d: %v", linenum, err))
}
linenum++
l1 := strings.TrimRight(line, "\n")
l := strings.Split(l1, "\t")
if len(l) < 2 {
return datamatrix, errors.New(fmt.Sprintf("means.Load: linenum %d has only %d elements", linenum, len(line)))
}
// for now assume 2 dimensions only
f0, err := Atof64(string(l[0]))
if err != nil {
return datamatrix, errors.New(fmt.Sprintf("means.Load: cannot convert f0 %s to float64.", l[0]))
}
f1, err := Atof64(string(l[1]))
if err != nil {
return datamatrix, errors.New(fmt.Sprintf("means.Load: cannot convert f1 %s to float64.",l[1]))
}
if linenum >= len(data) {
data = append(data, f0, f1)
} else {
data[idx] = f0
idx++
data[idx] = f1
idx++
}
}
numcols := 2
datamatrix = matrix.MakeDenseMatrix(data, linenum - 1, numcols)
return datamatrix, nil
}
// chooseCentroids picks random centroids based on the min and max values in the matrix
// and return a k by cols matrix of the centroids.
func (c RandCentroids) ChooseCentroids(mat *matrix.DenseMatrix, k int) *matrix.DenseMatrix {
_, cols := mat.GetSize()
centroids := matrix.Zeros(k, cols)
for colnum := 0; colnum < cols; colnum++ {
r := mat.ColSlice(colnum)
minj := float64(0)
// min value from column
for _, val := range r {
minj = math.Min(minj, val)
}
// max value from column
maxj := float64(0)
for _, val := range r {
maxj = math.Max(maxj, val)
}
// create a slice of random centroids
// based on maxj + minJ * random num to stay in range
for h := 0; h < k; h++ {
randInRange := ((maxj - minj) * rand.Float64()) + minj
centroids.Set(h, colnum, randInRange)
}
}
return centroids
}
// Needs comments
func (c DataCentroids) ChooseCentroids(mat *matrix.DenseMatrix, k int) (*matrix.DenseMatrix, error) {
// first set up a map to keep track of which data points have already been chosen so we don't dupe
rows, cols := mat.GetSize()
centroids := matrix.Zeros(k, cols)
if k > rows {
return centroids, errors.New("ChooseCentroids: Can't compute more centroids than data points!")
}
chosenIdxs := make(map [int]bool, k)
for len(chosenIdxs) < k {
index := rand.Intn(rows)
chosenIdxs[index] = true
}
i := 0
for idx, _ := range chosenIdxs {
centroids.SetRowVector(mat.GetRowVector(idx).Copy(), i)
i += 1
}
return centroids, nil
}
// Needs comments
func (c EllipseCentroids) ChooseCentroids(mat *matrix.DenseMatrix, k int) *matrix.DenseMatrix {
_, cols := mat.GetSize()
var xmin, xmax, ymin, ymax = matutil.GetBoundaries(mat)
x0, y0 := xmin + (xmax - xmin)/2.0, ymin + (ymax-ymin)/2.0
centroids := matrix.Zeros(k, cols)
rx, ry := xmax - x0, ymax - y0
thetaInit := rand.Float64() * math.Pi
for i := 0; i < k; i++ {
centroids.Set(i, 0, rx * c.frac * math.Cos(thetaInit + float64(i) * math.Pi / float64(k)))
centroids.Set(i, 1, ry * c.frac * math.Sin(thetaInit + float64(i) * math.Pi / float64(k)))
}
return centroids
}
// ComputeCentroids Needs comments.
func ComputeCentroid(mat *matrix.DenseMatrix) (*matrix.DenseMatrix, error) {
rows, _ := mat.GetSize()
vectorSum := mat.SumCols()
if rows == 0 {
return vectorSum, errors.New("No points inputted")
}
vectorSum.Scale(1.0 / float64(rows))
return vectorSum, nil
}
// Kmeansp returns means and distance squared of the coordinates for each
// centroid using parallel computation.
//
// Input values
//
// datapoints - a kX2 matrix of R^2 coordinates
//
// centroids - a kX2 matrix of R^2 coordinates for centroids.
//
// measurer - anythng that implements the matutil.VectorMeasurer interface to
// calculate the distance between a centroid and datapoint. (e.g., Euclidian
// distance)
//
// Return values
//
// centroidMean - a kX2 matrix where the row number corresponds to the same
// row in the centroid matrix and the two columns are the means of the
// coordinates for that cluster. i.e., the best centroids that could
// be determined.
//
// ____ ______
// | 12.29 32.94 | <-- The mean of coordinates for centroid 0
// | 4.6 29.22 | <-- The mean of coordinates for centroid 1
// |_____ ______|
//
//
// centroidSqErr - a kX2 matrix where the first column contains a number
// indicating the centroid and the second column contains the minimum
// distance between centroid and point squared. (i.e., the squared error)
//
// ____ _______
// | 0 38.01 | <-- Centroid 0, squared error for the coordinates in row 0 of datapoints
// | 1 23 .21| <-- Centroid 1, squared error for the coordinates in row 1 of datapoints
// | 0 14.12 | <-- Centroid 0, squared error for the coordinates in row 2 of datapoints
// _____ _______
//func Kmeansp(datapoints, centroids *matrix.DenseMatrix, measurer matutil.VectorMeasurer) (centroidMean,
func Kmeansp(datapoints *matrix.DenseMatrix, k int,cc CentroidChooser, measurer matutil.VectorMeasurer) (centroidMean,
centroidSqErr *matrix.DenseMatrix, err error) {
//k, _ := centroids.GetSize()
fp, _ := os.Create("/var/tmp/km.log")
w := io.Writer(fp)
log.SetOutput(w)
centroids := cc.ChooseCentroids(datapoints, k)
numRows, numCols := datapoints.GetSize()
centroidSqErr = matrix.Zeros(numRows, numCols)
centroidMean = matrix.Zeros(k, numCols)
jobs := make(chan PairPointCentroidJob, numworkers)
results := make(chan PairPointCentroidResult, minimum(1024, numRows))
done := make(chan int, numworkers)
go addPairPointCentroidJobs(jobs, datapoints, centroidSqErr, centroids, measurer, results)
for i := 0; i < numworkers; i++ {
go doPairPointCentroidJobs(done, jobs)
}
go awaitPairPointCentroidCompletion(done, results)
processPairPointToCentroidResults(centroidSqErr, results) // This blocks so that all the results can be processed
// Now that you have each data point grouped with a centroid, iterate
// through the centroidSqErr martix and for each centroid retrieve the
// original coordinates from datapoints and place the results in
// pointsInCuster.
for c := 0; c < k; c++ {
// c is the index that identifies the current centroid.
// d is the index that identifies a row in centroidSqErr and datapoints.
// Select all the rows in centroidSqErr whose first col value == c.
// Get the corresponding row vector from datapoints and place it in pointsInCluster.
matches, err := centroidSqErr.FiltColMap(float64(c), float64(c), 0) //rows with c in column 0.
if err != nil {
return centroidMean, centroidSqErr, nil
}
// It is possible that some centroids will not have any points, so there
// may not be any matches in the first column of centroidSqErr.
if len(matches) == 0 {
continue
}
pointsInCluster := matrix.Zeros(len(matches), 2)
for d, rownum := range matches {
pointsInCluster.Set(d, 0, datapoints.Get(int(rownum), 0))
pointsInCluster.Set(d, 1, datapoints.Get(int(rownum), 1))
}
// pointsInCluster now contains all the data points for the current
// centroid. Take the mean of each of the 2 cols in pointsInCluster.
means := pointsInCluster.MeanCols()
centroidMean.Set(c, 0, means.Get(0,0))
centroidMean.Set(c, 1, means.Get(0,1))
}
return
}
// CentroidPoint stores the row number in the centroids matrix and
// the distance squared between the centroid.
type CentroidPoint struct {
centroidRunNum float64
distPointToCentroidSq float64
}
// PairPointCentroidJobs stores the elements that defines the job that pairs a
// set of coordinates (i.e., a data point) with a centroid.
type PairPointCentroidJob struct {
point, centroids, centroidSqErr *matrix.DenseMatrix
results chan<- PairPointCentroidResult
rowNum int
measurer matutil.VectorMeasurer
}
// PairPointCentroidResult stores the results of pairing a data point with a
// centroids.
type PairPointCentroidResult struct {
centroidRowNum float64
distSquared float64
rowNum int
err error
}
// addPairPointCentroidJobs adds a job to the jobs channel.
func addPairPointCentroidJobs(jobs chan<- PairPointCentroidJob, datapoints, centroids,
centroidSqErr *matrix.DenseMatrix, measurer matutil.VectorMeasurer, results chan<- PairPointCentroidResult) {
numRows, _ := datapoints.GetSize()
for i := 0; i < numRows; i++ {
point := datapoints.GetRowVector(i)
jobs <- PairPointCentroidJob{point, centroids, centroidSqErr, results, i, measurer}
}
close(jobs)
}
// doPairPointCentroidJobs executes a job from the jobs channel.
func doPairPointCentroidJobs(done chan<- int, jobs <-chan PairPointCentroidJob) {
for job := range jobs {
job.PairPointCentroid()
}
done <- 1
}
// awaitPairPointCentroidCompletion waits until all jobs are completed.
func awaitPairPointCentroidCompletion(done <-chan int, results chan PairPointCentroidResult) {
for i := 0; i < numworkers; i++ {
<-done
}
close(results)
}
// processPairPointToCentroidResults assigns the results to the centroidSqErr matrix.
func processPairPointToCentroidResults(centroidSqErr *matrix.DenseMatrix, results <-chan PairPointCentroidResult) {
for result := range results {
centroidSqErr.Set(result.rowNum, 0, result.centroidRowNum)
centroidSqErr.Set(result.rowNum, 1, result.distSquared)
}
}
// AssignPointToCentroid checks a data point against all centroids and returns the best match.
// The centroid is identified by the row number in the centroid matrix.
func (job PairPointCentroidJob) PairPointCentroid() {
var err error = nil
distPointToCentroid := math.Inf(1)
centroidRowNum := float64(-1)
squaredErr := float64(0)
k, _ := job.centroids.GetSize()
// Find the centroid that is closest to this point.
for j := 0; j < k; j++ {
distJ, err := job.measurer.CalcDist(job.centroids.GetRowVector(j), job.point)
if err != nil {
continue
}
if distJ < distPointToCentroid {
distPointToCentroid = distJ
centroidRowNum = float64(j)
}
squaredErr = math.Pow(distPointToCentroid, 2)
}
job.results <- PairPointCentroidResult{centroidRowNum, squaredErr, job.rowNum, err}
}
// Kmeansbi bisects a given cluster and determines which centroids give the lowest error.
// Take the points in a cluster
// While the number of cluster < k
// for every cluster
// measure total error
// cacl kmeansp with k=2 on a given cluster
// measure total error after kmeansp split
// choose the cluster split with the lowest SSE
// commit the chosen split
//
// N.B. We are using SSE until the BIC is completed.
func Kmeansbi(datapoints *matrix.DenseMatrix, k int, cc CentroidChooser, measurer matutil.VectorMeasurer) (matCentroidlist, clusterAssignment *matrix.DenseMatrix, err error) {
numRows, numCols := datapoints.GetSize()
clusterAssignment = matrix.Zeros(numRows, numCols)
matCentroidlist = matrix.Zeros(k, numCols)
centroid0 := datapoints.MeanCols()
centroidlist := []*matrix.DenseMatrix{centroid0}
// Initially create one cluster.
for j := 0; j < numRows; j++ {
point := datapoints.GetRowVector(j)
distJ, err := measurer.CalcDist(centroid0, point)
if err != nil {
return matCentroidlist, clusterAssignment, errors.New(fmt.Sprintf("Kmeansbi: CalcDist returned err=%v", err))
}
clusterAssignment.Set(j,1, math.Pow(distJ, 2))
}
var bestClusterAssignment, bestNewCentroids *matrix.DenseMatrix
var bestCentroidToSplit int
// Find the best centroid configuration.
for ; len(centroidlist) < k; {
lowestSSE := math.Inf(1)
// Split cluster
for i, _ := range centroidlist {
// Get the points in this cluster
pointsCurCluster, err := clusterAssignment.FiltCol(float64(i), float64(i), 0)
if err != nil {
return matCentroidlist, clusterAssignment, err
}
centroids, splitClusterAssignment, err := Kmeansp(pointsCurCluster, 2, cc, measurer)
if err != nil {
return matCentroidlist, clusterAssignment, err
}
/* centroids is a 2X2 matrix of the best centroids found by kmeans
splitClustAssignment is a mX2 matrix where col0 is either 0 or 1 and refers to the rows in centroids
where col1 cotains the squared error between a centroid and a point. The rows here correspond to
the rows in ptsInCurrCluster. For example, if row 2 contains [1, 7.999] this means that centroid 1
has been paired with the point in row 2 of splitClustAssignment and that the squared error (distance
between centroid and point) is 7.999.
*/
// Calculate the sum of squared errors for each centroid.
// This give a statistcal measurement of how good
// the clustering is for this cluster.
sseSplit := splitClusterAssignment.SumCol(1)
// Calculate the SSE for the original cluster
sqerr, err := clusterAssignment.FiltCol(float64(0), math.Inf(1), 0)
if err != nil {
return matCentroidlist, clusterAssignment, err
}
sseNotSplit := sqerr.SumCol(1)
// TODO: Pre-BCI is this the best way to evaluate?
if sseSplit + sseNotSplit < lowestSSE {
bestCentroidToSplit = 1
bestNewCentroids = matrix.MakeDenseCopy(centroids)
bestClusterAssignment = matrix.MakeDenseCopy(splitClusterAssignment)
}
}
// Applying the split overwrites the existing cluster assginments for the
// cluster you have decided to split. Kmeansp() returned two clusters
// labeled 0 and 1. Change these cluster numbers to the cluster number
// you are splitting and the next cluster to be added.
m, err := bestClusterAssignment.FiltColMap(1, 1, 0)
if err != nil {
return matCentroidlist, clusterAssignment, err
}
for i,_ := range m {
bestClusterAssignment.Set(i, 0, float64(len(centroidlist)))
}
n, err := bestClusterAssignment.FiltColMap(0, 0, 0)
if err != nil {
return matCentroidlist, clusterAssignment, err
}
for i, _ := range n {
bestClusterAssignment.Set(i , 1, float64(bestCentroidToSplit))
}
fmt.Printf("Best centroid to split %f\n", bestCentroidToSplit)
r,_ := bestClusterAssignment.GetSize()
fmt.Printf("The length of best cluster assesment is %f\n", r)
// Replace a centroid with the two best centroids from the split.
centroidlist[bestCentroidToSplit] = bestNewCentroids.GetRowVector(0)
centroidlist = append(centroidlist, bestNewCentroids.GetRowVector(1))
// Reassign new clusters and SSE
rows, _ := clusterAssignment.GetSize()
for i, j := 0, 0 ; i < rows; i++ {
if clusterAssignment.Get(i, 0) == float64(bestCentroidToSplit) {
clusterAssignment.Set(i, 0, bestClusterAssignment.Get(j, 0))
clusterAssignment.Set(i, 1, bestClusterAssignment.Get(j, 1))
j++
}
}
// make centroidlist into a matrix
s := make([][]float64, len(centroidlist))
for i, mat := range centroidlist {
s[i][0] = mat.Get(0, 0)
s[i][1] = mat.Get(0, 1)
}
matCentroidlist = matrix.MakeDenseMatrixStacked(s)
}
return matCentroidlist, clusterAssignment, nil
}
// variance calculates the unbiased variance based on the number of data points
// and centroids (i.e., parameters). In our case, numcentroids should always be 1
// since each data point has been paired with one centroid.
//
// The points matrix contains the coordinates of the data points.
// The centroids matrix is 1Xn that contains the centroid cooordinates.
// variance = // 1 / (numpoints - numcentroids) * sum for all points (x_i - mean_(i))^2
func variance(points, centroid *matrix.DenseMatrix, measurer matutil.VectorMeasurer) (float64, error) {
crows, _ := centroid.GetSize()
if crows > 1 {
return float64(0), errors.New(fmt.Sprintf("variance: expected centroid matrix with 1 row, received matrix with %d rows.", crows))
}
prows, _ := points.GetSize()
// Term 1
t1 := float64(1 / float64((prows -1)))
// Mean of distance between all points and the centroid.
mean := modelMean(points, centroid)
// Term 2
// Sum over all points (point_i - mean(i))^2
t2 := float64(0)
for i := 0; i < prows; i++ {
p := points.GetRowVector(i)
dist, err := measurer.CalcDist(p, mean)
if err != nil {
return float64(-1), errors.New(fmt.Sprintf("variance: CalcDist returned: %v", err))
}
t2 += math.Pow(dist, 2)
}
variance := t1 * t2
return variance, nil
}
// modelMean calculates the mean between all points in a model and a centroid.
func modelMean(points, centroid *matrix.DenseMatrix) *matrix.DenseMatrix {
prows, pcols:= points.GetSize()
pdist := matrix.Zeros(prows, pcols)
for i := 0; i < prows; i++ {
diff := matrix.Difference(centroid, points.GetRowVector(i))
pdist.SetRowVector(diff, i)
}
return pdist.MeanCols()
}