问题描述
我正在使用Matlab命令 fitcdiscr 来实现具有379个功能和8个类的LDA。我想对每个功能进行全局加权,以研究它们在预测中的影响。如何从Classificationdiscriminant对象的Coeffs字段中的成对系数(对于每对类别)获得它?
解决方法
似乎fitcdiscr
不会输出特征值或特征向量。
由于网络上有大量文档,因此在这里我将不解释特征向量和特征值。但是基本上,生成的特征向量将确定使每个类之间的距离最大化的轴。
我写了一个最小的示例(受此excellent article启发)输出了两个示例:
% We load the fisheriris dataset
load fisheriris
feature = meas; % 150x4 array
class = species; % 150x1 cell
% Extract unique class and the corresponding index for each feature.
[ucl,~,idc] = unique(class);
% Number of parameter and number of class
np = size(meas,2);
nc = length(ucl);
% Mean by class
MBC = splitapply(@mean,feature,idc);
% Compute the Within class Scatter Matrix WSM
WSM = zeros(np);
for ii = 1:nc
FM = feature(idc==ii,:)-MBC(ii,:);
WSM = WSM + FM.'*FM;
end
WSM
% Compute the Between class Scatter Matrix
BSM = zeros(np);
GPC = accumarray(idc,ones(size(classe)));
for ii = 1:nc
BSM = BSM + GPC(ii)*((MBC(ii,:)-mean(feature)).'*(MBC(ii,:)-mean(feature)));
end
BSM
% Now we compute the eigenvalues and the eigenvectors
[eig_vec,eig_val] = eig(inv(WSM)*BSM)
% Compute the new feature:
new_feature = feature*eig_vec
使用:
eig_vec =
[-0.2087 -0.0065 0.7666 -0.4924 % -> feature 1
-0.3862 -0.5866 -0.0839 0.4417 % -> feature 2
0.5540 0.2526 -0.0291 0.2875 % -> feature 3
0.7074 -0.7695 -0.6359 -0.5699] % -> feature 4
% So the first new feature is a linear combination of
% -0.2087*feature1 + -0.3862*feature2 + 0.5540*feature3 + 0.7074*feature4
eig_val =
[ 32.1919 % eigen value of the new feature 1
0.2854 % eigen value of the new feature 2
0.0000 % eigen value of the new feature 3
-0.0000] % eigen value of the new feature 4
在这种情况下,我们有4个特征,这是这4个特征(1类= 1种颜色)的直方图:
如果我们想区分不同的类而不是完美的,我们发现功能3和4相当不错。
现在,在LDA之后,我们具有以下新功能:
我们看到几乎所有信息都已收集到第一个新功能(新功能1)中。所有其他功能都没用,因此仅保留new feature 1
并删除另一个功能。现在,我们有了1D数据集,而不是4D数据集。