📄 xor.m
字号:
%------------------------SVM-------------------------%
%-----------------------start------------------------%
%divide the data into two parts
%one part is:all x1 and half of x2
x1(1,1)=-1;x1(1,2)=-1;
x1(2,1)=1;x1(2,2)=1;
x2(1,1)=-1;x2(1,2)=1;
epsilon = 0;
N1=size(x1);
N2=size(x2);
X=[x1;x2];
Y1=ones(N1,1);
Y2=-ones(N2,1);
Y=[Y1;Y2];
x0=rands(N1+N2,1);
A=[];
b=[];
Aeq=[];
beq=[];
lb=[];
ub=[];
lambda = ones(N1+N2,1);
options = optimset('LargeScale','off');
[lambda, fval] = fmincon(@fun,lambda,[],[],[],[],[],[],@nonlcon,options); %find the lagrange coefficient-lambda
w = X'*(lambda.*Y);
svi = find(lambda > epsilon);
for i=1:(N1+N2)
for j=1:(N1+N2)
H(i,j) = Y(i)*Y(j)*(X(i,:)*X(j,:)');
end
end
w0 = (1/length(svi))*sum(Y(svi) - H(svi,svi)*lambda(svi).*Y(svi));
hx(1) = -2; hx(2) = 2; %find the hyperplane
hy(1) = (-w0-hx(1)*w(1))/w(2);
hy(2) = (-w0-hx(2)*w(1))/w(2);
figure(1);
plot(x1(:,1), x1(:,2), '*');
hold on;
plot(x2(:,1), x2(:,2), 'o');
axis([-2,2,-2,2])
hold on;
plot(hx,hy)
hold on;
plot(X(svi,1),X(svi,2),'or');
%other part is:all x1 and the left x2
x1(1,1)=-1;x1(1,2)=-1;
x1(2,1)=1;x1(2,2)=1;
x2(1,1)=1;x2(1,2)=-1;
epsilon = 0;
N1=size(x1);
N2=size(x2);
X=[x1;x2];
Y1=ones(N1,1);
Y2=-ones(N2,1);
Y=[Y1;Y2];
x0=rands(N1+N2,1);
A=[];
b=[];
Aeq=[];
beq=[];
lb=[];
ub=[];
lambda1 = ones(N1+N2,1);
options = optimset('LargeScale','off');
[lambda1, fval] = fmincon(@fun1,lambda1,[],[],[],[],[],[],@nonlcon1,options); %find the lagrange coefficient-lambda
w = X'*(lambda1.*Y);
svi = find(lambda1 > epsilon);
for i=1:(N1+N2)
for j=1:(N1+N2)
H(i,j) = Y(i)*Y(j)*(X(i,:)*X(j,:)');
end
end
w0 = (1/length(svi))*sum(Y(svi) - H(svi,svi)*lambda1(svi).*Y(svi));
hx(1) = -2; hx(2) = 2; %find the hyperplane
hy(1) = (-w0-hx(1)*w(1))/w(2);
hy(2) = (-w0-hx(2)*w(1))/w(2);
plot(x1(:,1), x1(:,2), '*');
hold on;
plot(x2(:,1), x2(:,2), 'o');
axis([-2,2,-2,2]);
hold on;
plot(hx,hy)
hold on;
plot(X(svi,1),X(svi,2),'or');
%-------------------------SVM------------------------%
%-------------------------end------------------------%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -