-
Notifications
You must be signed in to change notification settings - Fork 4
/
dopca.m
113 lines (73 loc) · 1.89 KB
/
dopca.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function [r,pc] = dopca()
%UNTITLED9 Summary of this function goes here
% Detailed explanation goes here
% with 60,000 samples , its too much. don't have enough memory
% on hand to compute (need 24gb).
%
% so just look at 10,000 samples -> this needs 0.8Gb which we have.
%
[train_data, train_labels, test_data, test_labels] = readDATA();
%train_data = procTD(train_data);
[test_data, test_labels] = loadHndDATA();
length = size(train_data,2);
length = 2000;
data = zeros(784,length);
for i =1:length
if(mod(i,1000) == 0)
disp(i);
end
img = train_data{i};
img = img(:);
data(:,i) = img;
end
disp('doing pca...');
[r, pc] = pca(data);
%[r, pc] = pca2(data');
disp('pca done');
disp('proj data');
size(r)
disp('principal comp')
size(pc)
%error('s');
disp('doiing testing of pca results...');
train_size = length;
train_data = r;
errors=0;
for i = 1:200
tv = test_data{i};
%tv = proc(tv);
tv = tv(:);
%tv = tv'
ptv = pc' * tv;
tv = ptv;
minIdx = 0;
minDist = Inf;
for k = 1:train_size
j=k;
av = train_data(j);
av = av(:);
diff = tv - av;
dist = norm(diff,2);
if(dist < minDist)
minDist = dist;
%disp(j);
minIdx = j;
end
end
result(i) = train_labels{minIdx};
%do accuracy checking in line
if( test_labels(i) ~= result(i) )
errors = errors + 1;
end
tot = i;
curr_acc = (tot - errors) / tot;
if( test_labels(i) ~= result(i) )
fprintf('Curr Accuracy: %f | %d,%d.\n', curr_acc, test_labels(i),result(i) );
%figure;
%im = [test_data{i} train_data{minIdx} ];
%imshow(im);
continue;
end
fprintf('Curr Accuracy: %f.\n', curr_acc);
end
end