forked from handspeaker/RandomForests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tree.cpp
176 lines (169 loc) · 5.26 KB
/
Tree.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
#include"Tree.h"
Tree::Tree(int MaxDepth,int trainFeatureNumPerNode,int minLeafSample,float minInfoGain,bool isRegression)
{
_MaxDepth=MaxDepth;
_trainFeatureNumPerNode=trainFeatureNumPerNode;
_minLeafSample=minLeafSample;
_minInfoGain=minInfoGain;
_nodeNum=static_cast<int>(pow(2.0,_MaxDepth)-1);
_cartreeArray=new Node*[_nodeNum];
_isRegression=isRegression;
for(int i=0;i<_nodeNum;++i)
{_cartreeArray[i]=NULL;}
}
Tree::~Tree()
{
if(_cartreeArray!=NULL)
{
for(int i=0;i<_nodeNum;++i)
{
if(_cartreeArray[i]!=NULL)
{
delete _cartreeArray[i];
_cartreeArray[i]=NULL;
}
}
delete[] _cartreeArray;
_cartreeArray=NULL;
}
}
Result Tree::predict(float*data)
{
int position=0;
Node*head=_cartreeArray[position];
while(!head->isLeaf())
{
position=head->predict(data,position);
head=_cartreeArray[position];
}
Result r;
head->getResult(r);
return r;
}
/************************************************/
//Classification Tree
ClasTree::ClasTree(int MaxDepth,int trainFeatureNumPerNode,int minLeafSample,float minInfoGain,bool isRegression)
:Tree(MaxDepth,trainFeatureNumPerNode,minLeafSample,minInfoGain,isRegression)
{}
ClasTree::~ClasTree()
{}
void ClasTree::train(Sample*sample)
{
//initialize root node
//random generate feature index
int*_featureIndex=new int[_trainFeatureNumPerNode];
Sample*nodeSample=new Sample(sample,0,sample->getSelectedSampleNum()-1);
_cartreeArray[0]=new ClasNode();
_cartreeArray[0]->_samples=nodeSample;
//calculate the probablity and gini
_cartreeArray[0]->calculateParams();
for(int i=0;i<_nodeNum;++i)
{
int parentId=(i-1)/2;
//if current node's parent node is NULL,continue
if(_cartreeArray[parentId]==NULL)
{continue;}
//if the current node's parent node is a leaf,continue
if(i>0&&_cartreeArray[parentId]->isLeaf())
{continue;}
//if it reach the max depth
//set current node as a leaf and continue
if(i*2+1>=_nodeNum) //if left child node is out of range
{
_cartreeArray[i]->createLeaf();
continue;
}
//if current samples in this node is less than the threshold
//set current node as a leaf and continue
if(_cartreeArray[i]->_samples->getSelectedSampleNum()<=_minLeafSample)
{
_cartreeArray[i]->createLeaf();
continue;
}
_cartreeArray[i]->_samples->randomSelectFeature
(_featureIndex,sample->getFeatureNum(),_trainFeatureNumPerNode);
//else calculate the information gain
_cartreeArray[i]->calculateInfoGain(_cartreeArray,i,_minInfoGain);
_cartreeArray[i]->_samples->releaseSampleIndex();
}
delete[] _featureIndex;
_featureIndex=NULL;
delete nodeSample;
}
void ClasTree::createNode(int id,int featureIndex,float threshold)
{
_cartreeArray[id]=new ClasNode();
_cartreeArray[id]->setLeaf(false);
_cartreeArray[id]->setFeatureIndex(featureIndex);
_cartreeArray[id]->setThreshold(threshold);
}
void ClasTree::createLeaf(int id,float clas,float prob)
{
_cartreeArray[id]=new ClasNode();
_cartreeArray[id]->setLeaf(true);
((ClasNode*)_cartreeArray[id])->setClass(clas);
((ClasNode*)_cartreeArray[id])->setProb(prob);
}
/************************************************/
//Regression Tree
RegrTree::RegrTree(int MaxDepth,int trainFeatureNumPerNode,int minLeafSample,float minInfoGain,bool isRegression)
:Tree(MaxDepth,trainFeatureNumPerNode,minLeafSample,minInfoGain,isRegression)
{}
RegrTree::~RegrTree()
{}
void RegrTree::train(Sample*sample)
{
//initialize root node
//random generate feature index
int*_featureIndex=new int[_trainFeatureNumPerNode];
Sample*nodeSample=new Sample(sample,0,sample->getSelectedSampleNum()-1);
_cartreeArray[0]=new RegrNode();
_cartreeArray[0]->_samples=nodeSample;
//calculate the mean and variance
_cartreeArray[0]->calculateParams();
for(int i=0;i<_nodeNum;++i)
{
int parentId=(i-1)/2;
//if current node's parent node is NULL,continue
if(_cartreeArray[parentId]==NULL)
{continue;}
//if the current node's parent node is a leaf,continue
if(i>0&&_cartreeArray[parentId]->isLeaf())
{continue;}
//if it reach the max depth
//set current node as a leaf and continue
if(i*2+1>=_nodeNum) //if left child node is out of range
{
_cartreeArray[i]->createLeaf();
continue;
}
//if current samples in this node is less than the threshold
//set current node as a leaf and continue
if(_cartreeArray[i]->_samples->getSelectedSampleNum()<=_minLeafSample)
{
_cartreeArray[i]->createLeaf();
continue;
}
_cartreeArray[i]->_samples->randomSelectFeature
(_featureIndex,sample->getFeatureNum(),_trainFeatureNumPerNode);
//else calculate the information gain
_cartreeArray[i]->calculateInfoGain(_cartreeArray,i,_minInfoGain);
_cartreeArray[i]->_samples->releaseSampleIndex();
}
delete[] _featureIndex;
_featureIndex=NULL;
delete nodeSample;
}
void RegrTree::createNode(int id,int featureIndex,float threshold)
{
_cartreeArray[id]=new RegrNode();
_cartreeArray[id]->setLeaf(false);
_cartreeArray[id]->setFeatureIndex(featureIndex);
_cartreeArray[id]->setThreshold(threshold);
}
void RegrTree::createLeaf(int id,float value)
{
_cartreeArray[id]=new RegrNode();
_cartreeArray[id]->setLeaf(true);
((RegrNode*)_cartreeArray[id])->setValue(value);
}