2017-03-27 91 views
0

我已經對大型數據集進行了線性支持向量機,但是爲了減少我進行PCA的維數,我們在組件分數的子集上進行了SVM(前650個組件解釋了99.5%的方差)。現在我想使用來自PCA空間中創建的SVM的beta權重和偏差在原始變量空間中繪製決策邊界。但我不知道如何將SVM中的偏置項投影到原始變量空間中。我已經寫了使用漁民虹膜數據來說明一個演示:如何在Matlab中使用PCA後從線性SVM繪製決策邊界?

clear; clc; close all 

% load data 
load fisheriris 
inds = ~strcmp(species,'setosa'); 
X = meas(inds,3:4); 
Y = species(inds); 
mu = mean(X) 

% perform the PCA 
[eigenvectors, scores] = pca(X); 

% train the svm 
SVMModel = fitcsvm(scores,Y); 

% plot the result 
figure(1) 
gscatter(scores(:,1),scores(:,2),Y,'rgb','osd') 
title('PCA space') 

% now plot the decision boundary 
betas = SVMModel.Beta; 
m = -betas(1)/betas(2); % my gradient 
b = -SVMModel.Bias;  % my y-intercept 
f = @(x) m.*x + b;  % my linear equation 
hold on 
fplot(f,'k') 
hold off 
axis equal 
xlim([-1.5 2.5]) 
ylim([-2 2]) 

% inverse transform the PCA 
Xhat = scores * eigenvectors'; 
Xhat = bsxfun(@plus, Xhat, mu); 

% plot the result 
figure(2) 
hold on 
gscatter(Xhat(:,1),Xhat(:,2),Y,'rgb','osd') 

% and the decision boundary 
betaHat = betas' * eigenvectors'; 
mHat = -betaHat(1)/betaHat(2); 
bHat = b * eigenvectors'; 
bHat = bHat + mu; % I know I have to add mu somewhere... 
bHat = bHat/betaHat(2); 
bHat = sum(sum(bHat)); % sum to reduce the matrix to a single value 
% the correct value of bHat should be 6.3962 

f = @(x) mHat.*x + bHat; 
fplot(f,'k') 
hold off 

axis equal 
title('Recovered feature space') 
xlim([3 7]) 
ylim([0 4]) 

對我如何計算銖任何指導不當,將不勝感激。

+0

正確的y截距是'b = -SVMModel.Bias/beta(2)' –

回答

0

爲防萬一別人遇到這個問題,解決的辦法是用偏倚項可以找到y截距b = -SVMModel.Bias/betas(2)。 y截距只是空間[0 b]中的另一點,可以通過PCA逆變換來恢復/未旋轉。這個新點可以用來求解線性方程y = mx + b(即,b = y - mx)。所以代碼應該是:

% and the decision boundary 
betaHat = betas' * eigenvectors'; 
mHat = -betaHat(1)/betaHat(2); 
yint = b/betas(2);     % y-intercept in PCA space 
yintHat = [0 b] * eigenvectors';  % recover in original space 
yintHat = yintHat + mu;  
bHat = yintHat(2) - mHat*yintHat(1); % solve the linear equation 
% the correct value of bHat is now 6.3962