X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=randomforest.cpp;h=acf87dfebcd022d37cf6974f8331c7ad940a017a;hp=bb8e2d565cb6348594b7c3e5cd229dd40ac37d76;hb=499f4ac6e321f9f03d4c3aa25c3b6880892c8b83;hpb=c2f279a697c7267b5d91882087ab91f7e6653aa2 diff --git a/randomforest.cpp b/randomforest.cpp index bb8e2d5..acf87df 100644 --- a/randomforest.cpp +++ b/randomforest.cpp @@ -37,7 +37,7 @@ int RandomForest::calcForrestErrorRate() { vector::iterator maxPredictedOutComeIterator = max_element(predictedOutComes.begin(), predictedOutComes.end()); int majorityVotedOutcome = (int)(maxPredictedOutComeIterator - predictedOutComes.begin()); int realOutcome = dataSet[indexOfSample][numFeatures]; - + if (majorityVotedOutcome == realOutcome) { numCorrect++; } } @@ -46,7 +46,7 @@ int RandomForest::calcForrestErrorRate() { m->mothurOut("numCorrect = " + toString(numCorrect)+ "\n"); m->mothurOut("forrestErrorRate = " + toString(forrestErrorRate)+ "\n"); - + return 0; } catch(exception& e) { @@ -54,6 +54,87 @@ int RandomForest::calcForrestErrorRate() { exit(1); } } +/***********************************************************************/ + +int RandomForest::printConfusionMatrix(map intToTreatmentMap) { + try { + int numGroups = intToTreatmentMap.size(); + vector > cm(numGroups, vector(numGroups, 0)); + + for (map >::iterator it = globalOutOfBagEstimates.begin(); it != globalOutOfBagEstimates.end(); it++) { + + if (m->control_pressed) { return 0; } + + int indexOfSample = it->first; //key + vector predictedOutComes = it->second; //value, vector of all predicted classes + vector::iterator maxPredictedOutComeIterator = max_element(predictedOutComes.begin(), predictedOutComes.end()); + int majorityVotedOutcome = (int)(maxPredictedOutComeIterator - predictedOutComes.begin()); + int realOutcome = dataSet[indexOfSample][numFeatures]; + cm[realOutcome][majorityVotedOutcome] = cm[realOutcome][majorityVotedOutcome] + 1; + } + + vector fw; + for (int w = 0; w mothurOut("confusion matrix:\n\t\t"); + for (int k = 0; k < numGroups; k++) { + //m->mothurOut(intToTreatmentMap[k] + "\t"); + cout << setw(fw[k]) << intToTreatmentMap[k] << "\t"; + } + for (int i = 0; i < numGroups; i++) { + cout << "\n" << setw(fw[i]) << intToTreatmentMap[i] << "\t"; + //m->mothurOut("\n" + intToTreatmentMap[i] + "\t"); + if (m->control_pressed) { return 0; } + for (int j = 0; j < numGroups; j++) { + //m->mothurOut(toString(cm[i][j]) + "\t"); + cout << setw(fw[i]) << cm[i][j] << "\t"; + } + } + //m->mothurOut("\n"); + cout << "\n"; + + return 0; + } + + catch(exception& e) { + m->errorOut(e, "RandomForest", "printConfusionMatrix"); + exit(1); + } +} + +/***********************************************************************/ + +int RandomForest::getMissclassifications(string filename, map intToTreatmentMap, vector names) { + try { + ofstream out; + m->openOutputFile(filename, out); + out <<"Sample\tRF classification\tActual classification\n"; + for (map >::iterator it = globalOutOfBagEstimates.begin(); it != globalOutOfBagEstimates.end(); it++) { + + if (m->control_pressed) { return 0; } + + int indexOfSample = it->first; + vector predictedOutComes = it->second; + vector::iterator maxPredictedOutComeIterator = max_element(predictedOutComes.begin(), predictedOutComes.end()); + int majorityVotedOutcome = (int)(maxPredictedOutComeIterator - predictedOutComes.begin()); + int realOutcome = dataSet[indexOfSample][numFeatures]; + + if (majorityVotedOutcome != realOutcome) { + out << names[indexOfSample] << "\t" << intToTreatmentMap[majorityVotedOutcome] << "\t" << intToTreatmentMap[realOutcome] << endl; + + } + } + + out.close(); + return 0; + } + catch(exception& e) { + m->errorOut(e, "RandomForest", "getMissclassifications"); + exit(1); + } +} /***********************************************************************/ int RandomForest::calcForrestVariableImportance(string filename) { @@ -78,7 +159,7 @@ int RandomForest::calcForrestVariableImportance(string filename) { vector< pair > globalVariableRanks; for (int i = 0; i < globalVariableImportanceList.size(); i++) { - cout << "[" << i << ',' << globalVariableImportanceList[i] << "], "; + //cout << "[" << i << ',' << globalVariableImportanceList[i] << "], "; if (globalVariableImportanceList[i] > 0) { pair globalVariableRank(0, 0.0); globalVariableRank.first = i; @@ -97,9 +178,9 @@ int RandomForest::calcForrestVariableImportance(string filename) { ofstream out; m->openOutputFile(filename, out); - out <<"OTU\tRank\n"; + out <<"OTU\tMean decrease accuracy\n"; for (int i = 0; i < globalVariableRanks.size(); i++) { - out << m->currentBinLabels[(int)globalVariableRanks[i].first] << '\t' << globalVariableImportanceList[globalVariableRanks[i].first] << endl; + out << m->currentSharedBinLabels[(int)globalVariableRanks[i].first] << '\t' << globalVariableImportanceList[globalVariableRanks[i].first] << endl; } out.close(); return 0; @@ -118,7 +199,7 @@ int RandomForest::populateDecisionTrees() { for (int i = 0; i < numDecisionTrees; i++) { if (m->control_pressed) { return 0; } - if (((i+1) % 10) == 0) { m->mothurOut("Creating " + toString(i+1) + " (th) Decision tree\n"); } + if (((i+1) % 100) == 0) { m->mothurOut("Creating " + toString(i+1) + " (th) Decision tree\n"); } // TODO: need to first fix if we are going to use pointer based system or anything else DecisionTree* decisionTree = new DecisionTree(dataSet, globalDiscardedFeatureIndices, OptimumFeatureSubsetSelector(optimumFeatureSubsetSelectionCriteria), treeSplitCriterion, featureStandardDeviationThreshold);