-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecisionTree.hpp
62 lines (45 loc) · 1.48 KB
/
DecisionTree.hpp
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
#ifndef DecisionTree_hpp
#define DecisionTree_hpp
#include <set>
#include "Split.hpp"
class DecisionTree;
struct DecisionTreeNode {
public:
const DecisionTree* owner;
DecisionTreeNode* parent;
int parentLinkNo;
vector<Instance*> instances;
double classLabel;
vector<int> classCount;
Split* split;
vector<DecisionTreeNode*> children;
DecisionTreeNode(const DecisionTree* owner) :owner(owner), parent(0), parentLinkNo(-1), instances(0), classLabel(-1), classCount(0), split(0), children(0) {}
~DecisionTreeNode() {
for (DecisionTreeNode* node : children)
delete node;
if (split)
delete split;
}
string toString() const;
};
class DecisionTree {
private:
DecisionTreeNode* root;
const DatasetMetadata* metadata;
int stopThreshold;
void buildDecisionTree(DecisionTreeNode* root, set<int>& featureIndices);
double predictDecisionTree(DecisionTreeNode* root, const Instance* instance) const;
void printDecisionTree(DecisionTreeNode* node, int level, stringstream& ss) const;
public:
DecisionTree(const DatasetMetadata* metadata, const vector<Instance*>& instances, int stopThreshold);
~DecisionTree() {
if (root)
delete root;
}
const DatasetMetadata* getMetadata() const {
return metadata;
}
string predict(const Instance* instance) const;
string toString() const;
};
#endif /* DecisionTree_hpp */