forked from tjdolan121/TextClassifierLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForestClassifier.cpp
More file actions
219 lines (180 loc) · 5.43 KB
/
Copy pathRandomForestClassifier.cpp
File metadata and controls
219 lines (180 loc) · 5.43 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*++
Revision History:
Date: Jun 28, 2024.
Author: Rajas Chavadekar.
Desc: Created.
--*/
#include "RandomForestClassifier.h"
#include <fstream>
#include <iostream>
#include <algorithm>
RandomForestClassifier::RandomForestClassifier(BaseVectorizer* pvec)
: num_trees(num_trees), max_depth(max_depth)
{
pVec = pvec;
}
RandomForestClassifier::~RandomForestClassifier()
{
delete pVec;
}
void RandomForestClassifier::setHyperparameters(std::string hyperparameters)
{
std::string token;
std::istringstream tokenStream(hyperparameters);
// "num_trees=50,max_depth=5"
num_trees = 50;
max_depth = 5;
pVec->ngrams = 1;
while (std::getline(tokenStream, token, ',')) {
std::istringstream pairStream(token);
std::string key;
double value;
if (std::getline(pairStream, key, '=') && pairStream >> value) {
cout << key << " = " << value << endl;
if (key == "ngrams") {
pVec->ngrams = value;
}
else if (key == "minfrequency") {
minfrequency = value;
}
else if (key == "num_trees") {
num_trees = value;
}
else if (key == "max_depth") {
max_depth = value;
}
}
}
}
void RandomForestClassifier::fit(std::string abs_filepath_to_features, std::string abs_filepath_to_labels)
{
if (minfrequency > 0)
{
pVec->scanForSparseHistogram(abs_filepath_to_features, minfrequency);
}
pVec->fit(abs_filepath_to_features, abs_filepath_to_labels);
std::vector<std::shared_ptr<Sentence>> sentences = pVec->sentences;
for (int i = 0; i < num_trees; ++i)
{
auto tree = std::make_shared<DecisionTree>(max_depth);
tree->fit(sentences);
trees.push_back(tree);
}
}
Prediction RandomForestClassifier::predict(std::string sentence, bool preprocess)
{
GlobalData vars;
Prediction result;
std::vector<std::string> processed_input = pVec->buildSentenceVector(sentence, preprocess);
std::vector<double> feature_vector = pVec->getSentenceFeatures(processed_input);
std::vector<int> votes(3, 0); // Assuming 3 classes: POS, NEG, NEU
for (const auto& tree : trees)
{
int prediction = tree->predict(feature_vector).label;
votes[prediction]++;
}
std::vector<double> probabilities(3, 0.0);
for (ml_size_t i = 0; i < votes.size(); ++i)
{
probabilities[i] = static_cast<double>(votes[i]) / trees.size();
}
int max_index = std::distance(votes.begin(), std::max_element(votes.begin(), votes.end()));
result.probability = probabilities[1];
switch (max_index)
{
case 0:
result.label = vars.NEG;
break;
case 1:
result.label = vars.POS;
break;
case 2:
result.label = vars.NEU;
break;
default:
break;
}
return result;
}
void RandomForestClassifier::predict(std::string abs_filepath_to_features, std::string abs_filepath_to_labels, bool preprocess)
{
std::ifstream in(abs_filepath_to_features);
std::ofstream out(abs_filepath_to_labels);
std::string feature_input;
if (!in)
{
std::cerr << "ERROR: Cannot open features file.\n";
return;
}
if (!out)
{
std::cerr << "ERROR: Cannot open labels file.\n";
return;
}
#ifdef BENCHMARK
double sumduration = 0.0;
double sumstrlen = 0.0;
ml_size_t num_rows = 0;
#endif
while (getline(in, feature_input))
{
#ifdef BENCHMARK
auto start = std::chrono::high_resolution_clock::now();
#endif
Prediction result = predict(feature_input, preprocess);
#ifdef BENCHMARK
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration = end - start;
double milliseconds = duration.count();
sumduration += milliseconds;
sumstrlen += feature_input.length();
num_rows++;
#endif
out << result.label << "," << result.probability << std::endl;
}
#ifdef BENCHMARK
double avgduration = sumduration / num_rows;
cout << "Average Time per Text = " << avgduration << " ms" << endl;
double avgstrlen = sumstrlen / num_rows;
cout << "Average Length of Text (chars) = " << avgstrlen << endl;
#endif
in.close();
out.close();
}
void RandomForestClassifier::save(const std::string& filename) const
{
std::ofstream outFile(filename, std::ios::binary);
if (!outFile.is_open())
{
std::cerr << "Failed to open file for writing." << std::endl;
return;
}
pVec->save(outFile);
ml_size_t num_trees = trees.size();
outFile.write(reinterpret_cast<const char*>(&num_trees), sizeof(num_trees));
for (const auto& tree : trees)
{
tree->save(outFile);
}
outFile.close();
}
void RandomForestClassifier::load(const std::string& filename)
{
std::ifstream inFile(filename, std::ios::binary);
if (!inFile.is_open())
{
std::cerr << "Failed to open file for reading." << std::endl;
return;
}
pVec->load(inFile);
ml_size_t num_trees;
inFile.read(reinterpret_cast<char*>(&num_trees), sizeof(num_trees));
trees.resize(num_trees);
for (ml_size_t i = 0; i < num_trees; ++i)
{
auto tree = std::make_shared<DecisionTree>();
tree->load(inFile);
trees[i] = tree;
}
inFile.close();
}