-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathknn.go
232 lines (200 loc) · 7.18 KB
/
knn.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package mlpack
/*
#cgo CFLAGS: -I./capi -Wall
#cgo LDFLAGS: -L. -lmlpack_go_knn
#include <capi/knn.h>
#include <stdlib.h>
*/
import "C"
import "gonum.org/v1/gonum/mat"
type KnnOptionalParam struct {
Algorithm string
Epsilon float64
InputModel *knnModel
K int
LeafSize int
Query *mat.Dense
RandomBasis bool
Reference *mat.Dense
Rho float64
Seed int
Tau float64
TreeType string
TrueDistances *mat.Dense
TrueNeighbors *mat.Dense
Verbose bool
}
func KnnOptions() *KnnOptionalParam {
return &KnnOptionalParam{
Algorithm: "dual_tree",
Epsilon: 0,
InputModel: nil,
K: 0,
LeafSize: 20,
Query: nil,
RandomBasis: false,
Reference: nil,
Rho: 0.7,
Seed: 0,
Tau: 0,
TreeType: "kd",
TrueDistances: nil,
TrueNeighbors: nil,
Verbose: false,
}
}
/*
This program will calculate the k-nearest-neighbors of a set of points using
kd-trees or cover trees (cover tree support is experimental and may be slow).
You may specify a separate set of reference points and query points, or just a
reference set which will be used as both the reference and query set.
For example, the following command will calculate the 5 nearest neighbors of
each point in input and store the distances in distances and the neighbors in
neighbors:
// Initialize optional parameters for Knn().
param := mlpack.KnnOptions()
param.K = 5
param.Reference = input
distances, neighbors, _ := mlpack.Knn(param)
The output is organized such that row i and column j in the neighbors output
matrix corresponds to the index of the point in the reference set which is the
j'th nearest neighbor from the point in the query set with index i. Row j and
column i in the distances output matrix corresponds to the distance between
those two points.
Input parameters:
- Algorithm (string): Type of neighbor search: 'naive', 'single_tree',
'dual_tree', 'greedy'. Default value 'dual_tree'.
- Epsilon (float64): If specified, will do approximate nearest neighbor
search with given relative error. Default value 0.
- InputModel (knnModel): Pre-trained kNN model.
- K (int): Number of nearest neighbors to find. Default value 0.
- LeafSize (int): Leaf size for tree building (used for kd-trees, vp
trees, random projection trees, UB trees, R trees, R* trees, X trees,
Hilbert R trees, R+ trees, R++ trees, spill trees, and octrees).
Default value 20.
- Query (mat.Dense): Matrix containing query points (optional).
- RandomBasis (bool): Before tree-building, project the data onto a
random orthogonal basis.
- Reference (mat.Dense): Matrix containing the reference dataset.
- Rho (float64): Balance threshold (only valid for spill trees).
Default value 0.7.
- Seed (int): Random seed (if 0, std::time(NULL) is used). Default
value 0.
- Tau (float64): Overlapping size (only valid for spill trees).
Default value 0.
- TreeType (string): Type of tree to use: 'kd', 'vp', 'rp', 'max-rp',
'ub', 'cover', 'r', 'r-star', 'x', 'ball', 'hilbert-r', 'r-plus',
'r-plus-plus', 'spill', 'oct'. Default value 'kd'.
- TrueDistances (mat.Dense): Matrix of true distances to compute the
effective error (average relative error) (it is printed when -v is
specified).
- TrueNeighbors (mat.Dense): Matrix of true neighbors to compute the
recall (it is printed when -v is specified).
- Verbose (bool): Display informational messages and the full list of
parameters and timers at the end of execution.
Output parameters:
- distances (mat.Dense): Matrix to output distances into.
- neighbors (mat.Dense): Matrix to output neighbors into.
- outputModel (knnModel): If specified, the kNN model will be output
here.
*/
func Knn(param *KnnOptionalParam) (*mat.Dense, *mat.Dense, knnModel) {
params := getParams("knn")
timers := getTimers()
disableBacktrace()
disableVerbose()
// Detect if the parameter was passed; set if so.
if param.Algorithm != "dual_tree" {
setParamString(params, "algorithm", param.Algorithm)
setPassed(params, "algorithm")
}
// Detect if the parameter was passed; set if so.
if param.Epsilon != 0 {
setParamDouble(params, "epsilon", param.Epsilon)
setPassed(params, "epsilon")
}
// Detect if the parameter was passed; set if so.
if param.InputModel != nil {
setKNNModel(params, "input_model", param.InputModel)
setPassed(params, "input_model")
}
// Detect if the parameter was passed; set if so.
if param.K != 0 {
setParamInt(params, "k", param.K)
setPassed(params, "k")
}
// Detect if the parameter was passed; set if so.
if param.LeafSize != 20 {
setParamInt(params, "leaf_size", param.LeafSize)
setPassed(params, "leaf_size")
}
// Detect if the parameter was passed; set if so.
if param.Query != nil {
gonumToArmaMat(params, "query", param.Query, false)
setPassed(params, "query")
}
// Detect if the parameter was passed; set if so.
if param.RandomBasis != false {
setParamBool(params, "random_basis", param.RandomBasis)
setPassed(params, "random_basis")
}
// Detect if the parameter was passed; set if so.
if param.Reference != nil {
gonumToArmaMat(params, "reference", param.Reference, false)
setPassed(params, "reference")
}
// Detect if the parameter was passed; set if so.
if param.Rho != 0.7 {
setParamDouble(params, "rho", param.Rho)
setPassed(params, "rho")
}
// Detect if the parameter was passed; set if so.
if param.Seed != 0 {
setParamInt(params, "seed", param.Seed)
setPassed(params, "seed")
}
// Detect if the parameter was passed; set if so.
if param.Tau != 0 {
setParamDouble(params, "tau", param.Tau)
setPassed(params, "tau")
}
// Detect if the parameter was passed; set if so.
if param.TreeType != "kd" {
setParamString(params, "tree_type", param.TreeType)
setPassed(params, "tree_type")
}
// Detect if the parameter was passed; set if so.
if param.TrueDistances != nil {
gonumToArmaMat(params, "true_distances", param.TrueDistances, false)
setPassed(params, "true_distances")
}
// Detect if the parameter was passed; set if so.
if param.TrueNeighbors != nil {
gonumToArmaUmat(params, "true_neighbors", param.TrueNeighbors)
setPassed(params, "true_neighbors")
}
// Detect if the parameter was passed; set if so.
if param.Verbose != false {
setParamBool(params, "verbose", param.Verbose)
setPassed(params, "verbose")
enableVerbose()
}
// Mark all output options as passed.
setPassed(params, "distances")
setPassed(params, "neighbors")
setPassed(params, "output_model")
// Call the mlpack program.
C.mlpackKnn(params.mem, timers.mem)
// Initialize result variable and get output.
var distancesPtr mlpackArma
distances := distancesPtr.armaToGonumMat(params, "distances")
var neighborsPtr mlpackArma
neighbors := neighborsPtr.armaToGonumUmat(params, "neighbors")
var outputModel knnModel
outputModel.getKNNModel(params, "output_model")
// Clean memory.
cleanParams(params)
cleanTimers(timers)
// Return output(s).
return distances, neighbors, outputModel
}