-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsvr.m
More file actions
63 lines (52 loc) · 1.12 KB
/
svr.m
File metadata and controls
63 lines (52 loc) · 1.12 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
fID = fopen('SVR_dataset.txt','r');
formatSpec = '%f %f';
A = fscanf(fID, formatSpec);
M = reshape(A, [2,50])' ;
h = 0.5;
C = 4;
eps = 0.1 ;
s=size(M);
n=s(1,1);
x=M(:,1);
y=M(:,2);
for i=1:n
for j=1:n
normSq = (norm(x(i)-x(j)))^2;
K(i,j)= exp(- (normSq/(2*h*h)) );
end
end
z=zeros(n,1);
cvx_begin
variable a(n);
variable a_star(n);
maximize((-1*0.5*(a-a_star)'*K*(a-a_star))-eps*sum(a+a_star)+(a-a_star)'*y);
subject to
a >= 0;
a_star >= 0;
a <= C;
a_star <= C;
cvx_end
ans=cvx_optval;
tps=50;
x_test=linspace(0,1,tps);
y_test=zeros(size(x_test));
for i=1:tps
kx=zeros(n,1);
for j=1:n
normSq = (norm(x(j)-x_test(i)))^2;
kx(j)= exp(- (normSq/(2*h*h)) );
end
y_test(i)=(a-a_star)'*kx;
end
supp_vec_a=a-a_star;
supp_vecs_x=zeros(n,1);
supp_vecs_y=zeros(n,1);
for i=1:n
if abs(supp_vec_a(i)) > C-exp(-5)
supp_vecs_x(i)=x(i);
supp_vecs_y(i)=y(i);
end
end
plot(x_test,y_test);hold on;
scatter(x,y);hold on;
scatter(supp_vecs_x,supp_vecs_y,'g');