-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.cpp
More file actions
152 lines (146 loc) · 5.35 KB
/
DecisionTree.cpp
File metadata and controls
152 lines (146 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "DecisionTree.h"
#include <iostream>
#include <limits>
#include <cmath>
using namespace std;
DecisionTree::DecisionTree(){
root = nullptr;
}
DecisionTree::~DecisionTree(){
deleteTree(root);
}
void DecisionTree::deleteTree(TreeNode* currentNode){
if (currentNode == nullptr){
return;
}
deleteTree(currentNode->left);
deleteTree(currentNode->right);
delete currentNode;
}
double DecisionTree::calculateMeanSquaredError(const vector<double>& outcomes) {
if (outcomes.empty()) {
return 0;
}
double sum = 0.0;
for (double price : outcomes) {
sum += price;
}
double average = sum / outcomes.size();
double meanSquaredError = 0.0;
for (double price : outcomes) {
double error = price - average;
meanSquaredError += error * error;
}
return meanSquaredError / outcomes.size();
}
double DecisionTree::calculateSplitScore(const vector<vector<double>>& featureVectors, const vector<double>& targetOutcomes, int featureIndex, double splitThreshold){
vector<double> leftOutcomes, rightOutcomes;
for (size_t i = 0; i < featureVectors.size(); i++){
if (featureVectors[i][featureIndex] <= splitThreshold){
leftOutcomes.push_back(targetOutcomes[i]);
}
else{
rightOutcomes.push_back(targetOutcomes[i]);
}
}
if (leftOutcomes.empty() || rightOutcomes.empty()){
return numeric_limits<double>::max();
}
double leftMSE = calculateMeanSquaredError(leftOutcomes);
double rightMSE = calculateMeanSquaredError(rightOutcomes);
double totalDataPoints = (double)targetOutcomes.size();
double weightedMSE = (leftOutcomes.size() * leftMSE + rightOutcomes.size() * rightMSE) / totalDataPoints;
return weightedMSE;
}
TreeNode* DecisionTree::buildTree(const vector<vector<double>>& featureVectors, const vector<double>& targetOutcomes, int currentDepth){
TreeNode* currentNode = new TreeNode();
double currentMSE = calculateMeanSquaredError(targetOutcomes);
if (targetOutcomes.size() < 10 || currentDepth >= 5){
currentNode->isLeaf = true;
double sum = 0.0;
if (!targetOutcomes.empty()){
for (double price: targetOutcomes){
sum += price;
}
currentNode->prediction = sum / targetOutcomes.size();
} else {
currentNode->prediction = 0.0;
}
return currentNode;
}
double lowestMSE = numeric_limits<double>::max();
int bestFeatureIndex = -1;
double bestSplitThreshold = 0;
int numberOfFeatures = featureVectors[0].size();
for (int featureIndex = 0; featureIndex < numberOfFeatures; featureIndex++){
for (const auto& dataPoint : featureVectors){
double trialCutoff = dataPoint[featureIndex];
double currentMSEScore = calculateSplitScore(featureVectors, targetOutcomes, featureIndex, trialCutoff);
if (currentMSEScore < lowestMSE){
lowestMSE = currentMSEScore;
bestFeatureIndex = featureIndex;
bestSplitThreshold = trialCutoff;
}
}
}
if (bestFeatureIndex == -1){
currentNode->isLeaf = true;
double sum = 0;
for (double price : targetOutcomes){
sum += price;
}
currentNode->prediction = sum / targetOutcomes.size();
return currentNode;
}
currentNode->questionIndex = bestFeatureIndex;
currentNode->cutoffValue = bestSplitThreshold;
vector<vector<double>> leftFeatureVectors, rightFeatureVectors;
vector<double> leftOutcomes, rightOutcomes;
for (size_t i = 0; i < featureVectors.size(); i++){
if (featureVectors[i][bestFeatureIndex] <= bestSplitThreshold){
leftFeatureVectors.push_back(featureVectors[i]);
leftOutcomes.push_back(targetOutcomes[i]);
}
else{
rightFeatureVectors.push_back(featureVectors[i]);
rightOutcomes.push_back(targetOutcomes[i]);
}
}
currentNode->left = buildTree(leftFeatureVectors, leftOutcomes, currentDepth + 1);
currentNode->right = buildTree(rightFeatureVectors, rightOutcomes, currentDepth + 1);
return currentNode;
}
void DecisionTree::train(const vector<vector<double>>& featureVectors, const vector<double>& targetOutcomes){
if (root != nullptr){
deleteTree(root);
root = nullptr;
}
cout << "Starting Decision Tree training with " << featureVectors.size() << " data points..." << endl;
root = buildTree(featureVectors, targetOutcomes, 0);
cout << "Training complete." << endl;
}
double DecisionTree::walk(TreeNode* currentNode, const vector<double>& featureAnswers){
if (currentNode->isLeaf){
return currentNode->prediction;
}
if (featureAnswers[currentNode->questionIndex] <= currentNode->cutoffValue){
return walk(currentNode->left, featureAnswers);
}
else{
return walk(currentNode->right, featureAnswers);
}
}
double DecisionTree::predict(const vector<double>& featureAnswers){
if (root == nullptr){
cerr << "Error: The tree has not been trained yet!" << endl;
return 0.0;
}
return walk(root, featureAnswers);
}
vector<double> DecisionTree::predictBatch(const vector<vector<double>>& rows){
vector<double> outputPredictions;
for (const auto& row : rows){
outputPredictions.push_back(predict(row));
}
return outputPredictions;
}