-
Notifications
You must be signed in to change notification settings - Fork 2
/
lda.m
166 lines (112 loc) · 2.54 KB
/
lda.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
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
saved_name = 'TRAIN';
s = load('TRAINTEST2D.mat',saved_name);
d = s.(saved_name);
data = d{1,4};
Z1 = rotdim(data{1,1});
X1 = Z1(:,1);
Y1 = Z1(:,2);
Z2 = rotdim(data{1,2});
X2 = Z2(:,1);
Y2 = Z2(:,2);
Z3 = rotdim(data{1,3});
X3 = Z3(:,1);
Y3 = Z3(:,2);
plot(X1,Y1,'r*','MarkerSize',8);
hold on;
plot(X2,Y2,'gs','MarkerSize',8);
hold on;
plot(X3,Y3,'b.','MarkerSize',8);
pause;
Z = cat(1,Z1,Z2,Z3);
indiZ = [ Z1 Z2 Z3];
function R = GaussianKernel(X,Y)
%power = (X - Y)*(X - Y)';
power = sum((X-Y).^2);
R = exp(-1*power/2*1);
return;
end
MStar = zeros(length(Z),1);
for i = 1:length(MStar)
for j = 1:length(Z)
MStar(i,1) += GaussianKernel(Z(i,:),Z(j,:));
endfor
MStar(i,1) = MStar(i,1) / length(Z);
endfor
M = zeros(length(Z),3);
for i = 1:3
temp = i*2;
for j = 1:length(Z)
for z = 1:length(indiZ(:,temp-1:temp))
M(j,i) += GaussianKernel(Z(j,:),indiZ(z,temp-1:temp));
endfor
M(j,i) = M(j,i) / length(indiZ(:,temp-1:temp));
endfor
endfor
K1 = zeros(length(Z),length(Z1));
K2 = zeros(length(Z),length(Z2));
K3 = zeros(length(Z),length(Z3));
for i = 1:length(Z)
for j = 1:length(Z1)
K1(i,j) = GaussianKernel(Z(i,:),indiZ(j,1:2));
endfor
for j = 1:length(Z2)
K2(i,j) = GaussianKernel(Z(i,:),indiZ(j,3:4));
endfor
for j = 1:length(Z3)
K3(i,j) = GaussianKernel(Z(i,:),indiZ(j,5:6));
endfor
endfor
I = eye(length(Z1));
T1 = 1/17 * ones(length(Z1));
T2 = I - T1;
N = K1*T2*K1' + K2*T2*K2' + K3*T2*K3';
VeraM = zeros(length(Z));
for i = 1:3
temp = i*2;
VeraM += 17 * (M(:,i) - MStar) * (M(:,i) - MStar)';
endfor
Matrx = pinv(N + 0.1 * eye(length(N)))*VeraM;
[v,lambda] = eigs(Matrx);
v1 = v(:,6);
#{
saved_name = 'TEST';
s = load('TRAINTEST2D.mat',saved_name);
d = s.(saved_name);
data = d{1,4};
Z1 = rotdim(data{1,1});
Z2 = rotdim(data{1,2});
Z3 = rotdim(data{1,3});
indiZ = [ Z1 Z2 Z3];
#}
zeroVect = zeros(length(Z1),1);
nZ1 = zeros(length(Z1),1);
for i = 1:length(Z1)
K = zeros(length(Z),1);
for j = 1:length(Z)
K(j) = GaussianKernel(indiZ(i,1:2),Z(j,:));
endfor
nZ1(i,1) = v1' * K;
endfor
plot(nZ1,zeroVect,'r*','MarkerSize',8);
hold on;
nZ2 = zeros(length(Z1),1);
for i = 1:length(Z1)
K = zeros(length(Z),1);
for j = 1:length(Z)
K(j) = GaussianKernel(indiZ(i,3:4),Z(j,:));
endfor
nZ2(i,1) = v1' * K;
endfor
plot(nZ2,zeroVect,'gs','MarkerSize',8);
hold on;
nZ3 = zeros(length(Z1),1);
for i = 1:length(Z1)
K = zeros(length(Z),1);
for j = 1:length(Z)
K(j) = GaussianKernel(indiZ(i,5:6),Z(j,:));
endfor
nZ3(i,1) = v1' * K;
endfor
plot(nZ3,zeroVect,'b.','MarkerSize',8);
title("Testing Data");
pause;