-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_test.go
52 lines (43 loc) · 913 Bytes
/
knn_test.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
package knn_test
func Example() {
// import "github.com/bunji2/knn"
data := [][]float32{
[]float32{11, 11},
[]float32{1, 1},
[]float32{-11, 11},
[]float32{-1, 1},
[]float32{-11, -11},
[]float32{-1, -1},
[]float32{11, -11},
[]float32{1, -1},
}
labels := []int{
0,
0,
1,
1,
2,
2,
3,
3,
}
k := 3
numDataElm := 2
numLabels := 4
cls := knn.New(k, numDataElm, numLabels)
err := cls.Add(data, labels)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
xx := []float32{10, 10}
fmt.Println(xx, "==>", cls.Predict(xx)) // 0
xx = []float32{-10, 10}
fmt.Println(xx, "==>", cls.Predict(xx)) // 1
xx = []float32{-10, -10}
fmt.Println(xx, "==>", cls.Predict(xx)) // 2
xx = []float32{10, -10}
fmt.Println(xx, "==>", cls.Predict(xx)) // 3
xx = []float32{5, -5}
fmt.Println(xx, "==>", cls.Predict(xx)) // 3
}