📄 adaboost算法中寻找最优阈值分类器的代码优化.htm
字号:
bestError=tempError;<BR>
bestThresh=tempThresh;<BR>
bestBias=tempBias;<BR>
end<BR>
%将搜索范围缩小,继续进行搜索<BR>
span=(maxFea-minFea)/8;
% 搜索范围减为原有的1/4
<BR> maxFea=tempThresh+span;
% 减少搜索范围后搜索空间的最大值
<BR>
minFea=tempThresh-span;
% 减少搜索范围后搜索空间的最小值<BR>
<BR>
step=(maxFea-minFea)/(sectNum-1);%
减少搜索范围后每次搜索的递增量<BR>end<BR><BR>在将循环向量化,并删除一些重复的赋值运算后。代码量大大减少,计算效率也得到极大改善。<BR><BR>%
在特征列上获得最优的阈值分类器<BR>% <BR>% 假设分布满足高斯分布<BR>% 通过高斯模型求取两个类别的均值<BR>%
在两个类别的均值中间搜索最有阈值分类器<BR>% 采用逐步求精的搜索策略<BR>%<BR>% 输入:<BR>% FeatureVector
特征向量,查找最佳弱分类器的特征列;列向量 <BR>% Y
每个样本所属类别的标识,长度为rows;列向量<BR>% rows
样本容量 <BR>% weight
权重向量,存放当前每个样本的权重值<BR>% <BR>%
输出:<BR>% bestError
搜索到第j列最佳弱分类器得到的最小错误率<BR>% bestThresh
搜索到第j列最佳弱分类器的阈值<BR>% bestBias
搜索到第j列最佳弱分类器的偏置<BR>% <BR>% 迭代4次,每次将区间划分为8个小段<BR>%<BR>%
调用格式为<BR>%
[bestError,bestThresh,bestBias]=searchBestWeakLearner(FeatureVector,Y,rows,weight)<BR>%
<BR>% 2007-11-07 <BR>% <BR>% findBestWeakLearner 扩展版本<BR>%
<BR>function
[bestError,bestThresh,bestBias]=searchBestWeakLearner(FeatureVector,Y,rows,weight)<BR>%
检查输入特征向量与类标需为列向量<BR>iptcheckinput(FeatureVector,{'logical','numeric'},{'column','nonempty','real'},mfilename,
'FeatureVector',
1);<BR>iptcheckinput(Y,{'logical','numeric'},{'column','nonempty','integer'},mfilename,
'Y', 2);<BR><BR>u1=mean(FeatureVector(find(Y==1)));
% 类别1均值<BR>u2=mean(FeatureVector(find(Y==0)));
% 类别2均值<BR><BR>iteration=4;
%
迭代次数<BR>sectNum=8;
%
每次迭代,将搜索区域划分的片段<BR><BR>maxFea=max(u1,u2);
% 搜索空间的最大值
<BR>minFea=min(u1,u2);
%
搜索空间的最小值<BR>step=(maxFea-minFea)/(sectNum-1);
% 每次搜索的递增量<BR>bestError=rows;
% 初值:最好的分类器错误率<BR><BR>for
iter=1:iteration
%
迭代iteration次,范围逐步缩小,寻找最优值<BR>
tempError=rows;
% 初值:第iter次迭代的分类器错误率
<BR> for
i=1:sectNum
% 第iter次迭代的搜索次数<BR>
thresh=minFea+(i-1)*step; %
第i次搜索的阈值<BR>
h=FeatureVector<thresh; %
所有样本的阈值分类结果<BR>
error=sum(weight(find(h~=Y)));% 第iter次迭代第i次搜索加权错误率<BR>
p=1;<BR>
if(error>0.5)
%
若错误率超过0.5,则将偏置反向<BR>
error=1-error;<BR>
p=-1;<BR>
end<BR>
if( error<bestError ) %
第iter次迭代最优的错误率 阈值 偏置<BR>
bestError=error;
% 第iter次迭代最小的错误率<BR>
bestThresh=thresh; %
第iter次迭代最小错误分类情况下的阈值<BR>
bestBias=p;
% 第iter次迭代最小错误分类情况下的偏置<BR>
end<BR>
end%end for i<BR><BR>
% 将搜索范围缩小,继续进行搜索<BR>
span=(maxFea-minFea)/8;
% 搜索范围减为原有的1/4
<BR>
maxFea=bestThresh+span;
% 减少搜索范围后搜索空间的最大值 <BR>
minFea=bestThresh-span;
% 减少搜索范围后搜索空间的最小值<BR>
<BR> step=(maxFea-minFea)/(sectNum-1); %
减少搜索范围后每次搜索的递增量<BR>end<BR><BR><BR><BR>针对2000个样本,训练50轮AdaBoost分类器,采用findBestWeakLearner搜索最优分类器耗时150s,采用searchBestWeakLearner耗时50s,仅为原有1/3。<BR><BR>toself:
程序改变心境</DIV></DIV>
<DIV class=signatures style="maxHeightIE: 2880px">欢迎朋友们加入我们这个大家庭! </DIV>
<DIV></DIV></TD></TR>
<TR>
<TD class=postauthor>
<DIV class="popupmenu_popup userinfopanel" id=userinfo47019_menu
style="DISPLAY: none">
<DL>
<DT>UID</DT>
<DD>3851 </DD>
<DT>帖子</DT>
<DD>1656 </DD>
<DT>精华</DT>
<DD><A
href="http://www.webmastersky.cn/digest.php?authorid=3851">0</A> </DD>
<DT>积分</DT>
<DD>0 </DD>
<DT>阅读权限</DT>
<DD>200 </DD>
<DT>在线时间</DT>
<DD>1008 小时 </DD>
<DT>注册时间</DT>
<DD>2007-8-28 </DD>
<DT>最后登录</DT>
<DD>2008-7-9 </DD></DL>
<P><A
href="http://www.webmastersky.cn/space.php?action=viewpro&uid=3851"
target=_blank>查看详细资料</A></P></DIV></TD>
<TD class=postcontent>
<DIV class=postactions>
<P><STRONG title=顶部 onclick=scroll(0,0)>TOP</STRONG> </P>
<DIV id=ad_thread1_0></DIV></DIV></TD></TR></TBODY></TABLE></DIV>
<DIV id=ad_interthread></DIV>
<DIV class="mainbox viewthread">
<TABLE id=pid48281 cellSpacing=0 cellPadding=0 summary=pid48281>
<TBODY>
<TR>
<TD class=postauthor><CITE><A class=dropmenu id=userinfo48281
onmouseover=showMenu(this.id)
href="http://www.webmastersky.cn/space.php?uid=4015"
target=_blank>hijk227</A></CITE>
<DIV class=avatar><IMG class=avatar alt=""
src="AdaBoost算法中寻找最优阈值分类器的代码优化.files/noavatar.gif"></DIV>
<P><EM>新手上路</EM></P>
<P><IMG alt="Rank: 1"
src="AdaBoost算法中寻找最优阈值分类器的代码优化.files/star_level1.gif"></P>
<UL>
<LI class=pm><A id=ajax_uid_48281
onclick="ajaxmenu(event, this.id, 9000000, null, 0)"
href="http://www.webmastersky.cn/pm.php?action=send&uid=4015"
target=_blank>发短消息</A> </LI>
<LI class=buddy><A id=ajax_buddy_1
onclick="ajaxmenu(event, this.id, null, 0)"
href="http://www.webmastersky.cn/my.php?item=buddylist&newbuddyid=4015&buddysubmit=yes"
target=_blank>加为好友</A> </LI>
<LI class=offline>当前离线 </LI></UL></TD>
<TD class=postcontent>
<DIV class=postinfo><STRONG id=postnum_48281 title=复制帖子链接到剪贴板
onclick="setcopy('http://www.webmastersky.cn/viewthread.php?tid=35231&page=1#pid48281', '帖子链接已经复制到剪贴板')">2<SUP>#</SUP></STRONG>
<EM onclick="$('postmessage_48281').className='t_bigfont'">大</EM> <EM
onclick="$('postmessage_48281').className='t_msgfont'">中</EM> <EM
onclick="$('postmessage_48281').className='t_smallfont'">小</EM> 发表于
2008-7-9 03:53 <A
href="http://www.webmastersky.cn/viewthread.php?tid=35231&page=1&authorid=4015">只看该作者</A>
</DIV>
<DIV id=ad_thread2_1></DIV>
<DIV class="postmessage defaultpost">
<DIV id=ad_thread3_1></DIV>
<DIV id=ad_thread4_1></DIV>
<H2>鼻</H2>
<DIV class=t_msgfont
id=postmessage_48281>签名<BR>——----------------------------------------------------------------------------<BR><A
href="http://www.bjmr120.com/zxmr/bibuzx/20080320545.htm"
target=_blank><FONT size=1><FONT color=black>隆鼻 </FONT></FONT></A><A
href="http://www.bjmr120.com/zxmr/bibuzx/20080408356.htm"
target=_blank><FONT size=1><FONT color=black>隆鼻术 </FONT></FONT></A><A
href="http://www.bjmr120.com/zxmr/bibuzx/20080408539.htm"
target=_blank><FONT size=1><FONT color=black>北京隆鼻 </FONT></FONT></A><A
href="http://www.bjmr120.com/zxmr/bibuzx/2008040858.htm"
target=_blank><FONT size=1><FONT color=black>面部整形 </FONT></FONT></A><A
href="http://www.bjmr120.com/zxmr/bibuzx/20080408837.htm"
target=_blank><FONT size=1><FONT
color=black>隆鼻手术</FONT></FONT></A></DIV></DIV>
<DIV class=signatures style="maxHeightIE: 2880px"><BR><A
href="http://www.bjmr120.com/" target=_blank>北京整形</A><BR><BR></DIV>
<DIV></DIV></TD></TR>
<TR>
<TD class=postauthor>
<DIV class="popupmenu_popup userinfopanel" id=userinfo48281_menu
style="DISPLAY: none">
<DIV class=imicons><A
href="http://wpa.qq.com/msgrd?V=1&Uin=12345678&Site=站长天空&Menu=yes"
target=_blank><IMG alt=QQ
src="AdaBoost算法中寻找最优阈值分类器的代码优化.files/qq.gif"></A> </DIV>
<DL>
<DT>UID</DT>
<DD>4015 </DD>
<DT>帖子</DT>
<DD>157 </DD>
<DT>精华</DT>
<DD><A
href="http://www.webmastersky.cn/digest.php?authorid=4015">0</A> </DD>
<DT>积分</DT>
<DD>0 </DD>
<DT>阅读权限</DT>
<DD>10 </DD>
<DT>在线时间</DT>
<DD>0 小时 </DD>
<DT>注册时间</DT>
<DD>2008-5-17 </DD>
<DT>最后登录</DT>
<DD>2008-7-16 </DD></DL>
<P><A href="http://www.bjmr120.com/" target=_blank>查看个人网站</A></P>
<P><A
href="http://www.webmastersky.cn/space.php?action=viewpro&uid=4015"
target=_blank>查看详细资料</A></P></DIV></TD>
<TD class=postcontent>
<DIV class=postactions>
<P><STRONG title=顶部 onclick=scroll(0,0)>TOP</STRONG> </P>
<DIV id=ad_thread1_1></DIV></DIV></TD></TR></TBODY></TABLE></DIV>
<DIV class="mainbox viewthread">
<TABLE id=pid48289 cellSpacing=0 cellPadding=0 summary=pid48289>
<TBODY>
<TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -