% ===============================Fisher===============================
% X: 训练样本
% x: 待判样本
% N: 每类样本数
% W: 权向量
% 对应课本Page88
% =====================================================================
clear,close all;
N=150;
X = [randn(N,2)+2*ones(N,2);...
randn(N,2)-2*ones(N,2);];
% ====================================================================
fig=figure;
plot(X(1:N,1),X(1:N,2),'r.')
hold on,plot(X(N+1:2*N,1),X(N+1:2*N,2),'b.')
title('初始样本分布图'),axis([-10,10,-10,10])
% ====================================================================
m1=mean(X(1:N,:));
m2=mean(X(N+1:2*N,:));
S1=0;S2=0;
for i=1:N
S1=S1+(X(i,:)-m1)*(X(i,:)-m1)';
end
for i=N+1:2*N
S1=S1+(X(i,:)-m1)*(X(i,:)-m1)';
end
Sw=S1+S2;
W=inv(Sw)*(m1-m2);
W=W./norm(W)
% ====================================================================
x=randn(1,2);%待判样本
y0=W*(m1+m2)'/2;
if W*x'>y0
disp('待判样本属于第一类')
hold on,plot(x(1),x(2),'r+','MarkerSize',10,'LineWidth',2)
else
disp('待判样本属于第二类')
hold on,plot(x(1),x(2),'b+','MarkerSize',10,'LineWidth',2)
end
legend('Cluster 1','Cluster 2','x','Location','NW')
% =================画投影直线=====================
X1=-8:0.05:8;
X2=(W(2)/W(1))*X1-6;
hold on,plot(X1,X2,'k','LineWidth',2);
% ================求投影直线上的阈值点============
x0=W(1)*(y0)/W(2);
y0=W(2)^2*y0-6*W(1)^2+W(1)*W(2)*x0;
x0=(y0+6)*W(1)/W(2);
hold on,plot(x0,y0,'ro','MarkerSize',10);
% =============求待判样本在投影直线上的投影点==============
y1=W(1)^2*x(1)+6*W(1)*W(2)+W(1)*W(2)*x(2);
y2=W(2)/W(1)*y1-6;
hold on,plot(y1,y2,'r.','MarkerSize',30);
hold on,plot([x(1) y1],[x(2) y2],'g','LineWidth',2);