-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternal.cpp
More file actions
88 lines (70 loc) · 2.22 KB
/
Internal.cpp
File metadata and controls
88 lines (70 loc) · 2.22 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
#include "Internal.h"
#include "global.h"
using namespace std;
//Randomly select a feature from top three features as determined by information gain
unsigned int Internal::randomFeature(unsigned int numberOfFeatures)
{
vector<double> probabilities;
if(features.size() < numberOfFeatures)
{
for(unsigned int i = 0; i < features.size(); ++i)
probabilities.push_back(1.0/features.size());
}
else
{
for(unsigned int i = 0; i < numberOfFeatures; ++i)
probabilities.push_back(1.0/numberOfFeatures);
for(unsigned int i = numberOfFeatures; i < features.size(); ++i)
probabilities.push_back(0.0);
}
dist = discrete_distribution<>(probabilities.begin(), probabilities.end());
unsigned int r = dist(gen);
return orderedGains[r].second;
}
//effects of removing an internal node
void Internal::cut()
{
--numberOfNodes;
for(vector<shared_ptr<Node> >::iterator child = children.begin(); child != children.end(); ++child)
{
(*child)->cut();
}
children.clear();
}
//recursively print internal nodes and thier branches
void Internal::printTree(unsigned int d)
{
for(vector<shared_ptr<Node> >::iterator child = children.begin(); child != children.end(); ++child)
{
cout << endl;
for(unsigned int i = 0; i < d; ++i)
cout << "| ";
cout << (attributes[feature]).first << " = " << (attributes[feature]).second[(*child)->value];
(*child)->printTree(d+1);
}
}
//recursively save internal nodes of decision tree in C++ file
void Internal::recursiveSave(ofstream &treeWriter, unsigned int depth)
{
for(vector<shared_ptr<Node> >::iterator it = children.begin(); it != children.end(); ++it)
{
for(unsigned int i = 0; i < depth; ++i)
treeWriter << " ";
treeWriter << " if(((*i)[" << feature << "]).compare(\"" << (attributes[feature]).second[(*it)->value] << "\") == 0)" << endl;
for(unsigned int i = 0; i < depth; ++i)
treeWriter << " ";
treeWriter << " {" << endl;
//treeWriter.flush();
(*it)->recursiveSave(treeWriter, depth + 1);
//treeWriter.flush();
for(unsigned int i = 0; i < depth; ++i)
treeWriter << " ";
treeWriter << " }" << endl;
}
}
Internal::Internal(void)
{
}
Internal::~Internal(void)
{
}