Commit dea77701 authored by ahmedaj's avatar ahmedaj
Browse files

Tree level explanation added

parent 3a6abe9f
......@@ -109,6 +109,15 @@ RandomForest::getForestPrediction(vector <string> test, RandomForest *forest, ve
predictions[treeIdx] = prediction;
explanations[treeIdx] = report;
map<string, vector<std::pair < int, double> >>::iterator itr;
cout << "Explanation "<<treeIdx<< " classified "<< report->classification << " because ";
for (itr = report->path.begin() ; itr != report->path.end(); ++itr) {
if(itr->first == report->classification){
sort(itr->second, test, features);
}
}
}
//pick the biggest voted label
......@@ -157,8 +166,6 @@ map<int, double> explain(string classification, vector<PredictionReport *> repor
}
}
return explanation;
}
......@@ -168,6 +175,35 @@ bool cmp(pair<int, double> &a,
return a.second > b.second;
}
vector <pair<int, double>> sort(vector<std::pair < int, double> > &M, vector <string> test, vector <string> features) {
// Declare vector of pairs
vector <pair<int, double>> A;
// Copy key-value pair from Map
// to vector of pairs
for (auto &it : M) {
A.push_back(it);
}
// Sort using comparator function
sort(A.begin(), A.end(), cmp);
// Print the sorted value
int count = 0;
for (auto &it : A) {
if (count > 2) {
break;
}
cout << features[it.first] << " is " << test[it.first]<< "(information gain: "<<it.second << "), ";
count++;
}
cout << endl;
return A;
}
// Function to sort the map according
// to value in a (key-value) pairs
vector <pair<int, double>> sort(map<int, double> &M, vector <string> test, vector <string> features) {
......
......@@ -28,6 +28,7 @@ vector <string> getBatchPrediction(vector<vector<string>> testData, RandomForest
map<int, double> explain(string classification, vector<PredictionReport *> reports);
vector<pair<int, double> > sort(map<int, double> &M, vector <string> test, vector <string> features);
vector<pair<int, double> > sort(vector<std::pair < int, double> > &M, vector <string> test, vector <string> features);
bool cmp(pair<int, double> &a, pair<int, double> &b);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment