-
Notifications
You must be signed in to change notification settings - Fork 0
/
knnSlice.m
55 lines (42 loc) · 1.4 KB
/
knnSlice.m
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
# WIP
# Adaptation of k nearest neighbors where we check for neighbors of a point by slices of
# increasing width to avoid checking all the n*(n-1) distances between points.
# Inputs:
# k = num of neighbors
# matrix = data point x feature values
# coord = coordinates in feature space of the point whose neighbors to find
#function label = knnSlice(k, matrix, coord)
k = 3;
n_pts = 20;
matrix = rand(n_pts,3);
matrix(1:n_pts/2,1) = 0;
matrix(n_pts/2 + 1:n_pts,1) = 1;
coord = 0.5 * ones(1,3);
# Assuming a n_points x 3 matrix, i.e. 2 features + column 1 for labels. TODO: generalize
n_feats = 2;
ini_rad = 0.2; # features assumed normalized TODO: check for normalization
# We'll find how many and which points are within a small area around the point.
# If there are at least k, we'll stop.
radius = ini_rad;
n_points_in_hood = 0;
while n_points_in_hood < k
x = matrix(:,2);
y = matrix(:,3);
x2 = coord(2);
y2 = coord(3);
dists = sqrt( (x2 .- x).^2 + (y2 .- y).^2 );
matrix_slice = (dists < radius^2);
n_points_in_hood = sum(matrix_slice);
radius += 0.1;
end
reduced_matrix = matrix(matrix_slice == 1, :)
for i=1:n_points_in_hood
x1 = reduced_matrix(i,2);
y1 = reduced_matrix(i,3);
x2 = coord(2);
y2 = coord(3);
dist = sqrt( (x2 - x1)^2 + (y2 - y1)^2 )
end
# TODO: take k nearest neighbors
# TODO: assign proper label to point
#end