-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDT.c
97 lines (90 loc) · 1.8 KB
/
DT.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
#include "ELM.h"
#ifdef DT
#include "dt.h"
#include <stdio.h>
#include <stdbool.h>
#ifdef REGRESSION
float (*pRegress)(float X[]) = decisionTree_regression;
#else
int (*pClassf)(float X[]) = decisionTree_classification;
#endif
#ifndef REGRESSION
int decisionTree_classification(float X[])
{
int currentNode = 0;
while (1)
{
if (feature[currentNode] >= 0)
{
if (X[feature[currentNode]] <= threshold[currentNode])
{
#ifdef DEBUG
printf("\ncurrent node: %d, X:%f <= %f\n", currentNode, X[feature[currentNode]], threshold[currentNode]);
fflush(stdout);
#endif
currentNode = children_left[currentNode];
}
else
{
#ifdef DEBUG
printf("\ncurrent node: %d, X:%f > %f\n", currentNode, X[feature[currentNode]], threshold[currentNode]);
fflush(stdout);
#endif
currentNode = children_right[currentNode];
}
}
else
{ // Leaf node
/*{
int j;
int maxClass;
int maxValue = 0;
for (j = 0; j < N_CLASS; j++)
{
if (values[currentNode][j] >= maxValue)
{
maxValue = values[currentNode][j];
maxClass = target_classes[j];
}
}
return maxClass;
}
break;*/
int j;
for (j = 0; j < N_LEAVES; j++) {
if (leaf_nodes[j][0] == currentNode) {
int maxIdx = leaf_nodes[j][1];
int maxClass = target_classes[maxIdx];
printf("\ncurrent node: %d, decision: %d\n", currentNode, maxClass);
fflush(stdout);
return maxClass;
}
}
}
}
}
#else
float decisionTree_regression(float X[])
{
int currentNode = 0;
while (1)
{
if (feature[currentNode] >= 0)
{
if (X[feature[currentNode]] <= threshold[currentNode])
{
currentNode = children_left[currentNode];
}
else
{
currentNode = children_right[currentNode];
}
}
else
{ // Leaf node
return values[currentNode][0];
}
}
}
#endif
#endif