-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDRandomForest.cpp
173 lines (110 loc) · 4.13 KB
/
DRandomForest.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "DRandomForest.h"
DRandomForest::DRandomForest(unsigned int _dtreeCount, unsigned int _maxDepth, unsigned int _minSamplesPerSplit,
unsigned int _minSamplesPerLeaf, double _impurityThreshold,
bool _bootstrappingAllowed, bool _regression, bool _multithread,
ImpurityFunctor _impurityFunction, FeatureFunctor _featureFunction) :
dtreeCount(_dtreeCount), maxDepth(_maxDepth), minSamplesPerSplit(_minSamplesPerSplit),
minSamplesPerLeaf(_minSamplesPerLeaf), impurityThreshold(_impurityThreshold),
bootstrappingAllowed(_bootstrappingAllowed), regression(_regression),
multithread(_multithread), isTrained(false), outOfBagError(100.0),
impurityFunction(_impurityFunction), featureFunction(_featureFunction)
{
threadCount = multithread ? std::thread::hardware_concurrency() : 1;
decisionTrees.reserve(_dtreeCount);
}
void DRandomForest::multiThreadFit(const DData& data, unsigned int _threadCount)
{
ThreadPool threadPool(_threadCount);
std::vector<std::future<DTree>> poolResults;
for (unsigned int treeIndex = 0; treeIndex < dtreeCount; treeIndex++)
{
poolResults.emplace_back(threadPool.enqueue(
[&, treeIndex]()
{
DTree dtree(maxDepth, minSamplesPerSplit, minSamplesPerLeaf, impurityThreshold,
bootstrappingAllowed, regression,
impurityFunction, featureFunction
);
dtree.fit(data);
return dtree;
}
));
}
std::vector<std::future<DTree>>::iterator resultIterator = poolResults.begin();
for (resultIterator; resultIterator != poolResults.end(); resultIterator++)
decisionTrees.push_back(resultIterator->get());
}
void DRandomForest::calculateOutOfBagError()
{
if (!isTrained)
return;
double errorSum = 0.0;
std::vector<DTree>::const_iterator treeIt = decisionTrees.begin();
for (treeIt; treeIt != decisionTrees.end(); treeIt++)
errorSum += treeIt->getOutOfBagError();
outOfBagError = errorSum / (double)dtreeCount;
}
void DRandomForest::fit(const DData& data)
{
std::cout << "Fitting trees to \"" << data.getFileName() << "\", please wait.... ";
multiThreadFit(data, threadCount);
isTrained = true;
std::cout << "Done.\n";
std::cout << "Calculating out-of-bag error for forest... ";
calculateOutOfBagError();
std::cout << "Done.\n"
<< "Your random forest is now trained and ready.\n\n";
}
void DRandomForest::reset()
{
if (!isTrained)
return;
std::cout << "Resetting forest... ";
decisionTrees.clear();
isTrained = false;
std::cout << "Done.\n\n";
}
DValue DRandomForest::classify(const DSample& sample) const
{
if (!isTrained)
return DValue();
double currentClass, mostVotedClass;
unsigned int mostVotes = 0;
std::unordered_map<unsigned int, unsigned int> votes;
std::vector<DTree>::const_iterator treeIt = decisionTrees.begin();
for (treeIt; treeIt != decisionTrees.end(); treeIt++)
{
currentClass = treeIt->classify(sample).getNumericValue();
votes[(unsigned int)currentClass]++;
if (votes[(unsigned int)currentClass] > mostVotes)
{
mostVotes = votes[(unsigned int)currentClass];
mostVotedClass = currentClass;
}
}
return DValue(mostVotedClass);
//return std::max_element(votes.begin(), votes.end(), [](const auto& x, const auto& y) {return x.second < y.second; })->first;
}
bool DRandomForest::classifyBatch(const DData& testData) const
{
if (!isTrained)
return false;
std::cout << "Batch \"" << testData.getFileName() << "\" classification in progress... ";
std::string resultsFileName = "results " + std::to_string(time(0)) + ".txt";
std::ofstream resultsFile(resultsFileName);
resultsFile << "Forest total out-of-bag error: " << getOutOfBagError() << "%\n";
std::vector<DSample>::const_iterator testSamplesIt = testData.getSamples().begin();
for (testSamplesIt; testSamplesIt != testData.getSamples().end(); testSamplesIt++)
{
resultsFile << "prediction: " << classify(*testSamplesIt).getNumericValue() << "\t"
<< "actual: " << testSamplesIt->getTargetClassNumericValue() << "\n";
}
resultsFile.close();
std::cout << "Done.\n"
<< "Results saved inside \"" << resultsFileName << "\".\n\n";
return true;
}
double DRandomForest::getOutOfBagError() const
{
return outOfBagError;
}