資源簡(jiǎn)介
adaboost算法是一個(gè)由多個(gè)弱分類器生成一個(gè)強(qiáng)分類器的算法,可以提高分類的正確率,這里利用adaboost算法的原理,結(jié)合matlab做了一個(gè)簡(jiǎn)單的實(shí)例
里面h1-h8為八個(gè)弱分類器,adaboost為訓(xùn)練的主函數(shù),test調(diào)用了訓(xùn)練函數(shù),對(duì)一個(gè)樣本進(jìn)行測(cè)試,calerr計(jì)算每次循環(huán)后的錯(cuò)誤頻率

代碼片段和文件信息
function?[at?choose?T]=adaboost()
%定義樣本
p=[10;-10;01;0-1];
%樣本數(shù)目
numofs=4;
%定義正確分類結(jié)果
y=[11-1-1];
%分類器數(shù)目
numofc=8;
%預(yù)設(shè)的循環(huán)次數(shù)
cycle=5;
%實(shí)際循環(huán)次數(shù)定義為T(mén)
%定義循環(huán)過(guò)程中選擇的函數(shù)下標(biāo)和比重
at=zeros(1cycle);
choose=zeros(1cycle);
%每個(gè)樣本在每個(gè)分類器下的label
label=zeros(numofsnumofc);
%計(jì)算得到label數(shù)組的值
for?i=1:numofs
????for?j=1:numofc
????????label(ij)=h(jp(i:));
????end
end
%得到每個(gè)函數(shù)錯(cuò)誤樣本的數(shù)組
wrong=zeros(numofcnumofs);
for?i=1:numofc
????for?j=1:numofs
????????wrong(ij)=(label(ji)~=y(j));
????end
end
%初始化每個(gè)樣本的權(quán)重
weight=ones(1numofs)/numofs;
%循環(huán)開(kāi)始
%記錄每次循環(huán)中每個(gè)分類器對(duì)應(yīng)的錯(cuò)誤
err_freq=ones(1cycle);
for?k=1:cycle
????error=zeros(1numofc);
????for?m=1:numofc
????????error(m)=sum(weight(wrong(m:)==1));
????end
????[err?index]=min(error);
????%計(jì)算當(dāng)前選擇的錯(cuò)誤最小的分類器對(duì)應(yīng)的權(quán)值
????choose(k)=index;
????at(k)=log((1-err)/err)/2;
????%更新權(quán)值
????weight(wrong(index:)==1)=weight(wrong(index:)==1)*exp(0.5)*(1-err)/err;
????weight(wrong(index:)==0)=weight(wrong(index:)==0)*exp(-0.5)*(1-err)/err;
????%標(biāo)準(zhǔn)化
????weight=weight/sum(weight);
????%計(jì)算當(dāng)前分類器下的錯(cuò)誤頻率如果訓(xùn)練錯(cuò)誤概率已經(jīng)為0,則退出循環(huán)
????err_freq(k)=calerr(numofsylabelatchoosek);
????if(err_freq(k)==0)
????????break;
????end
end
T=k;
at=at(1:T);
choose=choose(1:T);
plot(1:Terr_freq(1:T)‘b*‘);
title(‘error?vs?round‘);
xlabel(‘round‘);
ylabel(‘training?error‘);
end
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????1534??2013-07-27?11:55??ada2\adaboost.asv
?????文件???????1511??2013-07-27?13:54??ada2\adaboost.m
?????文件????????471??2013-07-27?11:59??ada2\calerr.m
?????文件????????109??2013-07-27?13:51??ada2\classier.asv
?????文件????????187??2013-07-27?13:53??ada2\classier.m
?????文件????????207??2013-07-27?11:10??ada2\h.asv
?????文件????????479??2013-07-27?11:11??ada2\h.m
?????文件?????????99??2013-07-27?11:56??ada2\h1.m
?????文件?????????99??2013-07-27?11:56??ada2\h2.m
?????文件?????????98??2013-07-27?11:56??ada2\h3.m
?????文件?????????98??2013-07-27?11:56??ada2\h4.m
?????文件?????????99??2013-07-27?11:56??ada2\h5.m
?????文件?????????99??2013-07-27?11:56??ada2\h6.m
?????文件?????????98??2013-07-27?11:57??ada2\h7.m
?????文件?????????98??2013-07-27?11:57??ada2\h8.m
?????文件????????110??2013-07-27?13:55??ada2\test.m
?????目錄??????????0??2013-07-27?13:52??ada2
-----------?---------??----------?-----??----
?????????????????5396????????????????????17
- 上一篇:WAVELET零水印算法
- 下一篇:Matlab_2011b
評(píng)論
共有 條評(píng)論