R
再現線性判別分析投影圖
我正在與線性判別分析(LDA)中的投影點作鬥爭。許多關於多元統計方法的書籍都用下圖說明了 LDA 的思想。
問題描述如下。首先,我們需要繪製決策邊界,添加垂直線,然後在其上繪製數據點的投影。我想知道如何將投影點添加到垂直線。
有什麼建議/指示嗎?
判別軸(點在圖 1 上投影到的軸)由第一個特徵向量給出. 在只有兩個類的情況下,這個特徵向量與, 在哪裡是類質心。對這個向量(或得到的特徵向量)進行歸一化,得到單位軸向量. 這足以繪製軸。
要將(中心)點投影到該軸上,您只需計算. 這裡是一個線性投影儀.
以下是您的保管箱和 LDA 投影中的數據樣本:
這是生成此圖的 MATLAB 代碼(根據要求):
% # data taken from your example X = [-0.9437 -0.0433; -2.4165 -0.5211; -2.0249 -1.0120; ... -3.7482 0.2826; -3.3314 0.1653; -3.1927 0.0043; -2.2233 -0.8607; ... -3.1965 0.7736; -2.5039 0.2960; -4.4509 -0.3555]; G = [1 1 1 1 1 2 2 2 2 2]; % # overall mean mu = mean(X); % # loop over groups for g=1:max(G) mus(g,:) = mean(X(G==g,:)); % # class means Ng(g) = length(find(G==g)); % # number of points per group end Sw = zeros(size(X,2)); % # within-class scatter matrix Sb = zeros(size(X,2)); % # between-class scatter matrix for g=1:max(G) Xg = bsxfun(@minus, X(G==g,:), mus(g,:)); % # centred group data Sw = Sw + transpose(Xg)*Xg; Sb = Sb + Ng(g)*(transpose(mus(g,:) - mu)*(mus(g,:) - mu)); end St = transpose(bsxfun(@minus,X,mu)) * bsxfun(@minus,X,mu); % # total scatter matrix assert(sum(sum((St-Sw-Sb).^2)) < 1e-10, 'Error: Sw + Sb ~= St') % # LDA [U,S] = eig(Sw\Sb); % # projecting data points onto the first discriminant axis Xcentred = bsxfun(@minus, X, mu); Xprojected = Xcentred * U(:,1)*transpose(U(:,1)); Xprojected = bsxfun(@plus, Xprojected, mu); % # preparing the figure colors = [1 0 0; 0 0 1]; figure hold on axis([-5 0 -2.5 2.5]) axis square % # plot discriminant axis plot(mu(1) + U(1,1)*[-2 2], mu(2) + U(2,1)*[-2 2], 'k') % # plot projection lines for each data point for i=1:size(X,1) plot([X(i,1) Xprojected(i,1)], [X(i,2) Xprojected(i,2)], 'k--') end % # plot projected points scatter(Xprojected(:,1), Xprojected(:,2), [], colors(G, :)) % # plot original points scatter(X(:,1), X(:,2), [], colors(G, :), 'filled')