LDA作为分类器在matlab下做实验

原文地址:http://www.cnblogs.com/cfantaisie/archive/2011/03/25/1995849.html
本文使用LDA作为分类器在matlab下做实验。
其中投影转换矩阵W按照LDA的经典理论生成,如下的LDA函数,并返回各个类的投影后的(k-1)维的类均值。
LDA.m代码如下:
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
因为LDA是二类分类器,需要推广到多类的问题。常用的方法one-vs-all方法训练K个分类器(这个方法在综合时不知道怎么处理?),以及任意两个分类配对训练分离器最后得到k(k-1)/2个的二类分类器。本文采用训练后者对样本进行训练得到模型model。在代码中,model为数组struct。
用于训练的函数LDATraining.m
http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif
function
[model,k,ClassLabel]=LDATraining(input,target)
% input: n*d matrix,representing samples
% target: n*1 matrix,class label
% model: struct type(see codes below)
% k: the total class number
% ClassLabel: the class name of each class
%
model=struct;
[n dim]=size(input);
ClassLabel=unique(target);
k=length(ClassLabel);
t=1;
for i=1:k-1
for j=i+1:k
model(t).a=i;
model(t).b=j;
g1=(target==ClassLabel(i));
g2=(target==ClassLabel(j));
tmp1=input(g1,:);
tmp2=input(g2,:);
in=[tmp1;tmp2];
out=ones(size(in,1),1);
out(1:size(tmp1,1))=0;
% tmp3=target(g1);
% tmp4=target(g2);
% tmp3=repmat(tmp3,length(tmp3),1);
% tmp4=repmat(tmp4,length(tmp4),1);
% out=[tmp3;tmp4];
[w m]=LDA(in,out);
model(t).W=w;
model(t).means=m;
t=t+1;
end
end
% input: n*d matrix,representing samples
% target: n*1 matrix,class label
% model: struct type(see codes below)
% k: the total class number
% ClassLabel: the class name of each class
%
model=struct;
[n dim]=size(input);
ClassLabel=unique(target);
k=length(ClassLabel);
t=1;
for i=1:k-1
for j=i+1:k
model(t).a=i;
model(t).b=j;
g1=(target==ClassLabel(i));
g2=(target==ClassLabel(j));
tmp1=input(g1,:);
tmp2=input(g2,:);
in=[tmp1;tmp2];
out=ones(size(in,1),1);
out(1:size(tmp1,1))=0;
% tmp3=target(g1);
% tmp4=target(g2);
% tmp3=repmat(tmp3,length(tmp3),1);
% tmp4=repmat(tmp4,length(tmp4),1);
% out=[tmp3;tmp4];
[w m]=LDA(in,out);
model(t).W=w;
model(t).means=m;
t=t+1;
end
end
在预测时,使用训练时生成的模型进行k(k-1)/2次预测,最后选择最多的分类作为预测结果。在处理二类分类器预测时,通过对预测样本作W的投影变换再比较与两个类的均值进行比较得到(不知道有没有更好的办法?)
用于预测的函数LDATesting.m
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
示例代码为:
function
target=test(in,out,t)
[model,k,ClassLabel]=LDATraining(in,out);
target=LDATesting(t,k,model,ClassLabel);
[model,k,ClassLabel]=LDATraining(in,out);
target=LDATesting(t,k,model,ClassLabel);
实验中对USPS数据集进行了测试,效果不怎么好,正确率才39%左右,而这个数据集使用KNN算法可以达到百分之百九十的正确率,汗!
分类: 机器学习
前一篇:sobel算子
后一篇:傅里叶变化(非常好,讲的很清楚)