source: proiecte/PPPP/ica/nnClassFn.m @ 138

Last change on this file since 138 was 138, checked in by (none), 14 years ago

PPPP - ica

File size: 2.4 KB
Line 
1%function [testPerf,rankmat,rank] = nnclassFn(train,test,trainClass,answer)
2%
3%Reads in training examples, test examples, class labels of training
4%examples, and correct class of test examples. Data are in columns of train
5%and test, and labels are column vectors.
6%
7%Note: You will need to create label vectors. TrainClass is a column
8%vector of integers indicating the identity of the training examples.
9%e.g. for faces of 3 people with two views each, TrainClass = [1 1 2 2 3 3 ]';
10%Answer contains the correct labels of the test images, which enables
11%us to compute percent correct. 
12%
13%Gets matrix of normalized dot products. Outputs nearest neighbor
14%classification of test examples and percent correct.
15%rankmat gives the top 30 matches for each test image.  rank is a vector
16%containing the percent of times the correct match is in the top N matches.
17
18
19function [testPerf] = nnclassFn(train,test,trainClass,answer);
20
21numTest = size(test,2);
22numTrain = size(train,2);
23
24%Get distances to training examples
25%dists = eucDist(test,train); %Outputs a Ntest x Ntrain matrix of Euc dist
26dists=-1 * cosFn(test,train);%Outputs a Ntest x Ntrain matrix of cosines
27
28%sort the rows of dists to find the nearest training example:
29[Sdist,nearest] = sort(dists'); %cols of Sdist are distances in ascend order
30        %1st row of nearest is index of 1st closest training example
31
32disp(Sdist)
33
34%Create vector with nearest example, and vector with class label.
35Nnbr = nearest(1,:);            %First row of nearest contains NN
36%Nnbr = nearest(2,:);
37testClass = trainClass(Nnbr);
38
39correct = find( (testClass - answer == 0));
40testPerf = size(correct,1) / size(answer,1)
41if(size(correct,2)>size(correct,1))
42        testPerf = size(correct,2) / size(answer,2)
43        'check vector orientation'
44end
45
46%get rank = %correct in top N:
47%cumtestPerf=0;
48%for i = 1
49%        rankmat(:,i) = trainClass(nearest(i,:)');
50%        correcti = find( (rankmat(:,i) - answer == 0));
51%        cumtestPerf = cumtestPerf + size(correcti,1) / size(answer,1);
52%        rank(i) = cumtestPerf;
53%end
54
55%For FERET test, want probeID (answer), then rank, then matched ID no.,
56%then FA flag, then "matching score".  This will be a matrix with:
57%probe  rank    match                           FAflag          matching score
58%i      1       trainClass(nearest(i,:))        Sdist(:,i)>4.7  1./Sdist(:,i)
59%i      2       OR rankmat(i,:)'
60%i      3
61%i      4
62
Note: See TracBrowser for help on using the repository browser.