-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
201 lines (177 loc) · 5.95 KB
/
model.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Package bitknn provides a fast exact k-nearest neighbors (k-NN) implementation for binary feature vectors.
package bitknn
import (
"github.com/keilerkonzept/bitknn/internal/slice"
)
// Create a k-NN model for the given data points and labels.
func Fit(data []uint64, labels []int, opts ...Option) *Model {
m := &Model{
Data: data,
Labels: labels,
DistanceWeighting: DistanceWeightingNone,
}
for _, opt := range opts {
opt(m)
}
return m
}
// A k-NN model for uint64s.
type Model struct {
// Input data points.
Data []uint64
// Class labels for each data point.
Labels []int
// Vote values for each data point.
Values []float64
// Distance weighting function.
DistanceWeighting DistanceWeighting
// Custom function when [Model.DistanceWeighting] is [DistanceWeightingCustom].
DistanceWeightingFunc func(int) float64
HeapDistances []int
HeapIndices []int
}
func (me *Model) PreallocateHeap(k int) {
me.HeapDistances = slice.OrAlloc(me.HeapDistances, k+1)
me.HeapIndices = slice.OrAlloc(me.HeapIndices, k+1)
}
// Finds the nearest neighbors of the given point.
// Writes their distances and indices in the dataset into the pre-allocated slices.
// Returns the distance and index slices, truncated to the actual number of neighbors found.
func (me *Model) Find(k int, x uint64) ([]int, []int) {
me.PreallocateHeap(k)
return me.FindInto(k, x, me.HeapDistances, me.HeapIndices)
}
// Finds the nearest neighbors of the given point.
// Writes their distances and indices in the dataset into the provided slices.
// The slices should be pre-allocated to length k+1.
// Returns the distance and index slices, truncated to the actual number of neighbors found.
func (me *Model) FindInto(k int, x uint64, distances []int, indices []int) ([]int, []int) {
k = Nearest(me.Data, k, x, distances, indices)
return distances[:k], indices[:k]
}
// Predicts the label of a single input point. Each call allocates two new slices of length K+1 for the neighbor heap.
func (me *Model) PredictAlloc(k int, x uint64, votes VoteCounter) {
distances, indices := make([]int, k+1), make([]int, k+1)
me.PredictInto(k, x, distances, indices, votes)
}
// Predicts the label of a single input point. Reuses two slices of length K+1 for the neighbor heap.
func (me *Model) Predict(k int, x uint64, votes VoteCounter) {
me.HeapDistances = slice.OrAlloc(me.HeapDistances, k+1)
me.HeapIndices = slice.OrAlloc(me.HeapIndices, k+1)
me.PredictInto(k, x, me.HeapDistances, me.HeapIndices, votes)
}
// Predicts the label of a single input point, using the given slices for the neighbor heap.
func (me *Model) PredictInto(k int, x uint64, distances []int, indices []int, votes VoteCounter) {
k = Nearest(me.Data, k, x, distances, indices)
me.Vote(k, distances, indices, votes)
}
// Predicts the label of a single input point, using the given slices for the neighbor heap.
func (me *Model) Vote(k int, distances []int, indices []int, votes VoteCounter) {
votes.Clear()
switch me.DistanceWeighting {
case DistanceWeightingNone:
if me.Values == nil {
me.votes1(k, indices, votes)
} else {
me.votes1v(k, indices, votes)
}
case DistanceWeightingLinear:
if me.Values == nil {
me.votes1l(k, indices, votes, distances)
} else {
me.votes1vl(k, indices, votes, distances)
}
case DistanceWeightingQuadratic:
if me.Values == nil {
me.votes1q(k, indices, votes, distances)
} else {
me.votes1vq(k, indices, votes, distances)
}
case DistanceWeightingCustom:
f := me.DistanceWeightingFunc
if me.Values == nil {
me.votes1c(k, indices, votes, f, distances)
} else {
me.votes1vc(k, indices, votes, f, distances)
}
}
}
func (me *Model) votes1vc(k int, indices []int, votes VoteCounter, f func(int) float64, distances []int) {
for i := range k {
index := indices[i]
label := me.Labels[index]
votes.Add(label, f(distances[i])*me.Values[index])
}
}
func (me *Model) votes1c(k int, indices []int, votes VoteCounter, f func(int) float64, distances []int) {
for i := range k {
index := indices[i]
label := me.Labels[index]
votes.Add(label, f(distances[i]))
}
}
func (me *Model) votes1vq(k int, indices []int, votes VoteCounter, distances []int) {
for i := range k {
index := indices[i]
label := me.Labels[index]
votes.Add(label, DistanceWeightingFuncQuadratic(distances[i])*me.Values[index])
}
}
func (me *Model) votes1q(k int, indices []int, votes VoteCounter, distances []int) {
for i := range k {
index := indices[i]
label := me.Labels[index]
votes.Add(label, DistanceWeightingFuncQuadratic(distances[i]))
}
}
func (me *Model) votes1vl(k int, indices []int, votes VoteCounter, distances []int) {
for i := range k {
index := indices[i]
label := me.Labels[index]
votes.Add(label, DistanceWeightingFuncLinear(distances[i])*me.Values[index])
}
}
func (me *Model) votes1l(k int, indices []int, votes VoteCounter, distances []int) {
for i := range k {
index := indices[i]
label := me.Labels[index]
votes.Add(label, DistanceWeightingFuncLinear(distances[i]))
}
}
func (me *Model) votes1v(k int, indices []int, votes VoteCounter) {
for i := range k {
index := indices[i]
label := me.Labels[index]
votes.Add(label, me.Values[index])
}
}
func (me *Model) votes1(k int, indices []int, votes VoteCounter) {
for i := range k {
index := indices[i]
label := me.Labels[index]
votes.Add(label, 1)
}
}
type distanceWeighting int
type DistanceWeighting distanceWeighting
const (
DistanceWeightingNone DistanceWeighting = iota
DistanceWeightingLinear
DistanceWeightingQuadratic
DistanceWeightingCustom
)
func (me DistanceWeighting) String() string {
switch me {
case DistanceWeightingNone:
return "none"
case DistanceWeightingLinear:
return "linear"
case DistanceWeightingQuadratic:
return "quadratic"
case DistanceWeightingCustom:
return "custom"
}
return "unknown"
}
func DistanceWeightingFuncLinear(dist int) float64 { return 1.0 / float64(1+dist) }
func DistanceWeightingFuncQuadratic(dist int) float64 { return 1.0 / float64(1+(dist*dist)) }