MATLAB实现LDA线性判别分析

代码如下

clear all;close all;clc;
x=[0.697,0.774,0.634,0.608,0.556,0.403,0.481,0.437,0.666,0.243,0.245,0.343,0.639,0.657,0.360,0.593,0.719];
y=[0.460,0.376,0.264,0.318,0.215,0.237,0.149,0.211,0.091,0.267,0.057,0.099,0.161,0.198,0.370,0.042,0.103];
z=[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0];
%data=[x;y;z]';
data=[4,2,2,3,4,9,6,9,8,10
    2,4,3,6,4,10,8,5,7,8
    1,1,1,1,1,0,0,0,0,0]';
[m,n]=size(data);
for i=1:m
    if data(i,3)==1
        plot(data(i,1),data(i,2),'r.','MarkerSize', 12);hold on;
    end
    if data(i,3)==0
        plot(data(i,1),data(i,2),'b.','MarkerSize', 12);
    end
end
new_data=zeros(m,n-1);
cen1=zeros(1,2);cen0=zeros(1,2);%定义类中心
sum1=zeros(1,2);sum0=zeros(1,2);
num1=0;num0=0;
%计算类中心
for i=1:m
    if data(i,3)==1
        sum1(1,1)=sum1(1,1)+data(i,1);
        sum1(1,2)=sum1(1,1)+data(i,2);
        num1=num1+1;
    end
    if data(i,3)==0
        sum0(1,1)=sum0(1,1)+data(i,1);
        sum0(1,2)=sum0(1,2)+data(i,2);
        num0=num0+1;
    end
end
cen0=sum0/num0;cen1=sum1/num1;
%计算类内散度矩阵Sw
Sw=zeros(2,2);
for i=1:m
    if data(i,3)==1
        Sw=Sw+(data(i,[1 2])-cen1(1,:))'*(data(i,[1 2])-cen1(1,:));
    end
    if data(i,3)==0
        Sw=Sw+(data(i,[1 2])-cen0(1,:))'*(data(i,[1 2])-cen0(1,:));
    end
end
%计算类间散度矩阵Sb;
Sb=(cen0-cen1)'*(cen0-cen1);
[L,D]=eigs(Sw\Sb',1);%计算最大特征值和特征向量
%显示投影线
k=L(1)/L(2);
b=0;
xx=0:10;
yy=k*xx;
plot(xx,yy)
%计算投影点并显示
new_data(:,1)=(k*data(:,2)+data(:,1))/(k*k+1);
new_data(:,2)=k*new_data(:,1);
new_data(:,3)=data(:,3);
for i=1:m
    if new_data(i,3)==1
        plot(new_data(i,1),new_data(i,2),'r+','MarkerSize', 5);
    end
    if new_data(i,3)==0
        plot(new_data(i,1),new_data(i,2),'b+','MarkerSize', 5);
    end
end
%axis([0 1 0 1])
axis([0 15 0 15])
hold on;

下面是运行后的结果
MATLAB实现LDA线性判别分析_第1张图片
下面是用周志华西瓜书数据集进行的,效果没有上面的明显
MATLAB实现LDA线性判别分析_第2张图片
还进行过二组平行直线的判别。结果没有很好符合。可能是选择特征向量时舍去的那部分导致的。

你可能感兴趣的:(MATLAB实现LDA线性判别分析)