forked from handspeaker/RandomForests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandomForest.cpp
291 lines (281 loc) · 7.19 KB
/
RandomForest.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include"RandomForest.h"
RandomForest::RandomForest(int treeNum,int maxDepth,int minLeafSample,float minInfoGain)
{
_treeNum=treeNum;
_maxDepth=maxDepth;
_minLeafSample=minLeafSample;
_minInfoGain=minInfoGain;
_trainSample=NULL;
printf("total tree number:%d\n",_treeNum);
printf("max depth of a single tree:%d\n",_maxDepth);
printf("the minimum samples in a leaf:%d\n",_minLeafSample);
printf("the minimum information gain:%f\n",_minInfoGain);
_forest=new Tree*[_treeNum];
for(int i=0;i<_treeNum;++i)
{_forest[i]=NULL;}
}
RandomForest::RandomForest(const char*modelPath)
{
readModel(modelPath);
}
RandomForest::~RandomForest()
{
//printf("destroy RandomForest...\n");
if(_forest!=NULL)
{
for(int i=0;i<_treeNum;++i)
{
if(_forest[i]!=NULL)
{
delete _forest[i];
_forest[i]=NULL;
}
}
delete[] _forest;
_forest=NULL;
}
if(_trainSample!=NULL)
{
delete _trainSample;
_trainSample=NULL;
}
}
void RandomForest::train(float**trainset,float*labels,int SampleNum,int featureNum,
int classNum,bool isRegression)
{
int trainFeatureNumPerNode=static_cast<int>(sqrt(static_cast<float>(featureNum)));
train(trainset,labels,SampleNum,featureNum,classNum,isRegression,trainFeatureNumPerNode);
}
void RandomForest::train(float**trainset,float*labels,int SampleNum,int featureNum,
int classNum,bool isRegression,int trainFeatureNumPerNode)
{
if(_treeNum<1)
{
printf("total tree number must bigger than 0!\n");
printf("training failed\n");
return;
}
if(_maxDepth<1)
{
printf("the max depth must bigger than 0!\n");
printf("training failed\n");
return;
}
if(_minLeafSample<2)
{
printf("the minimum samples in a leaf must bigger than 1!\n");
printf("training failed\n");
return;
}
_trainSampleNum=SampleNum;
_featureNum=featureNum;
_classNum=classNum;
_trainFeatureNumPerNode=trainFeatureNumPerNode;
_isRegression=isRegression;
//initialize every tree
if(_isRegression)
{
_classNum=1;
for(int i=0;i<_treeNum;++i)
{
_forest[i]=new RegrTree(_maxDepth,_trainFeatureNumPerNode,
_minLeafSample,_minInfoGain,_isRegression);
}
}
else
{
for(int i=0;i<_treeNum;++i)
{
_forest[i]=new ClasTree(_maxDepth,_trainFeatureNumPerNode,
_minLeafSample,_minInfoGain,_isRegression);
}
}
//this object hold the whole trainset&labels
_trainSample=new Sample(trainset,labels,_classNum,_trainSampleNum,_featureNum);
srand(static_cast<unsigned int>(time(NULL)));
int*_sampleIndex=new int[_trainSampleNum];
//start to train every tree in the forest
for(int i=0;i<_treeNum;++i)
{
printf("train the %d th tree...\n",i);
//random sampling from trainset
Sample*sample=new Sample(_trainSample);
sample->randomSelectSample(_sampleIndex,_trainSampleNum,_trainSampleNum);
_forest[i]->train(sample);
delete sample;
}
delete[] _sampleIndex;
_sampleIndex=NULL;
}
void RandomForest::predict(float*data,float&response)
{
//get the predict from every tree
//if regression,_classNum=1
float*result=new float[_classNum];
int i=0;
for(i=0;i<_classNum;++i)
{result[i]=0;}
for(i=0;i<_treeNum;++i)//_treeNum
{
Result r;
r.label=0;
r.prob=0;//Result
r=_forest[i]->predict(data);
result[static_cast<int>(r.label)]+=r.prob;
}
if(_isRegression)
{response=result[0]/_treeNum;}
else
{
float maxProbLabel=0;
float maxProb=result[0];
for(i=1;i<_classNum;++i)
{
if(result[i]>maxProb)
{
maxProbLabel=i;
maxProb=result[i];
}
}
response=maxProbLabel;
}
delete[] result;
}
void RandomForest::predict(float**testset,int SampleNum,float*responses)
{
//get the predict from every tree
for(int i=0;i<SampleNum;++i)
{
predict(testset[i],responses[i]);
}
}
void RandomForest::saveModel(const char*path)
{
FILE* saveFile=fopen(path,"wb");
fwrite(&_treeNum,sizeof(int),1,saveFile);
fwrite(&_maxDepth,sizeof(int),1,saveFile);
fwrite(&_classNum,sizeof(int),1,saveFile);
fwrite(&_isRegression,sizeof(bool),1,saveFile);
int nodeNum=static_cast<int>(pow(2.0,_maxDepth)-1);
int isLeaf=0;
for(int i=0;i<_treeNum;++i)
{
Node**arr=_forest[i]->getTreeArray();
isLeaf=0;
for(int j=0;j<nodeNum;++j)
{
if(arr[j]!=NULL)
{
if(arr[j]->isLeaf())
{
isLeaf=1;
fwrite(&isLeaf,sizeof(int),1,saveFile);
if(_isRegression)
{
float value=((RegrNode*)arr[j])->getValue();
fwrite(&value,sizeof(float),1,saveFile);
}
else
{
float clas=((ClasNode*)arr[j])->getClass();
float prob=((ClasNode*)arr[j])->getProb();
fwrite(&clas,sizeof(float),1,saveFile);
fwrite(&prob,sizeof(float),1,saveFile);
}
}
else
{
isLeaf=0;
fwrite(&isLeaf,sizeof(int),1,saveFile);
int featureIndex=arr[j]->getFeatureIndex();
float threshold=arr[j]->getThreshold();
fwrite(&featureIndex,sizeof(int),1,saveFile);
fwrite(&threshold,sizeof(float),1,saveFile);
}
}
}
////write an numb node to denote the tree end
//isLeaf=-1;
//fwrite(&isLeaf,sizeof(int),1,saveFile);
}
fclose(saveFile);
}
void RandomForest::readModel(const char*path)
{
_minLeafSample=0;
_minInfoGain=0;
_trainFeatureNumPerNode=0;
FILE* modelFile=fopen(path,"rb");
fread(&_treeNum,sizeof(int),1,modelFile);
fread(&_maxDepth,sizeof(int),1,modelFile);
fread(&_classNum,sizeof(int),1,modelFile);
fread(&_isRegression,sizeof(bool),1,modelFile);
int nodeNum=static_cast<int>(pow(2.0,_maxDepth)-1);
_trainSample=NULL;
printf("total tree number:%d\n",_treeNum);
printf("max depth of a single tree:%d\n",_maxDepth);
printf("_classNum:%d\n",_classNum);
printf("_isRegression:%d\n",_isRegression);
_forest=new Tree*[_treeNum];
//initialize every tree
if(_isRegression)
{
for(int i=0;i<_treeNum;++i)
{
_forest[i]=new RegrTree(_maxDepth,_trainFeatureNumPerNode,
_minLeafSample,_minInfoGain,_isRegression);
}
}
else
{
for(int i=0;i<_treeNum;++i)
{
_forest[i]=new ClasTree(_maxDepth,_trainFeatureNumPerNode,
_minLeafSample,_minInfoGain,_isRegression);
}
}
int*nodeTable=new int[nodeNum];
int isLeaf=-1;
int featureIndex=0;
float threshold=0;
float value=0;
float clas=0;
float prob=0;
for(int i=0;i<_treeNum;++i)
{
memset(nodeTable,0,sizeof(int)*nodeNum);
nodeTable[0]=1;
for(int j=0;j<nodeNum;j++)
{
//if current node is marked as null,continue
if(nodeTable[j]==0)
{continue;}
fread(&isLeaf,sizeof(int),1,modelFile);
if(isLeaf==0) //split node
{
nodeTable[j*2+1]=1;
nodeTable[j*2+2]=1;
fread(&featureIndex,sizeof(int),1,modelFile);
fread(&threshold,sizeof(float),1,modelFile);
_forest[i]->createNode(j,featureIndex,threshold);
}
else if(isLeaf==1) //leaf
{
if(_isRegression)
{
fread(&value,sizeof(float),1,modelFile);
((RegrTree*)_forest[i])->createLeaf(j,value);
}
else
{
fread(&clas,sizeof(float),1,modelFile);
fread(&prob,sizeof(float),1,modelFile);
((ClasTree*)_forest[i])->createLeaf(j,clas,prob);
}
}
}
//fread(&isLeaf,sizeof(int),1,modelFile);
}
fclose(modelFile);
delete[] nodeTable;
}