44 "fmt"
55 "math"
66 "math/rand"
7- "sync"
87 "time"
98
109 "gonum.org/v1/gonum/floats"
@@ -16,14 +15,8 @@ type kmeansEstimator struct {
1615 // variables keeping count of changes of points' membership every iteration. User as a stopping condition.
1716 changes , oldchanges , counter , threshold int
1817
19- // For online learning only
20- alpha float64
21- dimension int
22-
2318 distance DistanceFunc
2419
25- // slices holding the cluster mapping and sizes. Access is synchronized to avoid read during computation.
26- mu sync.RWMutex
2720 a , b []int
2821
2922 // slices holding values of centroids of each clusters
@@ -75,25 +68,17 @@ func (c *kmeansEstimator) Estimate(data [][]float64) (int, error) {
7568 )
7669
7770 for i := 0 ; i < c .max ; i ++ {
78- c .number = i
71+ c .number = i + 1
7972
8073 c .learn (data )
8174
82- fmt .Printf ("Learned data for i = %d\n " , i )
83-
8475 wks [i ] = math .Log (wk (c .d , c .m , c .a ))
8576
86- fmt .Printf ("Computed wks for i = %d\n " , i )
87-
8877 for j := 0 ; j < c .max ; j ++ {
8978 c .learn (c .buildRandomizedSet (size , bounds ))
9079
91- fmt .Printf ("Learned randomized dataset for i = %d, j = %d\n " , i , j )
92-
9380 bwkbs [j ] = math .Log (wk (c .d , c .m , c .a ))
9481 one [j ] = 1
95-
96- fmt .Printf ("Computed bwkbs for i = %d, j = %d\n " , i , j )
9782 }
9883
9984 wkbs [i ] = floats .Sum (bwkbs ) / float64 (c .max )
@@ -103,16 +88,16 @@ func (c *kmeansEstimator) Estimate(data [][]float64) (int, error) {
10388 floats .Mul (bwkbs , bwkbs )
10489
10590 sk [i ] = math .Sqrt (floats .Sum (bwkbs ) / float64 (c .max ))
106-
107- fmt .Printf ("WKBS: %v\n " , wkbs )
108- fmt .Printf ("SK: %v\n " , sk )
10991 }
11092
11193 floats .Scale (math .Sqrt (1 + (1 / float64 (c .max ))), sk )
11294
95+ fmt .Printf ("WKBS: %v\n " , wkbs )
96+ fmt .Printf ("SK: %v\n " , sk )
97+
11398 for i := 0 ; i < c .max - 1 ; i ++ {
11499 if wkbs [i ] >= wkbs [i + 1 ]- sk [i + 1 ] {
115- estimated = i
100+ estimated = i + 1
116101 break
117102 }
118103 }
@@ -123,8 +108,6 @@ func (c *kmeansEstimator) Estimate(data [][]float64) (int, error) {
123108// private
124109
125110func (c * kmeansEstimator ) learn (data [][]float64 ) {
126- c .mu .Lock ()
127-
128111 c .d = data
129112
130113 c .a = make ([]int , len (data ))
@@ -143,8 +126,6 @@ func (c *kmeansEstimator) learn(data [][]float64) {
143126 }
144127
145128 c .n = nil
146-
147- c .mu .Unlock ()
148129}
149130
150131func (c * kmeansEstimator ) initializeMeansWithData () {
@@ -191,19 +172,6 @@ func (c *kmeansEstimator) initializeMeansWithData() {
191172 }
192173}
193174
194- func (c * kmeansEstimator ) initializeMeans () {
195- c .m = make ([][]float64 , c .number )
196-
197- rand .Seed (time .Now ().UTC ().Unix ())
198-
199- for i := 0 ; i < c .number ; i ++ {
200- c .m [i ] = make ([]float64 , c .dimension )
201- for j := 0 ; j < c .dimension ; j ++ {
202- c.m [i ][j ] = 10 * (rand .Float64 () - 0.5 )
203- }
204- }
205- }
206-
207175func (c * kmeansEstimator ) run () {
208176 var (
209177 l , k , n int = len (c .m [0 ]), 0 , 0
0 commit comments