From d263f4b3a4f96c672317d317061f6adb72656427 Mon Sep 17 00:00:00 2001 From: Kathryn Iverson Date: Fri, 26 Oct 2012 17:16:10 -0400 Subject: [PATCH] restructuring random forest implementation --- forest.cpp | 5 +++-- forest.h | 2 ++ randomforest.cpp | 5 +++-- randomforest.hpp | 18 +++++++++--------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/forest.cpp b/forest.cpp index 8ac1b79..58c7f7e 100644 --- a/forest.cpp +++ b/forest.cpp @@ -27,10 +27,11 @@ treeSplitCriterion(treeSplitCriterion) { vector Forest::getGlobalDiscardedFeatureIndices() { try { - vector globalDiscardedFeatureIndices; + //vector globalDiscardedFeatureIndices; + //globalDiscardedFeatureIndices.push_back(1); // calculate feature vectors - vector< vector > featureVectors(numFeatures, vector(numSamples, 0)); + vector< vector > featureVectors(numFeatures, vector(numSamples, 0) ); for (int i = 0; i < numSamples; i++) { if (m->control_pressed) { return globalDiscardedFeatureIndices; } for (int j = 0; j < numFeatures; j++) { featureVectors[j][i] = dataSet[i][j]; } diff --git a/forest.h b/forest.h index c9d29dc..78f61b3 100644 --- a/forest.h +++ b/forest.h @@ -12,6 +12,7 @@ #include #include "mothurout.h" #include "macros.h" +#include "decisiontree.hpp" #include "abstractdecisiontree.hpp" /***********************************************************************/ //this is a re-implementation of the abstractrandomforest class @@ -26,6 +27,7 @@ public: virtual int populateDecisionTrees() = 0; virtual int calcForrestErrorRate() = 0; virtual int calcForrestVariableImportance(string) = 0; + virtual int updateGlobalOutOfBagEstimates(DecisionTree* decisionTree) = 0; /***********************************************************************/ diff --git a/randomforest.cpp b/randomforest.cpp index 36a2c1a..bd96cd2 100644 --- a/randomforest.cpp +++ b/randomforest.cpp @@ -11,7 +11,7 @@ /***********************************************************************/ RandomForest::RandomForest(const vector > dataSet,const int numDecisionTrees, - const string treeSplitCriterion = "informationGain") : AbstractRandomForest(dataSet, numDecisionTrees, treeSplitCriterion) { + const string treeSplitCriterion = "informationGain") : Forest(dataSet, numDecisionTrees, treeSplitCriterion) { m = MothurOut::getInstance(); } @@ -58,6 +58,7 @@ int RandomForest::calcForrestVariableImportance(string filename) { //could cause maintenance issues later if other types of Abstract decison trees are created that cannot be cast as a decision tree. for (int i = 0; i < decisionTrees.size(); i++) { if (m->control_pressed) { return 0; } + DecisionTree* decisionTree = dynamic_cast(decisionTrees[i]); for (int j = 0; j < numFeatures; j++) { @@ -127,7 +128,7 @@ int RandomForest::populateDecisionTrees() { } /***********************************************************************/ // TODO: need to finalize bettween reference and pointer for DecisionTree [partially solved] -// TODO: make this pure virtual in superclass +// DONE: make this pure virtual in superclass // DONE int RandomForest::updateGlobalOutOfBagEstimates(DecisionTree* decisionTree) { try { diff --git a/randomforest.hpp b/randomforest.hpp index e67a6b9..30eb438 100755 --- a/randomforest.hpp +++ b/randomforest.hpp @@ -10,7 +10,7 @@ #define rrf_fs_prototype_randomforest_hpp #include "macros.h" -#include "abstractrandomforest.hpp" +#include "forest.h" #include "decisiontree.hpp" class RandomForest: public Forest { @@ -23,14 +23,14 @@ public: //NOTE:: if you are going to dynamically cast, aren't you undoing the advantage of abstraction. Why abstract at all? //could cause maintenance issues later if other types of Abstract decison trees are created that cannot be cast as a decision tree. - virtual ~RandomForest() { - for (vector::iterator it = decisionTrees.begin(); it != decisionTrees.end(); it++) { - // we know that this is decision tree, so we can do a dynamic_case here - DecisionTree* decisionTree = dynamic_cast(*it); - // calling the destructor by deleting - delete decisionTree; - } - } +// virtual ~RandomForest() { +// for (vector::iterator it = decisionTrees.begin(); it != decisionTrees.end(); it++) { +// // we know that this is decision tree, so we can do a dynamic_case here +// DecisionTree* decisionTree = dynamic_cast(*it); +// // calling the destructor by deleting +// delete decisionTree; +// } +// } int calcForrestErrorRate(); int calcForrestVariableImportance(string); -- 2.39.2