• 大小: 4KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-13
  • 语言: 其他
  • 标签: AdaBoost  

资源简介

基于二叉分类决策树的AdaBoost算法实现,特征必须为2值的,标签可为多个

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include “Binary_Decision_Tree.cpp“

bool first=true;
using namespace std;
const int totalTrees=10;

double line(double error){
return 0.5-error;
}
void copyArray(int *sourceint *destint length){
for(int i=0;i dest[i]=source[i];
}

void normalize(double *arrayint length){
double sum=0;
for(int i=0;i sum+=array[i];
for(int i=0;i array[i]=array[i]/sum;
}

class AdaBoost{
public:
int rowscolsnumTrees;
int total;
DecisionTree **treeSet;
double *weightSamples;
double *weightTrees;
public:
int sign(double x);
AdaBoost(int rint cint numTrees);
void train(int **featureint *category);
int predict(int *sample);
void fillSamples(int **featureint *categoryint **samplesint *types);
};
int AdaBoost::sign(double x){
if(x>0)
return 1;
else if(x==0)
return 0;
else
return -1;
}
AdaBoost::AdaBoost(int rint cint num){
rows=r;
cols=c;
numTrees=num;
total=10*rows;

weightSamples=new double[rows];
setValue(weightSamples(double)1/rowsrows);

weightTrees=new double[numTrees]; 
setValue(weightTrees(double)1/numTreesnumTrees);

treeSet=new DecisionTree*[numTrees];
for(int i=0;i treeSet[i]=new DecisionTree(10*rowscols);
}
void AdaBoost::train(int **featureint *category){
int **samples=new int*[total];
for(int i=0;i samples[i]=new int[cols];

int *types=new int[total];
int judge[rows];

for(int i=0;i fillSamples(featurecategorysamplestypes);
treeSet[i]->train(samplestypes);

double error=0;
setValue(judge-1rows);//correct jugment

for(int j=0;j int cat=treeSet[i]->predict(feature[j]);
if(cat!=category[j]){
judge[j]=1;//wrong jugment
error+=weightSamples[j];
}
}

weightTrees[i]=line(error);

if(weightTrees[i]>5){
cout<<“weight:“< cout<<“error:“< }

if(i+1 for(int j=0;j double exponent=weightTrees[i]*judge[j];
weightSamples[j]=weightSamples[j]*exp(exponent);
}
normalize(weightSamplesrows);
}
}
normalize(weightTreesnumTrees);
}
void AdaBoost::fillSamples(int **featureint *categoryint **samplesint *types){
int tag=0pre=0;
for(int i=0;i tag+=weightSamples[i]*total;
for(int j=pre;j copyArray(feature[i]samples[j]cols);
types[j]=category[i];
}
pre=tag;
}

if(tag for(int j=tag;j int random=rand()%rows;
copyArray(feature[random]samples[j]cols);
types[j]=category[random];
}
}
int AdaBoost::predict(int *sample){
double sum=0;
for(int i=0;i sum+=weightTrees[i]*treeSet[i]->predict(sample);
return (int)(sum+0.5);
}
int main(){
srand((unsigned)time(NULL));
int rows=1000cols=8;
AdaBoost

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        4545  2015-09-24 22:05  AdaBoost_Demo.cpp
     文件        7853  2015-09-24 17:38  Binary_Decision_Tree.cpp

评论

共有 条评论