如何用MATLAB支持向量机将2组数据分类,跪求源代码(实例),邮箱1033558779@qq.com 5
1个回答
展开全部
p1=[…………];
t1=[…………];
[aa,bb]=size(p1);
n1=bb;
for i=1:bb
p(:,i)=(p1(:,i)-min(p1(:,i)))/(max(p1(:,i))-min(p1(:,i)));
end
[c1,c2]=size(t1);
for i=1:c2
test(:,i)=(t1(:,i)-min(t1(:,i)))/(max(t1(:,i))-min(t1(:,i)));
end
n=aa;
sigma=2;
T=[ones(19,1);-ones(19,1)];
for i=1:n
for j=1:n
K(i,j)=T(i)*p(i,:)*p(j,:)'*T(j);
end
end
f=-ones(n,1)
aeq=T';
beq=0;
la=zeros(1,n);
lb=ones(1,n);
[x,fval]=quadprog(K,f,[],[],aeq,beq,la,lb);
w=0;
for j=1:n
w=w+x(j)*p(j,:)*T(j);
end
cc=find(x>0.5);
zb=cc(1);
if T(zb)<0
b1=-1-p(zb,:)*w';
else if T(zb)>0
b1=1-p(zb,:)*w';
end
end
t_test=p;
for j=1:n
f1(j)=t_test(j,:)*w'+b1;
end
for j=1:n
if f1(j)>0
class(j)=1;
else if f1(j)<0
class(j)=-1;
else class(j)=0;
end
end
end
t_test1=test;
for j=1:c1
f2(j)=t_test1(j,:)*w'+b1;
end
for j=1:c1
if f2(j)>0
class1(j)=1;
else if f2(j)<0
class1(j)=-1;
else class1(j)=0;
end
end
end
给p1和t1附上相应的矩阵即可算,支持向量机只能将数据分为两类
t1=[…………];
[aa,bb]=size(p1);
n1=bb;
for i=1:bb
p(:,i)=(p1(:,i)-min(p1(:,i)))/(max(p1(:,i))-min(p1(:,i)));
end
[c1,c2]=size(t1);
for i=1:c2
test(:,i)=(t1(:,i)-min(t1(:,i)))/(max(t1(:,i))-min(t1(:,i)));
end
n=aa;
sigma=2;
T=[ones(19,1);-ones(19,1)];
for i=1:n
for j=1:n
K(i,j)=T(i)*p(i,:)*p(j,:)'*T(j);
end
end
f=-ones(n,1)
aeq=T';
beq=0;
la=zeros(1,n);
lb=ones(1,n);
[x,fval]=quadprog(K,f,[],[],aeq,beq,la,lb);
w=0;
for j=1:n
w=w+x(j)*p(j,:)*T(j);
end
cc=find(x>0.5);
zb=cc(1);
if T(zb)<0
b1=-1-p(zb,:)*w';
else if T(zb)>0
b1=1-p(zb,:)*w';
end
end
t_test=p;
for j=1:n
f1(j)=t_test(j,:)*w'+b1;
end
for j=1:n
if f1(j)>0
class(j)=1;
else if f1(j)<0
class(j)=-1;
else class(j)=0;
end
end
end
t_test1=test;
for j=1:c1
f2(j)=t_test1(j,:)*w'+b1;
end
for j=1:c1
if f2(j)>0
class1(j)=1;
else if f2(j)<0
class1(j)=-1;
else class1(j)=0;
end
end
end
给p1和t1附上相应的矩阵即可算,支持向量机只能将数据分为两类
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询