10 nodeType =
"DecisionTreeTripleFeatureNode";
23 if( (x[ featureIndexA ] - x[ featureIndexB ]) >= (x[ featureIndexC ] - x[ featureIndexB ]) )
return true;
42 std::ostringstream stream;
45 std::cout << stream.str();
55 for(UINT i=0; i<depth; i++) tab +=
"\t";
57 stream << tab <<
"depth: " << depth;
58 stream <<
" nodeSize: " << nodeSize;
59 stream <<
" featureIndexA: " << featureIndexA;
60 stream <<
" featureIndexB: " << featureIndexB;
61 stream <<
" featureIndexC: " << featureIndexC;
62 stream <<
" isLeafNode: " << isLeafNode << std::endl;
64 stream << tab <<
"ClassProbabilities: ";
65 for(UINT i=0; i<classProbabilities.size(); i++){
66 stream << classProbabilities[i] <<
"\t";
70 if( leftChild != NULL ){
71 stream << tab <<
"LeftChild: " << std::endl;
75 if( rightChild != NULL ){
76 stream << tab <<
"RightChild: " << std::endl;
93 node->isLeafNode = isLeafNode;
94 node->nodeID = nodeID;
95 node->predictedNodeID = predictedNodeID;
96 node->nodeSize = nodeSize;
97 node->featureIndexA = featureIndexA;
98 node->featureIndexB = featureIndexB;
99 node->featureIndexC = featureIndexC;
100 node->classProbabilities = classProbabilities;
105 node->leftChild->setParent( node );
111 node->rightChild->setParent( node );
122 return featureIndexA;
126 return featureIndexB;
130 return featureIndexC;
134 this->nodeSize = nodeSize;
135 this->featureIndexA = featureIndexA;
136 this->featureIndexB = featureIndexB;
137 this->featureIndexC = featureIndexC;
138 this->classProbabilities = classProbabilities;
142 bool DecisionTreeTripleFeatureNode::computeBestSpiltBestIterativeSpilt(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
144 return computeBestSpilt( numSplittingSteps, trainingData, features, classLabels, featureIndex, minError);
147 bool DecisionTreeTripleFeatureNode::computeBestSpiltBestRandomSpilt(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
149 return computeBestSpilt( numSplittingSteps, trainingData, features, classLabels, featureIndex, minError);
152 bool DecisionTreeTripleFeatureNode::computeBestSpilt(
const UINT &numSplittingSteps,
const ClassificationData &trainingData,
const Vector< UINT > &features,
const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
155 const UINT N = features.
getSize();
156 const UINT K = classLabels.
getSize();
158 if( N == 0 )
return false;
162 UINT bestFeatureIndexA = 0;
163 UINT bestFeatureIndexB = 0;
164 UINT bestFeatureIndexC = 0;
166 Float giniIndexL = 0;
167 Float giniIndexR = 0;
177 UINT numRandomFeatures = numSplittingSteps > N ? N : numSplittingSteps;
181 for(UINT n=0; n<numRandomFeatures; n++){
184 featureIndexB = features[ randomFeatures[n] ];
185 featureIndexA = features[ randomFeatures[ random.
getRandomNumberInt(0,numRandomFeatures) ] ];
186 featureIndexC = features[ randomFeatures[ random.
getRandomNumberInt(0,numRandomFeatures) ] ];
189 groupCounter[0] = groupCounter[1] = 0;
190 classProbabilities.setAllValues(0);
191 for(UINT i=0; i<M; i++){
192 groupIndex[i] =
predict( trainingData[i].getSample() ) ? 1 : 0;
193 groupCounter[ groupIndex[i] ]++;
194 classProbabilities[ getClassLabelIndexValue(trainingData[i].getClassLabel(),classLabels) ][ groupIndex[i] ]++;
198 for(UINT k=0; k<K; k++){
199 classProbabilities[k][0] = groupCounter[0]>0 ? classProbabilities[k][0]/groupCounter[0] : 0;
200 classProbabilities[k][1] = groupCounter[1]>0 ? classProbabilities[k][1]/groupCounter[1] : 0;
204 giniIndexL = giniIndexR = 0;
205 for(UINT k=0; k<K; k++){
206 giniIndexL += classProbabilities[k][0] * (1.0-classProbabilities[k][0]);
207 giniIndexR += classProbabilities[k][1] * (1.0-classProbabilities[k][1]);
209 weightL = groupCounter[0]/M;
210 weightR = groupCounter[1]/M;
211 error = (giniIndexL*weightL) + (giniIndexR*weightR);
214 if( error < minError ){
216 bestFeatureIndexA = featureIndexA;
217 bestFeatureIndexB = featureIndexB;
218 bestFeatureIndexC = featureIndexC;
222 trainingLog <<
"Best features indexs: [" << bestFeatureIndexA <<
"," << bestFeatureIndexB <<
"," << bestFeatureIndexC <<
"] Min Error: " << minError << std::endl;
225 featureIndex = bestFeatureIndexB;
228 set(M,bestFeatureIndexA,bestFeatureIndexB,bestFeatureIndexC,trainingData.getClassProbabilities(classLabels));
235 if( !file.is_open() )
237 errorLog <<
"saveParametersToFile(fstream &file) - File is not open!" << std::endl;
243 errorLog <<
"saveParametersToFile(fstream &file) - Failed to save DecisionTreeNode parameters to file!" << std::endl;
248 file <<
"FeatureIndexA: " << featureIndexA << std::endl;
249 file <<
"FeatureIndexB: " << featureIndexB << std::endl;
250 file <<
"FeatureIndexC: " << featureIndexC << std::endl;
259 errorLog <<
"loadParametersFromFile(fstream &file) - File is not open!" << std::endl;
265 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to load DecisionTreeNode parameters from file!" << std::endl;
272 if( word !=
"FeatureIndexA:" ){
273 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexA header!" << std::endl;
276 file >> featureIndexA;
279 if( word !=
"FeatureIndexB:" ){
280 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexB header!" << std::endl;
283 file >> featureIndexB;
286 if( word !=
"FeatureIndexC:" ){
287 errorLog <<
"loadParametersFromFile(fstream &file) - Failed to find FeatureIndexC header!" << std::endl;
290 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()
unsigned int getSize() const
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