資源簡介
adaboost源碼,用c++寫的。拋開了積分圖計算等復(fù)雜的部分,專注于adaboost算法的具體實現(xiàn)過程,希望對初學(xué)者有幫助,也希望高手不吝指教。
代碼片段和文件信息
/*初學(xué)adaboost,希望能和大家交流?QQ:358807915
email:smile_spring@yeah.net
*/
#include?
#include?
#include?
#include
#include?
using?std::vector?;
using?namespace?std;
#define?FCOUNT?100//特征數(shù)
#define?CCOUNT?30//弱分類器個數(shù)
#define?PCOUNT?200//正樣本數(shù)
#define?NCOUNT?300//負樣本數(shù)
struct?sample
{
int?features[FCOUNT];//特征
int?pos_neg;//正0,負1
float?weight;//權(quán)值
int?result;//分類器的識別結(jié)果
};
struct?weakClassifier
{
int?indexF;
float?threshold;
};
struct?MySortFunction
{
????int?m_n;
????MySortFunction(int?n):m_n(n)
????{
????}
????bool?operator()(sample&s1sample&s2)const
????{
????????return?s1.features[m_n] ????}
};
//創(chuàng)建正樣本
void?CreatePos(vector&a)
{
int?ij;
???for(i=0;i {
sample?temp;
temp.pos_neg=0;
temp.weight=(float)1/(2*PCOUNT);
temp.result?=0;
for(j=0;j temp.features[j]=rand()%10;
a.push_back(temp);
}
}
float?min(float?afloat?b)
{
return(a<=b?a:b);
}
//創(chuàng)建負樣本
void?CreateNeg(vector&a)
{
int?ij;
for(i=0;i {
sample?temp;
temp.pos_neg=1;
temp.weight=(float)1/(2*NCOUNT);
temp.result?=1;
for(j=0;j temp.features[j]=rand()%10;
a.push_back(temp);
}
}
//Training?classifier
void?Training(vector&avector&bfloat*factors)
{
int?ij;
vector::size_type?id=0tcount=a.size();
for(i=0;i {
weakClassifier?temp;
????float?totalWeights=0.0totalPos=0.0totalNeg=0.0bPos=0bNeg=0;//(當前樣本之前的)正負樣本權(quán)值和
float?ethrbesterr=1.0;//訓(xùn)練單個分類器時用到的錯誤率,閾值,最小錯誤率
float?FThr[FCOUNT];//特征閾值
float?minErr=1.0;//所有特征的最小錯誤率
????float?beta;//更新權(quán)值所需系數(shù)
/*權(quán)重歸一化*/
for(id=0;id {
評論
共有 條評論