-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomForest.cpp
241 lines (215 loc) · 5.41 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
#include "RandomForest.h"
RandomForest::RandomForest(float** datas, int* label, int treeNum, int classNum, int sampleNum, int featureNum) {
_trainSample = new Sample(datas, label, sampleNum, featureNum);
_treeNum = treeNum;
_classNum = classNum;
_forest = new Tree*[_treeNum];
for (int i = 0; i < _treeNum; i++) {
_forest[i] = NULL;
}
}
RandomForest::RandomForest(const char* modelPath) {
ifstream fin;
fin.open(modelPath);
char* line = new char[200];
int isFirstLine = 1;
int maxNodeNum = 0;
int treeIndex = 0;
int nodeIndex = 0;
while (fin.getline(line, 200)) {
if (isFirstLine) {
isFirstLine = 0;
char str[20];
int index = 0;
int len = strlen(line);
int paramIndex = 0;
for (int i= 0; i <= len; i++) {
if (line[i] == '\t' || i == len) {
str[index] = '\0';
int num = atoi(str);
if (paramIndex == 0) {
_treeNum = num;
_forest = new Tree*[_treeNum];
for (int i = 0; i < _treeNum; i++) {
_forest[i] = NULL;
}
} else if (paramIndex == 1) {
_classNum = num;
} else {
_maxDepth = num;
maxNodeNum = pow(2, _maxDepth) - 1;
}
paramIndex++;
index = 0;
} else {
str[index] = line[i];
index++;
}
}
} else {
if (nodeIndex % maxNodeNum == 0) {
_forest[treeIndex] = new Tree(maxNodeNum);
treeIndex++;
}
char str[50];
int index = 0;
int len = strlen(line);
int paramIndex = 0;
int isLeaf = -1;
int featureId = -1;
float threshold = -1;
int label = -1;
int prob = -1;
int* probs = new int[_classNum];
for (int i = 0 ; i <= len; i++) {
if (line[i] == '\t' || i == len) {
str[index] = '\0';
if (paramIndex == 0) {
isLeaf = atoi(str);
if (isLeaf == -1) {
break;
}
} else if (paramIndex == 1) {
featureId = atoi(str);
} else if (paramIndex == 2) {
threshold = atof(str);
} else if (paramIndex == 3) {
label = atoi(str);
} else {
prob = atoi(str);
probs[paramIndex-4] = prob;
}
paramIndex++;
index = 0;
} else {
str[index] = line[i];
index++;
}
}
if (isLeaf != -1) {
(_forest[treeIndex-1])->_cTree[nodeIndex] = new Node(isLeaf, featureId, threshold, label, probs, _classNum);
delete [] probs;
}
nodeIndex = (nodeIndex+1) % maxNodeNum;
}
}
delete [] line;
fin.close();
}
RandomForest::~RandomForest() {
if (_forest != NULL) {
for (int i = 0; i < _treeNum; i++) {
if (_forest[i] != NULL) {
delete _forest[i];
}
}
delete [] _forest;
}
if (_trainSample != NULL) {
delete _trainSample;
}
}
void RandomForest::generateForest(int maxDepth, int minLeafSample, float minInfoGain, int trainFeatureNumPerNode, int trainSampleNumPerTree) {
_maxDepth = maxDepth;
if (trainFeatureNumPerNode == -1) {
_trainFeatureNumPerNode = sqrt(_trainSample->_featureNum);
} else {
_trainFeatureNumPerNode = trainFeatureNumPerNode;
}
if (trainSampleNumPerTree == -1) {
_trainSampleNumPerTree = _trainSample->_sampleNum * 0.667;
} else {
_trainSampleNumPerTree = trainSampleNumPerTree;
}
if (_treeNum < 1) {
cout << "Tree number must be bigger than 0!" << endl;
cout << "Generating faild!" << endl;
return;
}
if (_maxDepth < 2) {
cout << "Max depth must be bigger than 1!" << endl;
cout << "Generating faild!" << endl;
return;
}
if (minLeafSample < 2) {
cout << "Minimum samples in a leaf must be bigger than 1!" << endl;
cout << "Generating faild" << endl;
return;
}
for (int i = 0; i < _treeNum; i++) {
cout << "Generate Tree " << i << endl;
_forest[i] = new Tree(_classNum, maxDepth, minLeafSample, minInfoGain);
Sample* trainSample = new Sample(_trainSample);
trainSample->randomSelectSample(_trainSampleNumPerTree);
trainSample->randomSelectFeature(_trainFeatureNumPerNode);
_forest[i]->generateTree(trainSample);
trainSample->releaseIndex();
delete trainSample;
}
_trainSample->releaseIndex();
delete _trainSample;
_trainSample = NULL;
}
void RandomForest::predict(float** testData, float* results, int testSize) {
int* probs = new int[_classNum];
for (int i = 0; i < testSize; i++) {
for (int j = 0 ; j < _classNum; j++) {
probs[j] = 0;
}
for (int j = 0; j < _treeNum; j++) {
int label = _forest[j]->predict(testData[i]);
probs[label]++;
}
int maxNum = 0;
int label = -1;
for (int j = 0; j < _classNum; j++) {
if (probs[j] > maxNum) {
maxNum = probs[j];
label = j;
}
}
if (label == 1) {
results[i] = (float)probs[label]/_treeNum;
} else {
results[i] = 1 - (float)probs[label]/_treeNum;
}
}
delete [] probs;
}
void RandomForest::saveModel(const char* path) {
ofstream fout;
fout.open(path);
int maxNodeNum = pow(2, _maxDepth) - 1;
fout << _treeNum << '\t' << _classNum << '\t' << _maxDepth << '\n';
for (int i = 0 ; i < _treeNum; i++) {
Node** cTree = _forest[i]->_cTree;
for (int j = 0 ; j < maxNodeNum; j++) {
if (cTree[j] == NULL) {
fout << -1 << '\n';
continue;
}
int isLeaf = 0;
if (cTree[j]->_isLeaf) {
isLeaf = 1;
}
if (isLeaf) {
fout << 1 << '\t';
} else {
fout << 0 << '\t';
}
fout << cTree[j]->_featureId << '\t';
fout << cTree[j]->_threshold << '\t';
int label = cTree[j]->_label;
fout << label << '\t';
for (int k = 0; k < _classNum; k++) {
fout << cTree[j]->_probs[k];
if (k != _classNum - 1) {
fout << '\t';
} else {
fout << '\n';
}
}
}
}
fout.close();
}