forked from tjdolan121/TextClassifierLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNNClassifier.cpp
More file actions
236 lines (193 loc) · 6.7 KB
/
Copy pathKNNClassifier.cpp
File metadata and controls
236 lines (193 loc) · 6.7 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*++
Revision History:
Date: Jun 28, 2024.
Author: Rajas Chavadekar.
Desc: Created.
--*/
#include "KNNClassifier.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cmath>
KNNClassifier::KNNClassifier(BaseVectorizer* pvec)
{
pVec = pvec;
}
KNNClassifier::~KNNClassifier()
{
delete pVec;
}
void KNNClassifier::setHyperparameters(std::string hyperparameters)
{
std::string token;
std::istringstream tokenStream(hyperparameters);
// "k=3"
k = 3;
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 == "k") {
k = value;
}
}
}
}
void KNNClassifier::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);
ml_size_t num_features = pVec->word_array.size();
std::vector<std::shared_ptr<Sentence>> sentences = pVec->sentences;
training_features.clear();
training_labels.clear();
std::ifstream label_file(abs_filepath_to_labels);
std::string label;
for (ml_size_t i = 0; i < sentences.size(); ++i)
{
std::vector<double> features;
const auto& sentence_map = sentences[i]->sentence_map;
features = pVec->getFrequencies(sentence_map);
training_features.push_back(features);
label_file >> label;
training_labels.push_back(std::stoi(label));
}
label_file.close();
kd_tree.build(training_features, training_labels);
}
void KNNClassifier::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();
}
Prediction KNNClassifier::predict(std::string sentence, bool preprocess)
{
GlobalData vars;
std::vector<std::string> processed_input = pVec->buildSentenceVector(sentence, preprocess);
std::vector<double> feature_vector = pVec->getSentenceFeatures(processed_input);
int label = kd_tree.nearestNeighbor(feature_vector);
// Calculate probability
double total_distance = 0.0;
std::vector<double> closest_distances = kd_tree.getClosestDistances(feature_vector, k);
for (double dist : closest_distances)
{
total_distance += dist;
}
double probability = 1.0 - (total_distance / closest_distances.size());
return { label, probability };
}
int KNNClassifier::getLabel(const std::vector<double>& features) const
{
// Deprecated: getLabel is no longer needed with KDTree nearestNeighbor.
return 0;
}
double KNNClassifier::calculateDistance(const std::vector<double>& a, const std::vector<double>& b) const
{
double sum = 0.0;
for (ml_size_t i = 0; i < a.size(); ++i)
{
double diff = a[i] - b[i];
sum += diff * diff;
}
return std::sqrt(sum);
}
void KNNClassifier::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 training_features_size = training_features.size();
outFile.write(reinterpret_cast<const char*>(&training_features_size), sizeof(training_features_size));
for (const auto& features : training_features)
{
ml_size_t features_size = features.size();
outFile.write(reinterpret_cast<const char*>(&features_size), sizeof(features_size));
outFile.write(reinterpret_cast<const char*>(features.data()), features_size * sizeof(int));
}
ml_size_t training_labels_size = training_labels.size();
outFile.write(reinterpret_cast<const char*>(&training_labels_size), sizeof(training_labels_size));
outFile.write(reinterpret_cast<const char*>(training_labels.data()), training_labels_size * sizeof(int));
outFile.close();
}
void KNNClassifier::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 training_features_size;
inFile.read(reinterpret_cast<char*>(&training_features_size), sizeof(training_features_size));
training_features.resize(training_features_size);
for (auto& features : training_features)
{
ml_size_t features_size;
inFile.read(reinterpret_cast<char*>(&features_size), sizeof(features_size));
features.resize(features_size);
inFile.read(reinterpret_cast<char*>(features.data()), features_size * sizeof(int));
}
ml_size_t training_labels_size;
inFile.read(reinterpret_cast<char*>(&training_labels_size), sizeof(training_labels_size));
training_labels.resize(training_labels_size);
inFile.read(reinterpret_cast<char*>(training_labels.data()), training_labels_size * sizeof(int));
inFile.close();
kd_tree.build(training_features, training_labels);
}