Machine Learning Experiment 3: Linear Discriminant Analysis 詳解+源代碼解析

  1. LDA for 2 Classes

首先,繪製原始數據:

  1. 查看LDA步驟

其中

  1. 設計代碼
  1. 加載數據,並繪製三個類的圖像

X1=load('ex3red.dat');

X2=load('ex3green.dat');

X3=load('ex3blue.dat');

hold on

scatter(X1(:,1),X1(:,2),'r')

scatter(X2(:,1),X2(:,2),'g')

scatter(X3(:,1),X3(:,2),'blue')

grid on

  1. 計算均值和以及Sw,Sb

mu1=mean(X1)

mu2=mean(X2)

mu3=mean(X3)

Sw=(X1-mu1)'*(X1-mu1)+(X3-mu3)'*(X3-mu3)

SW=(X1-mu1)'*(X1-mu1)+(X2-mu2)'*(X3-mu3)...

    +(X3-mu3)'*(X3-mu3)

Sb=(mu1-mu3)'*(mu1-mu3)

SB=(mu1-mu3)'*(mu1-mu3)+(mu1-mu2)'*(mu1-mu2)...

    +(mu2-mu3)'*(mu2-mu3)

其中

Sw = 2×2

    4.8008   -1.4172

   -1.4172   29.0730

SW = 2×2
    6.2625   -2.8762
   -1.7051   29.6357
Sb = 2×2
    1.5482    3.2831
    3.2831    6.9621
SB = 2×2
   16.8330   18.0347
   18.0347   22.2876

計算theta:theta=(Sw)\(mu1-mu3)'

此處沒有使用inv(Sw),因爲這樣相比於直接反除會損失精度。

theta = 2×1
    0.2901
    0.1049

(3) 繪製圖像如下:

 其中,直線的斜率爲theta(2)/theta(1)

  1. 將數據投影到直線

根據兩向量之間的夾角以及斜率計算投影之後的x,y座標

計算方式如下:

%投影在直線上的長度

L1=(X1*theta)/sqrt(theta(2)^2+theta(1)^2)

L2=(X3*theta)/sqrt(theta(2)^2+theta(1)^2)

%斜率

k=theta(2)/theta(1)

x1=L1/sqrt(1+k^2)

x2=L2/sqrt(1+k^2)

y1=x1*k

y2=x2*k

繪製圖像如下:

  1. 對於3-classes

步驟如下:

此時計算了矩陣的特徵值和特徵向量,調用MATLAB中的庫函數

[V,D]=eig(SW\SB) %其中 V是特徵向量矩陣,D是特徵值對角矩陣

w1=V(:,1)

w2=V(:,2)

其中w1和w2是兩個特徵向量。

V = 2×2

    0.9670   -0.7437

    0.2546    0.6685

D = 2×2

    3.9201         0

         0    0.0705

w1 = 2×1

    0.9670

    0.2546

w2 = 2×1

   -0.7437

    0.6685

 

對於第一個特徵向量繪製圖像如下:

對於第二個特徵向量w2:

可以看到,顯然我們要使用對應特徵值最大的那個向量,即w1。

 

 

MATLAB源代碼

X1=load('ex3red.dat');
X2=load('ex3green.dat');
X3=load('ex3blue.dat');
hold on 
scatter(X1(:,1),X1(:,2),'r')
scatter(X2(:,1),X2(:,2),'g')
scatter(X3(:,1),X3(:,2),'blue')
grid on 
%two classes
figure
hold on 
scatter(X1(:,1),X1(:,2),'r','filled')
scatter(X3(:,1),X3(:,2),'blue','filled')
title('red and blue')
xlabel('x')
ylabel('y')
xlim([0.00 10.00])
ylim([0.00 10.00])
mu1=mean(X1)
mu2=mean(X2)
mu3=mean(X3)
Sw=(X1-mu1)'*(X1-mu1)+(X3-mu3)'*(X3-mu3)
SW=(X1-mu1)'*(X1-mu1)+(X2-mu2)'*(X3-mu3)...
    +(X3-mu3)'*(X3-mu3)
Sb=(mu1-mu3)'*(mu1-mu3)
SB=(mu1-mu3)'*(mu1-mu3)+(mu1-mu2)'*(mu1-mu2)...
    +(mu2-mu3)'*(mu2-mu3)
[V,D]=eig(SW\SB) %其中 V是特徵向量矩陣,D是特徵值對角矩陣
w1=V(:,1)
w2=V(:,2)
theta=(Sw)\(mu1-mu3)'
disp(theta(1,1))
figure
hold on 
scatter(X1(:,1),X1(:,2),'r','filled')
scatter(X3(:,1),X3(:,2),'blue','filled')
x=linspace(0,10,100);
y=(theta(2)/theta(1))*x
plot(x,y,'black')
title('LDA for two-classes')
xlabel('x')
ylabel('y')
xlim([0.00 10.00])
2-ylim([0.00 10.00])
%投影在直線上的長度
L1=(X1*theta)/sqrt(theta(2)^2+theta(1)^2)
L2=(X3*theta)/sqrt(theta(2)^2+theta(1)^2)
%斜率
k=theta(2)/theta(1)
x1=L1/sqrt(1+k^2)
x2=L2/sqrt(1+k^2)
y1=x1*k
y2=x2*k
figure
hold on 
scatter(X1(:,1),X1(:,2),'r','filled')
scatter(X3(:,1),X3(:,2),'blue','filled')
scatter(x1',y1','red','x')
scatter(x2',y2','blue','x')
x=linspace(0,10,100);
y=(theta(2)/theta(1))*x
plot(x,y,'black')
title('LDA')
xlabel('x')
ylabel('y')
xlim([0.00 10.00])
ylim([0.00 10.00])
D1=(X1*w1)/sqrt(w1(2)^2+w1(1)^2)
D2=(X2*w1)/sqrt(w1(2)^2+w1(1)^2)
D3=(X3*w1)/sqrt(w1(2)^2+w1(1)^2)
K=w1(2)/w1(1)
x1=D1/sqrt(1+K^2)
x2=D2/sqrt(1+K^2)
x3=D3/sqrt(1+K^2)
y1=x1*K
y2=x2*K
y3=x3*K
figure
hold on 
scatter(X1(:,1),X1(:,2),'r','filled')
scatter(X2(:,1),X2(:,2),'g','filled')
scatter(X3(:,1),X3(:,2),'blue','filled')
scatter(x1',y1','red','x')
scatter(x2',y2','green','x')
scatter(x3',y3','blue','x')
x=linspace(0,10,100);
y=(w1(2)/w1(1))*x
plot(x,y,'black')
D1=(X1*w2)/sqrt(w2(2)^2+w2(1)^2)
D2=(X2*w2)/sqrt(w2(2)^2+w2(1)^2)
D3=(X3*w2)/sqrt(w2(2)^2+w2(1)^2)
K=w2(2)/w2(1)
x1=D1/sqrt(1+K^2)
x2=D2/sqrt(1+K^2)
x3=D3/sqrt(1+K^2)
y1=x1*K
y2=x2*K
y3=x3*K
figure
hold on 
scatter(X1(:,1),X1(:,2),'r','filled')
scatter(X2(:,1),X2(:,2),'g','filled')
scatter(X3(:,1),X3(:,2),'blue','filled')
scatter(x1',y1','red','x')
scatter(x2',y2','green','x')
scatter(x3',y3','blue','x')
x=linspace(0,10,100);
y=(w2(2)/w2(1))*x
plot(x,y,'black')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章