-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKNN.m
More file actions
46 lines (41 loc) · 1.38 KB
/
Copy pathKNN.m
File metadata and controls
46 lines (41 loc) · 1.38 KB
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
% Implement k-th nearest neighbors classifier
%
% Input: X: a matrix for training set features
% Y_NUM: a vector raining set class labels
% toTest: a vector for testing set features
% kVal: specify the k value in k-th nearest neighbors
%
% Return: result: a vector for predicted class labels given features in toTest
%
% Author: Xuanpei Ouyang
function[result] = KNN(X,Y_NUM,toTest,kVal)
result = zeros(size(toTest,1),1);
for iter=1:size(toTest,1)
euclidian_dist = zeros(size(Y_NUM,1),1);
for j=1:size(X,2)
euclidian_dist = euclidian_dist + (X(:,j) - toTest(iter,j)).^2;
end
euclidian_dist = sqrt(euclidian_dist);
topNMinIndex = zeros(kVal,1);
for i=1:kVal
[~, minIndex] = min(euclidian_dist);
topNMinIndex(i) = minIndex;
euclidian_dist(minIndex) = max(euclidian_dist);
end
Classification_Result = Y_NUM(topNMinIndex);
countForZero = 0;
countForOne = 0;
for i=1:size(Classification_Result,1)
if(double(Classification_Result(i)) == 0)
countForZero = countForZero+1;
else
countForOne = countForOne+1;
end
end
if(countForZero > countForOne)
result(iter) = 0;
else
result(iter) = 1;
end
end
end