github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/clustering/kmeans_test.go (about) 1 package clustering 2 3 import ( 4 "math/rand" 5 "reflect" 6 "testing" 7 "time" 8 ) 9 10 func TestKmeans(t *testing.T) { 11 rand.Seed(time.Now().UnixNano()) 12 13 m := [][]float64{ 14 {0.1, 0.0}, 15 {0.9, 1.0}, 16 {-0.1, 0.0}, 17 {0.0, -0.1}, 18 {1.1, 1.0}, 19 {1.0, 1.1}, 20 {1.0, 0.9}, 21 {0.0, 0.1}, 22 } 23 24 means, tags := Kmeans(m, 2) 25 26 if tags[0] == 0 { 27 assertEqual(tags, []int{0, 1, 0, 0, 1, 1, 1, 0}, t) 28 assertEqual(means, [][]float64{{0, 0}, {1, 1}}, t) 29 } else { 30 assertEqual(tags, []int{1, 0, 1, 1, 0, 0, 0, 1}, t) 31 assertEqual(means, [][]float64{{1, 1}, {0, 0}}, t) 32 } 33 } 34 35 func assertEqual(act, exp interface{}, t *testing.T) { 36 if !reflect.DeepEqual(act, exp) { 37 t.Fatalf("Wrong value: %v, expected %v", act, exp) 38 } 39 }