forked from sjwhitworth/golearn
/
kdtree.go
212 lines (182 loc) · 5.22 KB
/
kdtree.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
202
203
204
205
206
207
208
209
210
211
212
package kdtree
import (
"errors"
"gonum.org/v1/gonum/mat"
"github.com/mia-creators/golearn/metrics/pairwise"
"sort"
)
type node struct {
feature int
value []float64
srcRowNo int
left *node
right *node
}
// Tree is a kdtree.
type Tree struct {
firstDiv *node
data [][]float64
}
type SortData struct {
RowData [][]float64
Data []int
Feature int
}
func (d SortData) Len() int { return len(d.Data) }
func (d SortData) Less(i, j int) bool {
return d.RowData[d.Data[i]][d.Feature] < d.RowData[d.Data[j]][d.Feature]
}
func (d SortData) Swap(i, j int) { d.Data[i], d.Data[j] = d.Data[j], d.Data[i] }
// New return a Tree pointer.
func New() *Tree {
return &Tree{}
}
// Build builds the kdtree with specific data.
func (t *Tree) Build(data [][]float64) error {
if len(data) == 0 {
return errors.New("no input data")
}
size := len(data[0])
for _, v := range data {
if len(v) != size {
return errors.New("amounts of features are not the same")
}
}
t.data = data
newData := make([]int, len(data))
for k, _ := range newData {
newData[k] = k
}
if len(data) == 1 {
t.firstDiv = &node{feature: -1, srcRowNo: 0}
t.firstDiv.value = make([]float64, len(data[0]))
copy(t.firstDiv.value, data[0])
} else {
t.firstDiv = t.buildHandle(newData, 0)
}
return nil
}
// buildHandle builds the kdtree recursively.
func (t *Tree) buildHandle(data []int, featureIndex int) *node {
n := &node{feature: featureIndex}
tmp := SortData{RowData: t.data, Data: data, Feature: featureIndex}
sort.Sort(tmp)
middle := len(data) / 2
divPoint := middle
for i := middle + 1; i < len(data); i++ {
if t.data[data[i]][featureIndex] == t.data[data[middle]][featureIndex] {
divPoint = i
} else {
break
}
}
n.srcRowNo = data[divPoint]
n.value = make([]float64, len(t.data[data[divPoint]]))
copy(n.value, t.data[data[divPoint]])
if divPoint == 1 {
n.left = &node{feature: -1}
n.left.value = make([]float64, len(t.data[data[0]]))
copy(n.left.value, t.data[data[0]])
n.left.srcRowNo = data[0]
} else {
n.left = t.buildHandle(data[:divPoint], (featureIndex+1)%len(t.data[data[0]]))
}
if divPoint == (len(data) - 2) {
n.right = &node{feature: -1}
n.right.value = make([]float64, len(t.data[data[divPoint+1]]))
copy(n.right.value, t.data[data[divPoint+1]])
n.right.srcRowNo = data[divPoint+1]
} else if divPoint != (len(data) - 1) {
n.right = t.buildHandle(data[divPoint+1:], (featureIndex+1)%len(t.data[data[0]]))
} else {
n.right = &node{feature: -2}
}
return n
}
// Search return srcRowNo([]int) and length([]float64) contained
// k nearest neighbors from specific distance function.
func (t *Tree) Search(k int, disType pairwise.PairwiseDistanceFunc, target []float64) ([]int, []float64, error) {
if k > len(t.data) {
return []int{}, []float64{}, errors.New("k is largerer than amount of trainData")
}
if len(target) != len(t.data[0]) {
return []int{}, []float64{}, errors.New("amount of features is not equal")
}
h := newHeap()
t.searchHandle(k, disType, target, h, t.firstDiv)
srcRowNo := make([]int, k)
length := make([]float64, k)
i := k - 1
for h.size() != 0 {
srcRowNo[i] = h.maximum().srcRowNo
length[i] = h.maximum().length
i--
h.extractMax()
}
return srcRowNo, length, nil
}
func (t *Tree) searchHandle(k int, disType pairwise.PairwiseDistanceFunc, target []float64, h *heap, n *node) {
if n.feature == -1 {
vectorX := mat.NewDense(len(target), 1, target)
vectorY := mat.NewDense(len(target), 1, n.value)
length := disType.Distance(vectorX, vectorY)
h.insert(n.value, length, n.srcRowNo)
return
} else if n.feature == -2 {
return
}
dir := true
if target[n.feature] <= n.value[n.feature] {
t.searchHandle(k, disType, target, h, n.left)
} else {
dir = false
t.searchHandle(k, disType, target, h, n.right)
}
vectorX := mat.NewDense(len(target), 1, target)
vectorY := mat.NewDense(len(target), 1, n.value)
length := disType.Distance(vectorX, vectorY)
if k > h.size() {
h.insert(n.value, length, n.srcRowNo)
if dir {
t.searchAllNodes(k, disType, target, h, n.right)
} else {
t.searchAllNodes(k, disType, target, h, n.left)
}
} else if h.maximum().length > length {
h.extractMax()
h.insert(n.value, length, n.srcRowNo)
if dir {
t.searchAllNodes(k, disType, target, h, n.right)
} else {
t.searchAllNodes(k, disType, target, h, n.left)
}
} else {
vectorX = mat.NewDense(1, 1, []float64{target[n.feature]})
vectorY = mat.NewDense(1, 1, []float64{n.value[n.feature]})
length = disType.Distance(vectorX, vectorY)
if h.maximum().length > length {
if dir {
t.searchAllNodes(k, disType, target, h, n.right)
} else {
t.searchAllNodes(k, disType, target, h, n.left)
}
}
}
}
func (t *Tree) searchAllNodes(k int, disType pairwise.PairwiseDistanceFunc, target []float64, h *heap, n *node) {
vectorX := mat.NewDense(len(target), 1, target)
vectorY := mat.NewDense(len(target), 1, n.value)
length := disType.Distance(vectorX, vectorY)
if k > h.size() {
h.insert(n.value, length, n.srcRowNo)
} else if h.maximum().length > length {
h.extractMax()
h.insert(n.value, length, n.srcRowNo)
}
if n.left != nil {
t.searchAllNodes(k, disType, target, h, n.left)
}
if n.right != nil {
t.searchAllNodes(k, disType, target, h, n.right)
}
}