11package clusters
22
33import (
4- "fmt"
54 "math"
65 "math/rand"
76 "time"
@@ -72,12 +71,12 @@ func (c *kmeansEstimator) Estimate(data [][]float64) (int, error) {
7271
7372 c .learn (data )
7473
75- wks [i ] = math .Log (wk (c .d , c .m , c .a ))
74+ wks [i ] = math .Log (c . wk (c .d , c .m , c .a ))
7675
7776 for j := 0 ; j < c .max ; j ++ {
7877 c .learn (c .buildRandomizedSet (size , bounds ))
7978
80- bwkbs [j ] = math .Log (wk (c .d , c .m , c .a ))
79+ bwkbs [j ] = math .Log (c . wk (c .d , c .m , c .a ))
8180 one [j ] = 1
8281 }
8382
@@ -92,9 +91,6 @@ func (c *kmeansEstimator) Estimate(data [][]float64) (int, error) {
9291
9392 floats .Scale (math .Sqrt (1 + (1 / float64 (c .max ))), sk )
9493
95- fmt .Printf ("WKBS: %v\n " , wkbs )
96- fmt .Printf ("SK: %v\n " , sk )
97-
9894 for i := 0 ; i < c .max - 1 ; i ++ {
9995 if wkbs [i ] >= wkbs [i + 1 ]- sk [i + 1 ] {
10096 estimated = i + 1
@@ -106,7 +102,6 @@ func (c *kmeansEstimator) Estimate(data [][]float64) (int, error) {
106102}
107103
108104// private
109-
110105func (c * kmeansEstimator ) learn (data [][]float64 ) {
111106 c .d = data
112107
@@ -124,8 +119,6 @@ func (c *kmeansEstimator) learn(data [][]float64) {
124119 c .run ()
125120 c .check ()
126121 }
127-
128- c .n = nil
129122}
130123
131124func (c * kmeansEstimator ) initializeMeansWithData () {
@@ -223,6 +216,19 @@ func (c *kmeansEstimator) check() {
223216 c .oldchanges = c .changes
224217}
225218
219+ func (c * kmeansEstimator ) wk (data [][]float64 , centroids [][]float64 , mapping []int ) float64 {
220+ var (
221+ l = float64 (2 * len (data [0 ]))
222+ wk = make ([]float64 , len (centroids ))
223+ )
224+
225+ for i := 0 ; i < len (mapping ); i ++ {
226+ wk [mapping [i ]- 1 ] += EuclideanDistanceSquared (centroids [mapping [i ]- 1 ], data [i ]) / l
227+ }
228+
229+ return floats .Sum (wk )
230+ }
231+
226232func (c * kmeansEstimator ) buildRandomizedSet (size int , bounds []* [2 ]float64 ) [][]float64 {
227233 var (
228234 l = len (bounds )
0 commit comments