-
Notifications
You must be signed in to change notification settings - Fork 9
/
Node.cpp
207 lines (187 loc) · 5.49 KB
/
Node.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
#include "Node.h"
#include <vector>
#include "FeatureFactory.h"
#include <iostream>
#include "utils.h"
Node::Node():
_isLeaf(false),
_samples(nullptr),
_bestFeat(Features()),
_class(-1),
_prob(0.0)
{}
float Node::computeGini(std::vector<int> & samplesId, std::vector<float> &probs)
{
int numSamples = samplesId.size();
int numClasses = _samples->getNumClasses();
probs.resize(numClasses, 0);
for (int i = 0; i < numSamples; ++i)
{
// count the number of instances of each class
probs[(*(_samples->_labels))[samplesId[i]]]++;
}
float gini = 0;
for (int i = 0; i < numClasses; ++i)
{
probs[i] = probs[i] / (float)numSamples;
gini += (probs[i] * probs[i]);
}
return 1 - gini;
}
// calculate the gini of the samples in this node
void Node::computeNodeGini()
{
Eigen::VectorXi samplesId = _samples->getSelectedSamplesId();
// convert Eigen::Vector to std::vector
std::vector<int> samplesIdVec = toStdVec(samplesId);
_gini = computeGini(samplesIdVec, _probs);
}
// split the node into two children and compute the gini at each child node
void Node::computeInfoGain(std::vector<Node*> &nodes, int nodeId)
{
Eigen::MatrixXf data = *(_samples->_dataset);
Eigen::VectorXi labels =*( _samples->_labels);
//Eigen::MatrixXf cloud = *(_samples->_cloud);
//Eigen::VectorXi truths = *(_samples->_truths);
// randomly samples some points from the cloud
// and store the selected points id in sampleId
Eigen::VectorXi sampleId = _samples->getSelectedSamplesId();
int numSamples = sampleId.size();
// for this work, numClasses is 8
int numClasses = _samples->getNumClasses();
_samples->randomSampleFeatures();
std::vector<Features> featCandidates = _samples->getSelectedFeatures();
int numFeats = _samples->getNumFeatures();
// variables for storing the final parameters
float bestInfoGain = 0;
float bestLeftGini = 0;
float bestRightGini = 0;
Features bestFeat = featCandidates[0];
std::vector<int> bestLeftChild;
std::vector<int> bestRightChild;
std::vector<float> bestLeftProbs;
std::vector<float> bestRightProbs;
for (int i = 0; i < numFeats; ++i)
{
// apply each candidate feature to the samples at this node
float infoGain = 0;
float leftGini = 0;
float rightGini = 0;
Features feat = featCandidates[i];
std::vector<int> leftChildSamples;
std::vector<int> rightChildSamples;
std::vector<float> leftProbs;
std::vector<float> rightProbs;
std::vector<float> castResults;
std::vector<float> forMedian;
for (int j = 0; j < numSamples; ++j)
{
Eigen::MatrixXf neigh = _samples->buildNeighborhood(sampleId[j]);
FeatureFactory nodeFeat(neigh, feat);
float castResult = 0.0f;
bool success = true;
success = nodeFeat.project(castResult);
castResults.push_back(castResult);
if (success)
forMedian.push_back(castResult);
}
// find the median of all valid projections
if (!forMedian.empty())
feat._thresh = findMedian(forMedian);
// all projections are not valid, continue
else
continue;
for (int j = 0; j < numSamples; ++j)
{
if (castResults[j] < feat._thresh)
leftChildSamples.push_back(sampleId[j]);
else
rightChildSamples.push_back(sampleId[j]);
}
if (leftChildSamples.size() == 0 or rightChildSamples.size() == 0)
{
continue;
}
leftGini = computeGini(leftChildSamples, leftProbs);
rightGini = computeGini(rightChildSamples, rightProbs);
float leftRatio = leftChildSamples.size() / (float)numSamples;
float rightRatio = rightChildSamples.size() / (float)numSamples;
infoGain = _gini - leftGini * leftRatio - rightGini * rightRatio;
if (infoGain > bestInfoGain)
{
bestInfoGain = infoGain;
bestLeftGini = leftGini;
bestRightGini = rightGini;
bestFeat = feat;
bestLeftChild = leftChildSamples;
bestRightChild = rightChildSamples;
bestLeftProbs = leftProbs;
bestRightProbs = rightProbs;
}
}
if (bestLeftChild.size() == 0 or bestRightChild.size()==0 or bestInfoGain<0)
{
createLeaf(nodes[0]->_probs);
}
else
{
_bestFeat = bestFeat;
nodes[nodeId * 2 + 1] = new Node();
nodes[nodeId * 2 + 2] = new Node();
(nodes[nodeId * 2 + 1])->_gini = bestLeftGini;
(nodes[nodeId * 2 + 2])->_gini = bestRightGini;
(nodes[nodeId * 2 + 1])->_probs = bestLeftProbs;
(nodes[nodeId * 2 + 2])->_probs = bestRightProbs;
Eigen::VectorXi bestLeftChildVec = toEigenVec(bestLeftChild);
Eigen::VectorXi bestRightChildVec = toEigenVec(bestRightChild);
Sample *leftSamples = new Sample(_samples, bestLeftChildVec);
Sample *rightSamples = new Sample(_samples, bestRightChildVec);
(nodes[nodeId * 2 + 1])->_samples = leftSamples;
(nodes[nodeId * 2 + 2])->_samples = rightSamples;
}
}
// create a leaf and update the class posterior
void Node::createLeaf(std::vector<float> priorDistr)
{
_class = 0;
int numClasses = _samples->getNumClasses();
int nullClasses = 0; // classes have no instances
float normalizeFactor = 0;
for (int i = 0; i < numClasses; ++i)
{
if (priorDistr[i] == 0)
{
_probs[i] = 0;
nullClasses++;
}
else
{
_probs[i] = _probs[i] / priorDistr[i];
normalizeFactor += _probs[i];
}
}
for (int i = 0; i < numClasses; ++i)
{
_probs[i] /= normalizeFactor;
}
_prob = _probs[0];
for (int i = 1; i < numClasses; ++i)
{
if (_probs[i] > _prob)
{
_class = i;
_prob = _probs[i];
}
}
_isLeaf = true;
}
bool Node::isHomogenous(){
int numSamples = _samples->getSelectedSamplesId().size();
int pointId1 = _samples->getSelectedSamplesId()[0];
for (int i = 1; i < numSamples - 1; ++i)
{
if (_samples->getSelectedSamplesId()[i] != pointId1)
return false;
}
return true;
}