线性判别分析 (LDA)是对费舍尔的线性鉴别方法(FLD)的归纳,属于监督学习的方法。LDA使用统计学,模式识别和机器学习方法,试图找到两类物体或事件的特征的一个线性组合,以能够特征化或区分它们。所得的组合可用来作为一个线性分类器,或者,更常见的是,为后续的分类做降维处理。
LDA的基本思想是将高维的模式样本投影到最佳鉴别矢量空间,以达到抽取分类信息和压缩特征空间维数的效果,投影后保证模式样本在新的子空间有最大的类间距离和最小的类内距离,即模式在该空间中有最佳的可分离性。因此,它是一种有效的特征抽取方法。使用这种方法能够使投影后模式样本的类间散布矩阵最大,并且同时类内散布矩阵最小。就是说,它能够保证投影后模式样本在新的空间中有最小的类内距离和最大的类间距离,即模式在该空间中有最佳的可分离性。
根据基本思想,我们需要将样本点投影到最佳鉴别矢量空间,达到以下两个要求:最大化类间距离与最小化类中样本方差。
我们设矢量空间的weight vector为 w⃗ 且其长度为1
假设我们有两个类的样本类I和类A,则均值
协方差矩阵所刻画的是该类与样本总体之间的关系,其中该矩阵对角线上的函数所代表的是该类相对样本总体的方差(即分散度),而非对角线上的元素所代表是该类样本总体均值的协方差(即该类和总体样本的相关联度或称冗余度)
类内散度矩阵 Total within class variance
由于是两类数据,因此我们只需要将数据投影到一条直线上即可。假设我们的投影直线是向量 w ,则对任意一个样本本 xi ,它在直线w的投影为 wTxi ,对于我们的两个类别的中心点 μ0,μ1 ,在在直线w的投影为 wTμ0 和 wTμ1 。由于LDA需要让不同类别的数据的类别中心之间的距离尽可能的大,也就是我们要最大化 ||wTμ0−wTμ1||22 ,同时我们希望同一种类别数据的投影点尽可能的接近,也就是要同类样本投影点的协方差 wTΣ0w 和 wTΣ1w 尽可能的小,即最小化 wTΣ0w+wTΣ1w 。
myfld.m
% FLD classifies an input sample into either class 1 or class 2.
%
% [output_class w] = myfld(input_sample, class1_samples, class2_samples)
% classifies an input sample into either class 1 or class 2,
% from samples of class 1 (class1_samples) and samples of
% class 2 (class2_samples).
%
% Input parameters:
% input_sample = an input sample
% - The number of dimensions of the input sample is N.
%
% class1_samples = a NC1xN matrix
% - class1_samples contains all samples taken from class 1.
% - The number of samples is NC1.
% - The number of dimensions of each sample is N.
%
% class2_samples = a NC2xN matrix
% - class2_samples contains all samples taken from class 2.
% - The number of samples is NC2.
% - NC1 and NC2 do not need to be the same.
% - The number of dimensions of each sample is N.
%
% Output parameters:
% output_class = the class to which input_sample belongs.
% - output_class should have the value either 1 or 2.
%
% w = weight vector.
% - The vector length must be one.
%
function [output_class w] = myfld(input_sample, class1_samples, class2_samples)
[m1, n1] = size(class1_samples);
[m2, n2] = size(class2_samples);
mu1 = sum(class1_samples) / size(class1_samples, 1);
mu2 = sum(class2_samples) / size(class2_samples, 1);
s1 = 0;
s2 = 0;
for i = 1:m1
s1 = s1 + (class1_samples(i,:) - mu1)' * (class1_samples(i,:) - mu1);
end
for i = 1:m2
s2 = s2 + (class2_samples(i,:) - mu2)' * (class2_samples(i,:) - mu2);
end
sw = s1 + s2;
w = inv(sw)' * (mu1 - mu2)';
w1 = w(1) / (w(1) * w(1) + w(2) * w(2))^0.5;
w2 = w(2) / (w(1) * w(1) + w(2) * w(2))^0.5;
w = [w1; w2];
separationPoint = (mu1 + mu2) * w * 0.5;
output_class = (input_sample * w < separationPoint) + 1;
LDA用于降维,和PCA有很多相同,也有很多不同的地方,因此值得好好的比较一下两者的降维异同点。
相同点:
不同点:
这点可以从下图形象的看出,在某些数据分布下LDA比PCA降维较优。
主要优点:
主要缺点: