-
Notifications
You must be signed in to change notification settings - Fork 62
/
Adaboost.cpp
168 lines (167 loc) · 4.48 KB
/
Adaboost.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "Adaboost.h"
#define MAX 1000000
#define MIN -100000
namespace MLL
{
double* Adaboost::rangeSize(const int &axis)
{
double rangemin = MAX;
double rangemax = MIN;
double *range = (double *)malloc(sizeof(double) * 2);
for(size_t i = 0; i < data.size(); i++)
{
if(data[i][axis] > rangemax)
rangemax = data[i][axis];
if(data[i][axis] < rangemin)
rangemin = data[i][axis];
}
range[0] = rangemin;
range[1] = rangemax;
return range;
}
std::vector<int> Adaboost::Classify(const int &axis, const double &threshVal, const std::string &threshIneq)
{
std::vector<int> label;
for(size_t i = 0; i < data.size(); i++)
{
label.push_back(1);
if(threshIneq == "lt")
{
if(data[i][axis] <= threshVal)
label[i] = -1;
}
else
{
if(data[i][axis] > threshVal)
label[i] = -1;
}
}
return label;
}
Adaboost::Stump Adaboost::buildStump(std::vector<double> &weight)
{
Stump stump;
int i = 0, j = 0, k = 0, l = 0;
double *errArr = (double *)malloc(sizeof(double)*data.size());
double minErr = MAX;
double weightError = 0;
double *range = (double *)malloc(sizeof(double)*2);
double rangemin = 0, rangemax = 0;
double stepSize = 0;
int numSteps = 2;//划分步长
//int numSteps = data.size()/10;
double threshVal = 0 ;
int label_index = data[0].size()-1;
std::vector<int> label;
std::string threshIneq[2]= {"lt","gt"};
for(i = 0; i < data[0].size()-1; i++) //属性
{
range = rangeSize(i);
rangemin = range[0];
rangemax = range[1];
stepSize = (rangemax-rangemin) / numSteps;
for(j = 0; j <= numSteps; j++) //其实是numstep+1步
{
threshVal = rangemin+stepSize*j;
for(k = 0; k < 2; k++) //大于小于
{
label = Classify(i, threshVal, threshIneq[k]);
weightError = 0;
for(l = 0; l < data.size(); l++)
{
errArr[l] = label[l] - data[l][label_index] > 0 ? (label[l] - data[l][label_index]) / 2 : - (label[l]-data[l][label_index]) / 2;
weightError += errArr[l] * weight[l];
}
if(weightError < minErr)
{
minErr = weightError;
for(l = 0; l < data.size(); l++)
{
if(label[l] > 0)
stump.twosubdata.left.push_back(data[l]);
else
stump.twosubdata.right.push_back(data[l]);
}
stump.label = label;
stump.bestIndex = i;
stump.minErr = minErr;
stump.threshVal = threshVal;
stump.ltOrgt = threshIneq[k];
stump.alpha = 0.5 * log((1 - minErr) / minErr);
}
}
}
}
return stump;
}
int Adaboost::AdaboostTrain()
{
int i = 0, j = 0, k = 0;
int label_index = data[0].size()-1;
std::vector<double> weight;
double weightSum = 0;
std::vector<double> aggErr;
double sumErr = 0;
for(i = 0; i < data.size(); i++)
{
weight.push_back(1.0 / data.size());
aggErr.push_back(0);
}
for(i = 0; i < numIt; i++)
{
stump[i] = buildStump(weight);//写成赋值语句结构体中的结构体复制出现问题
std::cout << "alpha====" << stump[i].alpha << std::endl;
std::cout << "minErr=" << stump[i].minErr << std::endl;
weightSum = 0;
for(j = 0; j < data.size(); j++)
{
weight[j] = weight[j] * (exp(-1 * stump[i].alpha * (data[i][label_index] * stump[i].label[j])));
weightSum += weight[j];
}
for(j = 0; j < data.size(); j++)
{
weight[j] /= weightSum;
}
for(j = 0; j < data.size(); j++)
{
for(k = 0; k < i; k++)
{
aggErr[j] += stump[k].alpha * (stump[k].label[j]);
}
if(aggErr[j] > 0)
aggErr[j] = 1;
else
aggErr[j] = -1;
}
sumErr = 0;
for(j = 0; j<data.size(); j++)
{
sumErr += (aggErr[j] - data[j][label_index]) >0 ? (aggErr[j]-data[j][label_index])/2 : -(aggErr[j]-data[j][label_index])/2;
}
//if(sumErr/data.size()<0.01)
{
std::cout<< "sumErr:" << sumErr<<std::endl;
//break;
}
}
for(i = 0; i < numIt; i++)
{
std::cout << "weight:" << stump[i].alpha << "\t" << "bestindex:" << stump[i].bestIndex << "\t" << "threshval:" << stump[i].threshVal << "\t" << "ltOrGt:" << stump[i].ltOrgt << "\t" << "error:" << stump[i].minErr <<std::endl;
}
return 0;
}
Adaboost::Adaboost(const std::string &file,const int &numIt)
{
this->numIt = numIt;
stump.resize(numIt);
LoadData(data,file);
int label_index=data[0].size()-1;
for(int l=0; l<data.size(); l++)
{
if(data[l][label_index] == 0)
{
data[l][label_index] = -1;
}
}
}
}