判别或预测方法汇总(判别分析、神经网络、支持向量机SVM等)

%% 【Input】:s_train(输入样本数据,行数为样本数,列为维数);s_group(训练样本类别);s_sample(待判别数据)
%% 【Output】:Cla(预测类别)

function Cla = fun_panbie(s_train,s_group,s_sample,index )
switch index
case 1%Classify
%[s_train,~]=mapminmax(s_train);%标准化处理
%[s_sample,~]=mapminmax(s_sample);%标准化处理
[Cla,err,posterior,~,~]=classify(s_sample,s_train,s_group,'linear','empirical');%SS={'linear','diaglinear','quadratic','diagquadratic','mahalanobis'};%判别函数
case 2%SVM
net=svmtrain(s_train,s_group,'kernel_function','linear');%核函数SS={'linear','quadratic','polynomial','rbf','mlp'}
Cla=svmclassify(net,s_sample);
case 3%knnclassify
%略
case 4%RBF
net=newrb(s_train',s_group',0.1,0.1);
Cla=int16(net(s_sample))';
case 5%LVQ
T=ind2vec(s_group'+1);
net=newlvq(minmax(s_train'),5);
net.trainParam.showWindow=0;
net=train(net,s_train',T);
y=sim(net,s_sample');
Cla=vec2ind(y)'-1;
case 6%Elman
[pn,minp,maxp,tn,mint,maxt]=premnmx(s_train',s_group');%数据归一化
p2= tramnmx(s_sample',minp,maxp);
net=newelm(minmax(pn),[3,size(s_group,2)],{'tansig','tansig'});%建立网络模型,其中参数可以根据要求修改
net.trainparam.show=100;%每迭代100次显示1次
net.trainparam.epochs=1000;%最大迭代次数2000
net.trainparam.goal=0.001;%迭代目标
net=init(net);%初始化网络
net.trainParam.showWindow=0;%神经网络训练过程的窗口不弹出来
[net,tr]=train(net,pn,tn);%训练网络
Cla=sim(net,s_sample')';%仿真
Cla=postmnmx(Cla,mint,maxt);%仿真值反归一化
case 7%单层感知器
YY=minmax(s_train');
net=newp(YY,1);
net.trainParam.epochs=20;
net.trainParam.showWindow=0;
net=train(net,s_train',s_group');
Cla=sim(net,s_sample)';
case 8%线性神经网络
T=repmat(s_group,1,size(s_train',2));
net=newlin(minmax(s_train'),size(T,1),10,0.05);
net.trainParam.epochs=500;
net.trainParam.goal=0.0001;
net.trainParam.showWindow=0;
net=train(net,s_train',T);
y=sim(net,s_sample');
Cla=y(:,1);
case 9%单层竞争神经网络
mm=s_train(:);
mm=minmax(mm');
Q=repmat(mm,size(s_train,2),1);
net=newc(Q,2,0.1);
net=init(net);
net.trainParam.showWindow=0;
net.trainparam.epochs=20;
net=train(net,s_train');
a=sim(net,s_train');
Cla=vec2ind(s_sample')';
case 10%BP神经网络
mm=s_train(:);
mm=minmax(mm');
net=newff(repmat(mm,size(s_train',1),1),[5,3,3,1],{'tansig','tansig','tansig','purelin'},'traingd');
net.trainparam.epochs=300;
net.trainparam.lr=0.05;
net.trainparam.show=50;
net.trainparam.goal=1e-5;
net.trainParam.showWindow=0;
[net,tr]=train(net,s_train',s_group');
Cla=sim(net,s_sample')';
otherwise

end

end

上一篇:java计时代码


下一篇:Effective Java2读书笔记-创建和销毁对象(一)