-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
61 lines (47 loc) · 1.38 KB
/
example_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
53
54
55
56
57
58
59
60
61
package bitknn_test
import (
"fmt"
"github.com/keilerkonzept/bitknn"
"github.com/keilerkonzept/bitknn/pack"
)
func Example() {
// feature vectors packed into uint64s
data := []uint64{0b101010, 0b111000, 0b000111}
// class labels
labels := []int{0, 1, 1}
model := bitknn.Fit(data, labels, bitknn.WithLinearDistanceWeighting())
// one vote counter per class
votes := make([]float64, 2)
k := 2
model.Predict(k, 0b101011, bitknn.VoteSlice(votes))
// or, just return the nearest neighbor's distances and indices:
// distances,indices := model.Find(k, 0b101011)
fmt.Println("Votes:", votes)
// you can also use a map for the votes.
// this is good if you have a very large number of different labels:
votesMap := make(map[int]float64)
model.Predict(k, 0b101011, bitknn.VoteMap(votesMap))
fmt.Println("Votes for 0:", votesMap[0])
// Output:
// Votes: [0.5 0.25]
// Votes for 0: 0.5
}
func ExampleFitWide() {
// feature vectors packed into uint64s
data := [][]uint64{
pack.String("foo"),
pack.String("bar"),
pack.String("baz"),
}
// class labels
labels := []int{0, 1, 1}
model := bitknn.FitWide(data, labels, bitknn.WithLinearDistanceWeighting())
// one vote counter per class
votes := make([]float64, 2)
k := 2
query := pack.String("fob")
model.Predict(k, query, bitknn.VoteSlice(votes))
fmt.Println("Votes:", votes)
// Output:
// Votes: [0.25 0.16666666666666666]
}