1. 程式人生 > >MATLAB實現LDA線性判別分析

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;

下面是執行後的結果 影象符合的還是可以的 下面是用周志華西瓜書資料集進行的,效果沒有上面的明顯 在這裡插入圖片描述 還進行過二組平行直線的判別。結果沒有很好符合。可能是選擇特徵向量時捨去的那部分導致的。