-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuntitled2.m~
More file actions
183 lines (137 loc) · 3.65 KB
/
untitled2.m~
File metadata and controls
183 lines (137 loc) · 3.65 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
clear all;
load imdata.mat;
x = double(x);
y = double(y);
%2-a
mu = mean(x,1);
C = cov(x);
[E, lambda] = eig(C);
lambda = diag(lambda);
[lambda, permutation] = sort(lambda, 'descend');
E = E(:, permutation);
imagesc(reshape([mean(x,1) zeros(1,18)],35,30)');
figure; imagesc(reshape([E(:,1)' zeros(1,18)],35,30)');
figure; imagesc(reshape([E(:,2)' zeros(1,18)],35,30)');
figure; imagesc(reshape([E(:,3)' zeros(1,18)],35,30)');
%2-b
%stores the projection
pca3d = zeros(100000, 3);
for i=1:100000
%create the projection
for j=1:3
pca3d(i, j) = (x(i,:) -mu) * E(:, j);
end
end
%sotres the projected images
x_pca3d = zeros(size(x));
for i=1:100000
x_pca3d(i,:) = mu + pca3d(i,1)*E(:,1)' + pca3d(i,2)*E(:,2)' + pca3d(i,3)*E(:,3)';
end
MSE = zeros(100000,1);
for i=1:100000
MSE(i) = sum( (x(i,:)-x_pca3d(i,:) ).^2);
end
%find the image worst represented by the 3d principal subspace
[c index] = max(MSE);
%index = 34729
image(reshape([x(index,:) zeros(1,18)],35,30)');
figure;
image(reshape([x_pca3d(index,:) zeros(1,18)],35,30)');
%2-c
figure;hist(y,64);
%2-d
diff = zeros(size(y));
diff = y - x(:,end);
hist(diff,-64:64);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%3-b-ii
clear all;
load imdata.mat;
x=double(x);
y=double(y);
data = [x(:,end) x(:,end-34) x(:,end-35) y];
L=0;
for i=1:4
if i<4
train_data = data([1:25000*(i-1),(25000*i+1):end],:);
test_data = data(25000*(i-1)+1 : 25000*i, :);
elseif i==4
train_data = data(1:75000,:);
test_data = data(75001:end,:);
end
%train
[py p1 p2 p3] = naivebayes(train_data);
%test
for i=1:25000
x1 = test_data(i,1) + 1;
x2 = test_data(i,2) + 1;
x3 = test_data(i,3) + 1;
sum = 0;
for j=1:64
pt(i,j) = py(j) * p1(x1,j) * p2(x2,j) * p3(x3,j) ;
sum = sum + pt(i,j);
end
pt(i,:) = pt(i,:) / sum;
end
for i=1:25000
y = test_data(i,4) + 1;
L = L -log( pt(i,y) );
end
end
perplexity = exp(L/100000);
% naive bayes function
function [py p1 p2 p3] = naivebayes(data)
num = size(data,1);
cat_y = zeros(64,1);% y categories number
py = zeros(64,1);
p1 = zeros(64,64);
p2 = zeros(64,64);
p3 = zeros(64,64);
for i=1:num
x1 = data(i,1) + 1;
x2 = data(i,2) + 1;
x3 = data(i,3) + 1;
y = data(i,4) + 1;
cat_y(y) = cat_y(y) + 1;
p1(x1,y) = p1(x1,y) + 1;
p2(x2,y) = p2(x2,y) + 1;
p3(x3,y) = p3(x3,y) + 1;
end
for i=1:64
p1(:,i) = (p1(:,i)+1) / (cat_y(i)+64); %dirichilet distribution
p2(:,i) = (p2(:,i)+1) / (cat_y(i)+64); %dirichilet distribution
p3(:,i) = (p3(:,i)+1) / (cat_y(i)+64); %dirichilet distribution
py(i) = cat_y(i) / num;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%4-a linear regression
clear all;
load imdata.mat;
x=double(x);
y=double(y);
num = size(x,1);
data = [ x(:,end), x(:,end-34), x(:,end-35), ones(num,1), y];
L=0;
for i=1:4
if i<4
train_data = data([1:25000*(i-1),(25000*i+1):end],:);
test_data = data(25000*(i-1)+1 : 25000*i, :);
elseif i==4
train_data = data(1:75000,:);
test_data = data(75001:end,:);
end
[beta sigma] = mvregress(train_data(:,1:4), train_data(:,5));
p_y = zeros(25000,64);
for i=1:25000
for y=0:63
p_y(i,y+1) = exp( -(y - test_data(i,1:4)*beta)^2 / (2*sigma) ) / sqrt(2*pi*sigma) ;
end
end
for i=1:25000
y = test_data(i,5);
L = L - log(p_y(i,y+1));
end
end
perplexity = exp(L/100000);
% 4-