2 #define GRT_DLL_EXPORTS 20 if( (x[ featureIndexA ] - x[ featureIndexB ]) >= (x[ featureIndexC ] - x[ featureIndexB ]) )
return true;
39 std::ostringstream stream;
42 std::cout << stream.str();
52 for(UINT i=0; i<depth; i++) tab +=
"\t";
54 stream << tab <<
"depth: " << depth;
55 stream <<
" nodeSize: " << nodeSize;
56 stream <<
" featureIndexA: " << featureIndexA;
57 stream <<
" featureIndexB: " << featureIndexB;
58 stream <<
" featureIndexC: " << featureIndexC;
59 stream <<
" isLeafNode: " << isLeafNode << std::endl;
61 stream << tab <<
"ClassProbabilities: ";
62 for(UINT i=0; i<classProbabilities.size(); i++){
63 stream << classProbabilities[i] <<
"\t";
67 if( leftChild != NULL ){
68 stream << tab <<
"LeftChild: " << std::endl;
72 if( rightChild != NULL ){
73 stream << tab <<
"RightChild: " << std::endl;
90 node->isLeafNode = isLeafNode;
91 node->nodeID = nodeID;
92 node->predictedNodeID = predictedNodeID;
93 node->nodeSize = nodeSize;
94 node->featureIndexA = featureIndexA;
95 node->featureIndexB = featureIndexB;
96 node->featureIndexC = featureIndexC;
97 node->classProbabilities = classProbabilities;
101 node->leftChild = leftChild->
deepCopy();
102 node->leftChild->setParent( node );
107 node->rightChild = rightChild->
deepCopy();
108 node->rightChild->setParent( node );
111 return dynamic_cast< Node*
>( node );
115 return featureIndexA;
119 return featureIndexB;
123 return featureIndexC;
127 this->nodeSize = nodeSize;
128 this->featureIndexA = featureIndexA;
129 this->featureIndexB = featureIndexB;
130 this->featureIndexC = featureIndexC;
131 this->classProbabilities = classProbabilities;
135 bool DecisionTreeTripleFeatureNode::computeBestSplitBestIterativeSplit(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
136 return computeSplit( numSplittingSteps, trainingData, features, classLabels, featureIndex, minError);
139 bool DecisionTreeTripleFeatureNode::computeBestSplitBestRandomSplit(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
140 return computeSplit( numSplittingSteps, trainingData, features, classLabels, featureIndex, minError);
143 bool DecisionTreeTripleFeatureNode::computeSplit(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
146 const UINT N = features.
getSize();
147 const UINT K = classLabels.
getSize();
149 if( N == 0 )
return false;
153 UINT bestFeatureIndexA = 0;
154 UINT bestFeatureIndexB = 0;
155 UINT bestFeatureIndexC = 0;
157 Float giniIndexL = 0;
158 Float giniIndexR = 0;
168 UINT numRandomFeatures = numSplittingSteps > N ? N : numSplittingSteps;
172 for(UINT n=0; n<numRandomFeatures; n++){
175 featureIndexB = features[ randomFeatures[n] ];
176 featureIndexA = features[ randomFeatures[ random.
getRandomNumberInt(0,numRandomFeatures) ] ];
177 featureIndexC = features[ randomFeatures[ random.
getRandomNumberInt(0,numRandomFeatures) ] ];
180 groupCounter[0] = groupCounter[1] = 0;
182 for(UINT i=0; i<M; i++){
183 groupIndex[i] =
predict( trainingData[i].getSample() ) ? 1 : 0;
184 groupCounter[ groupIndex[i] ]++;
185 classProbabilities[ getClassLabelIndexValue(trainingData[i].getClassLabel(),classLabels) ][ groupIndex[i] ]++;
189 for(UINT k=0; k<K; k++){
190 classProbabilities[k][0] = groupCounter[0]>0 ? classProbabilities[k][0]/groupCounter[0] : 0;
191 classProbabilities[k][1] = groupCounter[1]>0 ? classProbabilities[k][1]/groupCounter[1] : 0;
195 giniIndexL = giniIndexR = 0;
196 for(UINT k=0; k<K; k++){
197 giniIndexL += classProbabilities[k][0] * (1.0-classProbabilities[k][0]);
198 giniIndexR += classProbabilities[k][1] * (1.0-classProbabilities[k][1]);
200 weightL = groupCounter[0]/M;
201 weightR = groupCounter[1]/M;
202 error = (giniIndexL*weightL) + (giniIndexR*weightR);
205 if( error < minError ){
207 bestFeatureIndexA = featureIndexA;
208 bestFeatureIndexB = featureIndexB;
209 bestFeatureIndexC = featureIndexC;
213 trainingLog <<
"Best features indexs: [" << bestFeatureIndexA <<
"," << bestFeatureIndexB <<
"," << bestFeatureIndexC <<
"] Min Error: " << minError << std::endl;
216 featureIndex = bestFeatureIndexB;
219 set(M,bestFeatureIndexA,bestFeatureIndexB,bestFeatureIndexC,trainingData.getClassProbabilities(classLabels));
226 if( !file.is_open() )
228 errorLog <<
"saveParametersToFile(fstream &file) - File is not open!" << std::endl;
234 errorLog <<
"saveParametersToFile(fstream &file) - Failed to save DecisionTreeNode parameters to file!" << std::endl;
239 file <<
"FeatureIndexA: " << featureIndexA << std::endl;
240 file <<
"FeatureIndexB: " << featureIndexB << std::endl;
241 file <<
"FeatureIndexC: " << featureIndexC << std::endl;
250 errorLog <<
"loadParametersFromFile(fstream &file) - File is not open!" << std::endl;
256 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to load DecisionTreeNode parameters from file!" << std::endl;
263 if( word !=
"FeatureIndexA:" ){
264 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexA header!" << std::endl;
267 file >> featureIndexA;
270 if( word !=
"FeatureIndexB:" ){
271 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexB header!" << std::endl;
274 file >> featureIndexB;
277 if( word !=
"FeatureIndexC:" ){
278 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexC header!" << std::endl;
281 file >> featureIndexC;
virtual bool predict(VectorFloat inputVector)
bool set(const UINT nodeSize, const UINT featureIndexA, const UINT featureIndexB, const UINT featureIndexC, const VectorFloat &classProbabilities)
virtual bool clear() override
virtual bool getModel(std::ostream &stream) const override
This file contains the Random class, a useful wrapper for generating cross platform random functions...
virtual bool print() const override
virtual bool loadParametersFromFile(std::fstream &file) override
virtual bool saveParametersToFile(std::fstream &file) const override
virtual bool saveParametersToFile(std::fstream &file) const override
DecisionTreeTripleFeatureNode()
virtual Node * deepCopy() const override
virtual bool loadParametersFromFile(std::fstream &file) override
bool setAllValues(const T &value)
Vector< unsigned int > getRandomSubset(const unsigned int startRange, const unsigned int endRange, const unsigned int subsetSize)
UINT getNumSamples() const
virtual bool getModel(std::ostream &stream) const override
This class implements a DecisionTreeTripleFeatureNode, which is a specific type of node used for a De...
UINT getFeatureIndexB() const
Vector< MinMax > getRanges() const
int getRandomNumberInt(int minRange, int maxRange)
virtual ~DecisionTreeTripleFeatureNode()
virtual bool clear() override
virtual bool predict_(VectorFloat &x) override
UINT getFeatureIndexA() const
virtual Node * deepCopy() const
UINT getFeatureIndexC() const