2 #define GRT_DLL_EXPORTS
11 nodeType =
"DecisionTreeTripleFeatureNode";
24 if( (x[ featureIndexA ] - x[ featureIndexB ]) >= (x[ featureIndexC ] - x[ featureIndexB ]) )
return true;
43 std::ostringstream stream;
46 std::cout << stream.str();
56 for(UINT i=0; i<depth; i++) tab +=
"\t";
58 stream << tab <<
"depth: " << depth;
59 stream <<
" nodeSize: " << nodeSize;
60 stream <<
" featureIndexA: " << featureIndexA;
61 stream <<
" featureIndexB: " << featureIndexB;
62 stream <<
" featureIndexC: " << featureIndexC;
63 stream <<
" isLeafNode: " << isLeafNode << std::endl;
65 stream << tab <<
"ClassProbabilities: ";
66 for(UINT i=0; i<classProbabilities.size(); i++){
67 stream << classProbabilities[i] <<
"\t";
71 if( leftChild != NULL ){
72 stream << tab <<
"LeftChild: " << std::endl;
76 if( rightChild != NULL ){
77 stream << tab <<
"RightChild: " << std::endl;
94 node->isLeafNode = isLeafNode;
95 node->nodeID = nodeID;
96 node->predictedNodeID = predictedNodeID;
97 node->nodeSize = nodeSize;
98 node->featureIndexA = featureIndexA;
99 node->featureIndexB = featureIndexB;
100 node->featureIndexC = featureIndexC;
101 node->classProbabilities = classProbabilities;
106 node->leftChild->setParent( node );
112 node->rightChild->setParent( node );
123 return featureIndexA;
127 return featureIndexB;
131 return featureIndexC;
135 this->nodeSize = nodeSize;
136 this->featureIndexA = featureIndexA;
137 this->featureIndexB = featureIndexB;
138 this->featureIndexC = featureIndexC;
139 this->classProbabilities = classProbabilities;
143 bool DecisionTreeTripleFeatureNode::computeBestSpiltBestIterativeSpilt(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
145 return computeBestSpilt( numSplittingSteps, trainingData, features, classLabels, featureIndex, minError);
148 bool DecisionTreeTripleFeatureNode::computeBestSpiltBestRandomSpilt(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
150 return computeBestSpilt( numSplittingSteps, trainingData, features, classLabels, featureIndex, minError);
153 bool DecisionTreeTripleFeatureNode::computeBestSpilt(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
156 const UINT N = features.
getSize();
157 const UINT K = classLabels.
getSize();
159 if( N == 0 )
return false;
163 UINT bestFeatureIndexA = 0;
164 UINT bestFeatureIndexB = 0;
165 UINT bestFeatureIndexC = 0;
167 Float giniIndexL = 0;
168 Float giniIndexR = 0;
178 UINT numRandomFeatures = numSplittingSteps > N ? N : numSplittingSteps;
182 for(UINT n=0; n<numRandomFeatures; n++){
185 featureIndexB = features[ randomFeatures[n] ];
186 featureIndexA = features[ randomFeatures[ random.
getRandomNumberInt(0,numRandomFeatures) ] ];
187 featureIndexC = features[ randomFeatures[ random.
getRandomNumberInt(0,numRandomFeatures) ] ];
190 groupCounter[0] = groupCounter[1] = 0;
191 classProbabilities.setAllValues(0);
192 for(UINT i=0; i<M; i++){
193 groupIndex[i] =
predict( trainingData[i].getSample() ) ? 1 : 0;
194 groupCounter[ groupIndex[i] ]++;
195 classProbabilities[ getClassLabelIndexValue(trainingData[i].getClassLabel(),classLabels) ][ groupIndex[i] ]++;
199 for(UINT k=0; k<K; k++){
200 classProbabilities[k][0] = groupCounter[0]>0 ? classProbabilities[k][0]/groupCounter[0] : 0;
201 classProbabilities[k][1] = groupCounter[1]>0 ? classProbabilities[k][1]/groupCounter[1] : 0;
205 giniIndexL = giniIndexR = 0;
206 for(UINT k=0; k<K; k++){
207 giniIndexL += classProbabilities[k][0] * (1.0-classProbabilities[k][0]);
208 giniIndexR += classProbabilities[k][1] * (1.0-classProbabilities[k][1]);
210 weightL = groupCounter[0]/M;
211 weightR = groupCounter[1]/M;
212 error = (giniIndexL*weightL) + (giniIndexR*weightR);
215 if( error < minError ){
217 bestFeatureIndexA = featureIndexA;
218 bestFeatureIndexB = featureIndexB;
219 bestFeatureIndexC = featureIndexC;
223 trainingLog <<
"Best features indexs: [" << bestFeatureIndexA <<
"," << bestFeatureIndexB <<
"," << bestFeatureIndexC <<
"] Min Error: " << minError << std::endl;
226 featureIndex = bestFeatureIndexB;
229 set(M,bestFeatureIndexA,bestFeatureIndexB,bestFeatureIndexC,trainingData.getClassProbabilities(classLabels));
236 if( !file.is_open() )
238 errorLog <<
"saveParametersToFile(fstream &file) - File is not open!" << std::endl;
244 errorLog <<
"saveParametersToFile(fstream &file) - Failed to save DecisionTreeNode parameters to file!" << std::endl;
249 file <<
"FeatureIndexA: " << featureIndexA << std::endl;
250 file <<
"FeatureIndexB: " << featureIndexB << std::endl;
251 file <<
"FeatureIndexC: " << featureIndexC << std::endl;
260 errorLog <<
"loadParametersFromFile(fstream &file) - File is not open!" << std::endl;
266 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to load DecisionTreeNode parameters from file!" << std::endl;
273 if( word !=
"FeatureIndexA:" ){
274 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexA header!" << std::endl;
277 file >> featureIndexA;
280 if( word !=
"FeatureIndexB:" ){
281 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexB header!" << std::endl;
284 file >> featureIndexB;
287 if( word !=
"FeatureIndexC:" ){
288 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexC header!" << std::endl;
291 file >> featureIndexC;
bool set(const UINT nodeSize, const UINT featureIndexA, const UINT featureIndexB, const UINT featureIndexC, const VectorFloat &classProbabilities)
This file implements a DecisionTreeTripleFeatureNode, which is a specific type of node used for a Dec...
virtual bool getModel(std::ostream &stream) const
DecisionTreeTripleFeatureNode()
virtual bool saveParametersToFile(std::fstream &file) const
virtual bool print() const
Vector< unsigned int > getRandomSubset(const unsigned int startRange, const unsigned int endRange, const unsigned int subsetSize)
UINT getNumSamples() const
virtual Node * deepCopyNode() const
virtual bool predict(const VectorFloat &x)
virtual bool getModel(std::ostream &stream) const
virtual bool saveParametersToFile(std::fstream &file) const
UINT getFeatureIndexB() const
DecisionTreeTripleFeatureNode * deepCopy() const
Vector< MinMax > getRanges() const
int getRandomNumberInt(int minRange, int maxRange)
virtual ~DecisionTreeTripleFeatureNode()
UINT getFeatureIndexA() const
virtual bool loadParametersFromFile(std::fstream &file)
virtual Node * deepCopyNode() const
virtual bool loadParametersFromFile(std::fstream &file)
UINT getFeatureIndexC() const