|
a |
|
b/functions/functions_Classifiers/computekNNClassificationPerformance.m |
|
|
1 |
function [TestPCAOutput, distMatrix] = computekNNClassificationPerformance(ftestPCA_all, TestPCALabels, sizeTest, stepPrint, numCoresKnn, param) |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
%Distance matrix |
|
|
5 |
if strcmp(param.matchDistance, 'chisq') |
|
|
6 |
distMatrix = sc_pdist2(ftestPCA_all, ftestPCA_all, 'chisq'); |
|
|
7 |
elseif strcmp(param.matchDistance, 'euclidean') |
|
|
8 |
%fast Euclidean distance |
|
|
9 |
distMatrix = full(fastEuclideanDistance(ftestPCA_all, ftestPCA_all)); |
|
|
10 |
else %if strcmp |
|
|
11 |
distMatrix = pdist2(ftestPCA_all', ftestPCA_all', param.matchDistance); |
|
|
12 |
end %if strcmp |
|
|
13 |
|
|
|
14 |
%modo parallelo per knn (k = 1) classification |
|
|
15 |
%leave-one-out |
|
|
16 |
%we use knn_neighbors + 1 because otherwise it would find the same vector |
|
|
17 |
%in this way we choose the second neighbor (which is the actual first neighbor) |
|
|
18 |
%(we use the entire feature vector for all samples) |
|
|
19 |
%loop on test samples |
|
|
20 |
%init |
|
|
21 |
TestPCAOutput = zeros(sizeTest, 1); |
|
|
22 |
|
|
|
23 |
%parallel vars init |
|
|
24 |
% ftestPCA_all = parallel.pool.Constant(ftestPCA_all); |
|
|
25 |
knnDistancePar = param.knnDistance; |
|
|
26 |
matchDistance = param.matchDistance; |
|
|
27 |
numkPar = param.knn_neighbors; |
|
|
28 |
|
|
|
29 |
% start_pool(numCoresKnn); |
|
|
30 |
|
|
|
31 |
% parfor g = 1 : sizeTest |
|
|
32 |
for g = 1 : sizeTest |
|
|
33 |
|
|
|
34 |
%get id of current worker |
|
|
35 |
t = getCurrentTask(); |
|
|
36 |
|
|
|
37 |
%display progress |
|
|
38 |
if mod(g, stepPrint) == 0 |
|
|
39 |
%fprintf(1, ['\t\tCore ' num2str(t.ID) ': ' num2str(g) ' / ' num2str(sizeTest) '\n']) |
|
|
40 |
fprintf(1, ['\t\t' num2str(g) ' / ' num2str(sizeTest) '\n']) |
|
|
41 |
end %if mod(i, 100) == 0 |
|
|
42 |
|
|
|
43 |
if strcmp(knnDistancePar, matchDistance) |
|
|
44 |
%we can re-use the distance matrix |
|
|
45 |
distV = distMatrix(g, :); |
|
|
46 |
sortV = sort(distV, 'ascend'); |
|
|
47 |
minD = sortV(2); %the first will be 0 |
|
|
48 |
idx = find(distV == minD); |
|
|
49 |
idx = idx(1); %se dovessero essercene altri a pari merito |
|
|
50 |
|
|
|
51 |
else %if strcmp(param.knnDistance, param.matchDistance) |
|
|
52 |
%we use + 1 |
|
|
53 |
%idx = knnsearch(ftestPCA_all.Value', ftestPCA_all.Value(:, g)', 'K', numkPar + 1, 'Distance', knnDistancePar); |
|
|
54 |
idx = knnsearch(ftestPCA_all', ftestPCA_all(:, g)', 'K', numkPar + 1, 'Distance', knnDistancePar); |
|
|
55 |
idx(idx == g) = []; %il pių vicino č il vettore stesso, lo togliamo |
|
|
56 |
idx = idx(1); %se dovessero essercene altri a pari merito |
|
|
57 |
end %if strcmp(param.knnDistance, param.matchDistance) |
|
|
58 |
|
|
|
59 |
TestPCAOutput(g) = idx; |
|
|
60 |
|
|
|
61 |
end %for g |
|
|
62 |
|
|
|
63 |
%mettiamo le labels al posto degli indici trovati |
|
|
64 |
for g = 1 : sizeTest |
|
|
65 |
TestPCAOutput(g) = TestPCALabels(TestPCAOutput(g)); |
|
|
66 |
end %for g |
|
|
67 |
|
|
|
68 |
|