-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRF.c
175 lines (142 loc) · 3.72 KB
/
RF.c
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
#include "ELM.h"
#ifdef RF
#include "RF.h"
#include <stdio.h>
#include <stdbool.h>
#ifdef REGRESSION
float (*pRegress)(float X[]) = randomForest_regression;
#else
int (*pClassf)(float X[]) = randomForest_classification;
#endif
#ifndef REGRESSION
int randomForest_classification(float X[])
{
int currentNode = 0;
float probab[FOREST_DIM][N_CLASS];
bool next_tree = 0;
int maxClass = -1;
float maxValue = 0;
float result[N_CLASS];
//compute the classification for each tree
for (int i = 0; i < FOREST_DIM; i++)
{
next_tree = 0;
currentNode = 0;
#ifdef DEBUG
printf("\n\nalbero %d della forest", i);
#endif
while (next_tree == 0)
{
//travel the tree
if (*(forest_feature[i] + currentNode) >= 0)
{
if (X[*(forest_feature[i] + currentNode)] <= *(forest_threshold[i] + currentNode))
{
#ifdef DEBUG
printf("\ncurrent node: %d, X:%f <= %f\n", currentNode, X[*(forest_feature[i] + currentNode)], *(forest_threshold[i] + currentNode));
fflush(stdout);
#endif
currentNode = *(forest_children_left[i] + currentNode);
}
else
{
#ifdef DEBUG
printf("\ncurrent node: %d, X:%f <= %f\n", currentNode, X[*(forest_feature[i] + currentNode)], *(forest_threshold[i] + currentNode));
fflush(stdout);
#endif
currentNode = *(forest_children_right[i] + currentNode);
}
}
else
{ // Leaf node
// inside each leaf note are stored the number of sample divided between the classes; the probability of each prediction is value/total samples
int j, k;
int total_samples = 0;
for (k = 0; k < forest_num_leaves[i]; k++)
{
if (*(forest_leaves[i] + k * (N_CLASS + 2)) == currentNode)
{
for (j = 0; j < N_CLASS; j++)
{
total_samples += *(forest_leaves[i] + k * (N_CLASS + 2) + j + 2);
}
for (j = 0; j < N_CLASS; j++)
{
probab[i][j] = (float)*(forest_leaves[i] + k * (N_CLASS + 2) + j + 2) / (float)total_samples;
#ifdef DEBUG
printf("\nProbab class: %d = %f", j, probab[i][j]);
fflush(stdout);
#endif
}
}
}
next_tree = 1;
}
}
}
/// once the prob array is built, it's required to compute the soft majority voting
// for each classes the probability between all the trees are added togheter and the highest probability is the "winning" class
for (int i = 0; i < FOREST_DIM; i++)
{
for (int j = 0; j < N_CLASS; j++)
{
if (i == 0)
result[j] = 0; //init the array
result[j] += probab[i][j];
#ifdef DEBUG
printf("\n\n\nResult probab class: %d = %f", j, result[j]);
fflush(stdout);
#endif
}
}
for (int j = 0; j < N_CLASS; j++)
{
if (result[j] >= maxValue)
{
maxValue = result[j];
maxClass = target_classes[j];
}
}
return maxClass;
}
#else
float randomForest_regression(float X[])
{
int currentNode = 0;
bool next_tree = 0;
float result = 0;
for (int i = 0; i < FOREST_DIM; i++)
{
next_tree = 0;
while (next_tree == 0)
{
//travel the tree
if (*(forest_feature[i] + currentNode) >= 0)
{
if (X[*(forest_feature[i] + currentNode)] <= *(forest_threshold[i] + currentNode))
{
#ifdef DEBUG
printf("\ncurrent node: %d, X:%f <= %f\n", currentNode, X[*(forest_feature[i] + currentNode)], *(forest_threshold[i] + currentNode));
fflush(stdout);
#endif
currentNode = *(forest_children_left[i] + currentNode);
}
else
{
#ifdef DEBUG
printf("\ncurrent node: %d, X:%f <= %f\n", currentNode, X[*(forest_feature[i] + currentNode)], *(forest_threshold[i] + currentNode));
fflush(stdout);
#endif
currentNode = *(forest_children_right[i] + currentNode);
}
}
else
{ // Leaf node
result += (*(forest_values[i] + currentNode);
}
next_tree = 1;
}
return result / (float)FOREST_DIM;
}
#endif
#endif