模式识别 感知器,fisher ,matlab实现
x1=[1.24,1.27;1.36, 1.74;1.38, 1.64;1.38, 1.82;1.38, 1.90;1.40, 1.70;1.48, 1.82;1.54, 1.82;1.56, 2.08;];
x2=[1.14, 1.82; 1.18, 1.96;1.20, 1.86;1.26, 2.0;1.28, 2.0;1.3, 1.96];
1、感知机算法实现:利用以上数据,用matlab或者其他程序写出增广矩阵,规范化处理结果,以及a转置的更新结果;判定A[1.24, 1.8],B=[1.40, 2.04]的类别。
源代码:
clear;clc;
%% 载入数据
y1 = [1.24,1.27,1;
1.36, 1.74,1;
1.38, 1.64,1;
1.38, 1.82,1;
1.38, 1.90,1;
1.40, 1.70,1;
1.48, 1.82,1;
1.54, 1.82,1;
1.56, 2.08,1;];
y2=[1.14, 1.82,-1;
1.18, 1.96,-1;
1.20, 1.86,-1;
1.26, 2.0,-1;
1.28, 2.0,-1;
1.3, 1.96,-1];
data=cat(1,y1,y2);
X=data(:,[1,2]); y=data(:,3);
m=size(X,1); % m=样本点个数
plotData1(X,y);%先在图上将样本画出来
axis([1 2 1 2.5]);
hold on
x1=1:0.1:2.5; %x1坐标轴
E=[1,1];
X=cat(1,E,X);%增广矩阵
W=[0;0]; b=0;
p=1; %步长
error=1;
while error>0
error=0;
for i=1:m
if (((W'*X(i,:)'+b)*y(i))<=0)
error=error+1;
W=W+p*y(i)*X(i,:)';
b=b+p*y(i);
y1=(-W(1)*x1-b)/W(2);
plot(x1,y1,'-b','Linewidth',0.5);
pause(0.5);
end
end
end
plot(x1,y1,'-r','Linewidth',2);
hold on
A=[1.24,1.8];
C=A;
deal(C,A,W);
B=[1.40,2.04];
C=B;
deal(C,B,W);
function deal(C,A,W)
if W'.*A>=0
k=1;
plot(C(1,1),C(1,2),'c+','Linewidth',2,'MarkerSize',9);
else k=-1;
plot(C(1,1),C(1,2),'co','MarkerFaceColor','r','Linewidth',2,'MarkerSize',7);
end
end
function plotData1(X,y)
figure;hold on;
pos=find(y==1);
neg=find(y==-1);
plot(X(pos,1),X(pos,2),'k+','Linewidth',2,'MarkerSize',9);
plot(X(neg,1),X(neg,2),'ko','MarkerFaceColor','r','Linewidth',2,'MarkerSize',7);
hold off;
end
结果输出:
E=[1,1];
X=cat(1,E,X);%实现增广矩阵,分类如上;
所以A,B均属于x2类。
2、fisher判别算法实现: 利用以上数据,写出判定函数g(x),用matlab或者其他程序实现判定程序,并判定A[1.24, 1.8],B=[1.40, 2.04]的类别。给出g(x)具体形式,程序和判定结果。
clear;clc;
%% 载入数据
x1 = [1.24,1.27;
1.36, 1.74;
1.38, 1.64;
1.38, 1.82;
1.38, 1.90;
1.40, 1.70;
1.48, 1.82;
1.54, 1.82;
1.56, 2.08;];
[m n]=size(x1);
y1=[x1 ones(m,1)];
y2=[1.14, 1.82,-1;
1.18, 1.96,-1;
1.20, 1.86,-1;
1.26, 2.0,-1;
1.28, 2.0,-1;
1.3, 1.96,-1];
A=[1.24,1.8];
B=[1.40,2.04];
data=cat(1,y1,y2);
X=data(:,[1,2]); y=data(:,3);
m=size(X,1); % m=样本点个数
plotData1(X,y);%先在图上将样本画出来
axis([1 2 1 2.5]);
hold on
x2=y2(:,[1,2]);
a=fisher(x1,x2,A);
deal(A,a);
b=fisher(x1,x2,B);
deal(B,b);
function deal(C,k)
if k==1;
plot(C(1,1),C(1,2),'c+','Linewidth',2,'MarkerSize',9);
else k==-1;
plot(C(1,1),C(1,2),'co','MarkerFaceColor','b','Linewidth',2,'MarkerSize',7);
end
end
function plotData1(X,y)
figure;hold on;
pos=find(y==1);
neg=find(y==-1);
plot(X(pos,1),X(pos,2),'k+','Linewidth',2,'MarkerSize',9);
plot(X(neg,1),X(neg,2),'ko','MarkerFaceColor','r','Linewidth',2,'MarkerSize',7);
hold off;
end
function y=fisher(x1,x2,sample)
%Fisher函数
r1=size(x1,1);r2=size(x2,1);
m1=mean(x1)';m2=mean(x2)';
s1=cov(x1)*(r1-1);s2=cov(x2)*(r2-1);
sw=s1+s2;
w=inv(sw)*(m1-m2);
y1=mean(w'*m1);
y2=mean(w'*m2);
y0=(r1*y1+r2*y2)/(r1+r2);
g=w'*sample';
if g>y0
y=1;
else
y=-1;
end
End
结果:A,B均为x2类。
选做:在二维空间产生三分类问题的数据,实现多类线性判别函数算法,并用数据进行测试。
并非用严格的线性判定函数实现而是用二类的多次判定实现二维3类的分类。
Function main
clc;clear;
x1=[0.45,0.45;
0.23,0.43;
0.34,0.49;
0.13,0.50;
0.28,0.46;];
x2=[1.23,0.35;
1.42,0.45;
1.22,0.50;
1.37,0.12;
1.80,0.44;];
x3= [1.24,1.27;
1.36, 1.74;
1.38, 1.64;
1.18, 1.82;
1.28, 1.90;
1.40, 1.70;
1.48, 1.82;
1.54, 1.82;
1.56, 2.08;];
[m1,n1]=size(x1);
[m2,n2]=size(x2);
[m3,n3]=size(x3);
p1=ones(m1,1);
p2=2*ones(m2,1);
p3=3*ones(m3,1);
p=cat(1,p1,p2,p3);
data=cat(1,x1,x2,x3);
plotData1(data,p);
axis([0:0.01:2; 0:0.01:2]);
hold on
x1=0:0.01:2; %x1坐标轴
class=3;%类别数
n=m1+m2+m3;%样本点个数
sample=[1.2,1,3];
y1=fisher(x1,x2,sample);
y2=fisher(x1,x3,sample);
y3=fisher(x2,x3,sample);
if y1*y2>0
plot(sample(1,1),sample(fir1,2),'c+','Linewidth',2,'MarkerSize',9);
else if y1*y3>0
plot(sample(1,1),sample(1,2),'co','MarkerFaceColor','r','Linewidth',2,'MarkerSize',7);
else
plot(sample(1,1),sample(1,2),'c*','MarkerFaceColor','g','Linewidth',2,'MarkerSize',7);
end
function y=fisher(x1,x2,sample)
%Fisher函数
r1=size(x1,1);r2=size(x2,1);
m1=mean(x1)';m2=mean(x2)';
s1=cov(x1)*(r1-1);s2=cov(x2)*(r2-1);
sw=s1+s2;
w=inv(sw)*(m1-m2);
y1=mean(w'*m1);
y2=mean(w'*m2);
y0=(r1*y1+r2*y2)/(r1+r2);
g=w'*sample';
if g>y0
y=1;
else
y=-1;
end
end
function plotData1(X,y)
figure;hold on;
fir=find(y==1);
sec=find(y==2);
tri=find(y==3);
plot(X(fir,1),X(fir,2),'k+','Linewidth',2,'MarkerSize',9);
plot(X(sec,1),X(sec,2),'ko','MarkerFaceColor','r','Linewidth',2,'MarkerSize',7);
plot(X(tri,1),X(tri,2),'k*','MarkerFaceColor','g','Linewidth',2,'MarkerSize',7);
hold off;
end