資源簡介
adaboost算法是一個由多個弱分類器生成一個強分類器的算法,可以提高分類的正確率,這里利用adaboost算法的原理,結合matlab做了一個簡單的實例
里面h1-h8為八個弱分類器,adaboost為訓練的主函數,test調用了訓練函數,對一個樣本進行測試,calerr計算每次循環后的錯誤頻率

代碼片段和文件信息
function?[at?choose?T]=adaboost()
%定義樣本
p=[10;-10;01;0-1];
%樣本數目
numofs=4;
%定義正確分類結果
y=[11-1-1];
%分類器數目
numofc=8;
%預設的循環次數
cycle=5;
%實際循環次數定義為T
%定義循環過程中選擇的函數下標和比重
at=zeros(1cycle);
choose=zeros(1cycle);
%每個樣本在每個分類器下的label
label=zeros(numofsnumofc);
%計算得到label數組的值
for?i=1:numofs
????for?j=1:numofc
????????label(ij)=h(jp(i:));
????end
end
%得到每個函數錯誤樣本的數組
wrong=zeros(numofcnumofs);
for?i=1:numofc
????for?j=1:numofs
????????wrong(ij)=(label(ji)~=y(j));
????end
end
%初始化每個樣本的權重
weight=ones(1numofs)/numofs;
%循環開始
%記錄每次循環中每個分類器對應的錯誤
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);
????%計算當前選擇的錯誤最小的分類器對應的權值
????choose(k)=index;
????at(k)=log((1-err)/err)/2;
????%更新權值
????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;
????%標準化
????weight=weight/sum(weight);
????%計算當前分類器下的錯誤頻率如果訓練錯誤概率已經為0,則退出循環
????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
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????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
評論
共有 條評論