|
a |
|
b/functions/functions_Classifiers/computekNNClassificationPerformanceTrainTest.m |
|
|
1 |
function [TestPCAOutput, distMatrix] = computekNNClassificationPerformanceTrainTest(ftrain_all, ftestPCA_all, TrnPCALabels, TestPCALabels, sizeTest, stepPrint, numCoresKnn, param) |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
%Distance matrix |
|
|
5 |
distMatrix = full(fastEuclideanDistance(ftestPCA_all, ftrain_all)); |
|
|
6 |
|
|
|
7 |
% whos ftrain_all ftestPCA_all distMatrix |
|
|
8 |
|
|
|
9 |
%modo parallelo per knn (k = 1) classification |
|
|
10 |
%leave-one-out |
|
|
11 |
%we use knn_neighbors + 1 because otherwise it would find the same vector |
|
|
12 |
%in this way we choose the second neighbor (which is the actual first neighbor) |
|
|
13 |
%(we use the entire feature vector for all samples) |
|
|
14 |
%loop on test samples |
|
|
15 |
%init |
|
|
16 |
TestPCAOutput = zeros(sizeTest, 1); |
|
|
17 |
|
|
|
18 |
% start_pool(numCoresKnn); |
|
|
19 |
|
|
|
20 |
% parfor g = 1 : sizeTest |
|
|
21 |
for g = 1 : sizeTest |
|
|
22 |
|
|
|
23 |
%get id of current worker |
|
|
24 |
%t = getCurrentTask(); |
|
|
25 |
|
|
|
26 |
%display progress |
|
|
27 |
if mod(g, stepPrint) == 0 |
|
|
28 |
%fprintf(1, ['\t\tCore ' num2str(t.ID) ': ' num2str(g) ' / ' num2str(sizeTest) '\n']) |
|
|
29 |
fprintf(1, ['\t\t' num2str(g) ' / ' num2str(sizeTest) '\n']) |
|
|
30 |
end %if mod(i, 100) == 0 |
|
|
31 |
|
|
|
32 |
%we can re-use the distance matrix |
|
|
33 |
distV = distMatrix(g, :); |
|
|
34 |
sortV = sort(distV, 'ascend'); |
|
|
35 |
%minD = sortV(2); %the first will be 0 |
|
|
36 |
minD = sortV(1); %the first will be 0 |
|
|
37 |
idx = find(distV == minD); |
|
|
38 |
idx = idx(1); %se dovessero essercene altri a pari merito |
|
|
39 |
|
|
|
40 |
TestPCAOutput(g) = idx; |
|
|
41 |
|
|
|
42 |
end %for g |
|
|
43 |
|
|
|
44 |
%mettiamo le labels al posto degli indici trovati |
|
|
45 |
for g = 1 : sizeTest |
|
|
46 |
%TestPCAOutput(g) = TestPCALabels(TestPCAOutput(g)); |
|
|
47 |
TestPCAOutput(g) = TrnPCALabels(TestPCAOutput(g)); |
|
|
48 |
end %for g |
|
|
49 |
|
|
|
50 |
|