-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_pca.m
71 lines (58 loc) · 1.48 KB
/
mnist_pca.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
target = zeros(10,60000);
for j=1:60000
if training_label(j,1)== 0
target(1,j) = 1;
end
if training_label(j,1)== 1
target(2,j) = 1;
end
if training_label(j,1)== 2
target(3,j) = 1;
end
if training_label(j,1)== 3
target(4,j) = 1;
end
if training_label(j,1)== 4
target(5,j) = 1;
%break;
end
if training_label(j,1)== 5
target(6,j) = 1;
%break;
end
if training_label(j,1)== 6
target(7,j) = 1;
end
if training_label(j,1)== 7
target(8,j) = 1;
end
if training_label(j,1)== 8
target(9,j) = 1;
end
if training_label(j,1)== 9
target(10,j) = 1;
end
end
%pca
mean_matrix = zeros(size(training_image,1),1);
for i=1:size(training_image,1)
sum=0;
for j=1:size(training_image,2)
sum = sum + training_image(i,j);
end
mean_matrix(i)=sum/j;
end
for i=1:size(training_image,1)
for j=1:size(training_image,2)
training_image(i,j)= training_image(i,j)-mean_matrix(i);
end
end
%covariance matrix
%cov = zeros(size(training_image,2),size(training_image,2));
cov = training_image*training_image';
[eig_vec,eig_val] = eig(cov);
%Eig_vec = eig_vec'*training_image;
%weight_vector = zeros(2500,size(img_vectors,2));
weight_vector = eig_vec'*training_image;
%mapping 1st three eigen faces to original dimension
%pca=zeros(size(training_image,1),3);