-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
434 lines (368 loc) · 14.2 KB
/
Copy pathmain.cpp
File metadata and controls
434 lines (368 loc) · 14.2 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cfloat>
#include <climits>
#include <chrono>
class DATA {
public:
std::vector<int> originalClass;
std::vector<std::vector<double>> row;
int features() const {
return features_d;
};
int size() const {
return size_d;
};
void init() {
size_d = originalClass.size();
features_d = row.at(0).size();
};
DATA() {};
~DATA() {};
DATA(const DATA& other) = delete;
DATA& operator=(const DATA& other) {
if (this != &other) {
originalClass = other.originalClass;
row = other.row;
size_d = other.size_d;
features_d = other.features_d;
};
return *this;
};
private:
int size_d;
int features_d;
};
double leave_one_out_cross_validation(const DATA& data, std::vector<int>& features, int feature_to_add);
void feature_search_demo(DATA& data);
void read(const std::string& filename, DATA& data);
void backwardsElimination(DATA& data);
double backward_leave_one_out_cross_validation(const DATA& data, std::vector<int>& features, int feature_to_remove);
int main() {
DATA data;
std::string filename;
std::cout << "Hello! This is Ryan Hoang's Feature Selection Algorithm.\n" <<
"Type in the name of the file (with extension) you want to test: ";
std::cin >> filename;
if (filename == "で") {
filename = "CS170_Large_DataSet__28.txt";
}
else if (filename == "ち") {
filename = "CS170_Small_DataSet__15.txt";
};
std::cin.clear();
std::cin.ignore();
std::cout << "\n" << "Type the number of the algorithm you want to run:\n"
<< "1. Forward Selection\n"
<< "2. Backward Selection\n";
int option = INT_MAX;
do {
if (option != INT_MAX) {
std::cout << "Invalid Option\n";
};
std::cin >> option;
std::cin.clear();
std::cin.ignore();
} while (option != 1 && option != 2);
read(filename, data);
data.init();
std::cout << "This dataset has " << data.features() << " features (not including the class attribute), with " << data.size() << " instances.\n"
<< "Running nearest neighbor with all " << data.features() << " features, using \"leaving-one-out\" evaluation.\n";
std::cout << "\n====Beginning Search====\n";
auto start = std::chrono::steady_clock::now();
switch (option) {
case 1:
feature_search_demo(data);
break;
case 2:
backwardsElimination(data);
break;
default:
feature_search_demo(data);
break;
};
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
std::cout << "Took " << (double)(duration.count()) << " seconds to finish search\n";
return 0;
};
/* Read Function
https://youtu.be/Rc61KnQArGI
*/
void read(const std::string& filename, DATA& data) {
std::ifstream openedFile;
openedFile.open(filename);
if (!openedFile) {
std::cout << "Failed to open file\n";
return;
};
std::string currLine;
while (std::getline(openedFile, currLine)) {
std::istringstream ss(currLine);
double value;
ss >> value;
data.originalClass.push_back(value);
std::vector<double> entry;
while(ss >> value) {
entry.push_back(value);
};
data.row.push_back(entry);
};
openedFile.close();
};
void modifyData(const DATA& data, DATA& newData, const std::vector<int>& features, int feature_to_add) {
newData = data;
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data.features(); j++) {
bool is_a_feature = false;
for (int k = 0; k < features.size(); k++) {
if (j == features.at(k)) {
is_a_feature = true;
};
};
if (j == feature_to_add) {
is_a_feature = true;
};
if (!is_a_feature) {
newData.row.at(i).at(j) = 0;
};
};
};
};
void modifyData_remove(const DATA& data, DATA& newData, const std::vector<int>& features, int feature_to_remove) {
newData = data;
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data.features(); j++) {
bool to_remove = true;
for (int k = 0; k < features.size(); k++) {
if (j == features.at(k)) {
to_remove = false;
};
};
if (j == feature_to_remove) {
to_remove = true;
};
if (to_remove) {
newData.row.at(i).at(j) = 0;
};
};
};
};
// From Slides
double leave_one_out_cross_validation(const DATA& data, std::vector<int>& features, int feature_to_add) {
DATA newData;
modifyData(data, newData, features, feature_to_add);
int number_correctly_classified = 0;
for (int i = 0; i < newData.size(); i++) {
const std::vector<double>& object_to_classify = newData.row.at(i);
int label_object_to_classify = newData.originalClass.at(i);
double nearest_neighbor_distance = DBL_MAX;
int nearest_neighbor_location = INT_MAX;
int nearest_neighbor_label = 0; // 1 or 2
for (int k = 0 ; k < newData.size(); k++) {
if (k != i) {
double num = 0;
for (int a = 0; a < newData.features(); a++) {
double sum = (object_to_classify.at(a) - newData.row.at(k).at(a));
num += pow(sum, 2.0);
};
double distance = sqrt(num);
if (distance < nearest_neighbor_distance) {
nearest_neighbor_distance = distance;
nearest_neighbor_location = k;
nearest_neighbor_label = newData.originalClass.at(nearest_neighbor_location);
};
};
};
if (label_object_to_classify == nearest_neighbor_label) {
number_correctly_classified++;
};
};
double accuracy = (double)number_correctly_classified / data.size();
return accuracy;
};
// From Slides
bool isempty(const std::vector<int>& overlap) {
return overlap.empty();
};
std::vector<int> intersect(const std::vector<int>& features, int k) {
std::vector<int> results;
for (int i = 0; i < features.size(); i++) {
if (k == features.at(i)) {
results.push_back(features.at(i));
};
};
return results;
};
void feature_search_demo(DATA& data) {
std::vector<int> current_set_of_features;
std::vector<int> best_set_of_features;
double best_accuracy = 0;
for (int i = 0; i < data.features(); i++) {
// std::cout << "On the " << (i + 1) << "th level of the search tree\n";
int feature_to_add_at_this_level = -1;
double best_so_far_accuracy = 0;
for (int k = 0; k < data.features(); k++) {
if (isempty(intersect(current_set_of_features, k))) {
// std::cout << "--Considering adding the " << (k + 1) << " feature\n";
double accuracy = leave_one_out_cross_validation(data, current_set_of_features, k);
std:: cout << "\tUsing feature(s) {";
for (int a = 0; a < current_set_of_features.size(); a++) {
if (a == 0) {
std::cout << (current_set_of_features.at(a) + 1);
}
else {
std::cout << ", " << (current_set_of_features.at(a) + 1);
};
};
if (current_set_of_features.size() > 0) {
std::cout << ", " << (k + 1);
}
else {
std::cout << (k + 1);
};
std::cout << "} accuracy is " << (accuracy * 100) << "%\n";
if (accuracy > best_so_far_accuracy) {
best_so_far_accuracy = accuracy;
feature_to_add_at_this_level = k;
};
};
};
if (feature_to_add_at_this_level != -1) {
current_set_of_features.push_back(feature_to_add_at_this_level);
std::cout << "Feature set {";
for (int i = 0; i < current_set_of_features.size(); i++) {
if (i == 0) {
std::cout << (current_set_of_features.at(i) + 1);
}
else {
std::cout << ", " << (current_set_of_features.at(i) + 1);
};
};
std::cout << "} was best, accuracy is " << (best_so_far_accuracy * 100) << "%\n";
};
if (best_so_far_accuracy > best_accuracy) {
best_accuracy = best_so_far_accuracy;
best_set_of_features = current_set_of_features;
};
};
std::cout << "\nFinished Search. Best Feature Subset is {";
for (int a = 0; a < best_set_of_features.size(); a++) {
if (a == 0) {
std::cout << (best_set_of_features.at(a) + 1);
}
else {
std::cout << ", " << (best_set_of_features.at(a) + 1);
};
};
std::cout << "}, which had an accuracy of " << (best_accuracy * 100) << "%\n";
};
void backwardsElimination(DATA& data) {
std::vector<int> current_set_of_features;
for (int i = 0; i < data.features(); i++) {
current_set_of_features.push_back(i);
};
std::vector<int> best_set_of_features;
double best_accuracy = 0;
for (int i = 0; i < data.features(); i++) {
// std::cout << "On the " << (i + 1) << "th level of the search tree\n";
int feature_to_remove_at_this_level = -1;
double best_so_far_accuracy = 0;
for (int k = 0; k < data.features(); k++) {
if (!isempty(intersect(current_set_of_features, k))) {
// std::cout << "--Considering removing the " << (k + 1) << " feature\n";
double accuracy = backward_leave_one_out_cross_validation(data, current_set_of_features, k);
std:: cout << "\tUsing feature(s) {";
bool first = true;
for (int a = 0; a < current_set_of_features.size(); a++) {
if (current_set_of_features.at(a) != k) {
if (first) {
std::cout << (current_set_of_features.at(a) + 1);
first = false;
}
else {
std::cout << ", " << (current_set_of_features.at(a) + 1);
};
};
};
std::cout << "} accuracy is " << (accuracy * 100) << "%\n";
if (accuracy > best_so_far_accuracy) {
best_so_far_accuracy = accuracy;
feature_to_remove_at_this_level = k;
};
};
};
if (feature_to_remove_at_this_level != -1) {
int eraseIndex;
for (int a = 0; a < current_set_of_features.size(); a++) {
if (current_set_of_features.at(a) == feature_to_remove_at_this_level) {
eraseIndex = a;
};
};
current_set_of_features.erase(current_set_of_features.begin() + eraseIndex);
std::cout << "Feature set {";
for (int i = 0; i < current_set_of_features.size(); i++) {
if (i == 0) {
std::cout << (current_set_of_features.at(i) + 1);
}
else {
std::cout << ", " << (current_set_of_features.at(i) + 1);
};
};
std::cout << "} was best, accuracy is " << (best_so_far_accuracy * 100) << "%\n";
};
// else {
// // std::cout << "On level " << (i + 1) << " i didn't remove a feature\n";
// };
if (best_so_far_accuracy > best_accuracy) {
best_accuracy = best_so_far_accuracy;
best_set_of_features = current_set_of_features;
};
};
std::cout << "Finished Search. Best Feature Subset is {";
for (int a = 0; a < best_set_of_features.size(); a++) {
if (a == 0) {
std::cout << (best_set_of_features.at(a) + 1);
}
else {
std::cout << ", " << (best_set_of_features.at(a) + 1);
};
};
std::cout << "}, which had an accuracy of " << (best_accuracy * 100) << "%\n";
};
double backward_leave_one_out_cross_validation(const DATA& data, std::vector<int>& features, int feature_to_remove) {
DATA newData;
modifyData_remove(data, newData, features, feature_to_remove);
int number_correctly_classified = 0;
for (int i = 0; i < newData.size(); i++) {
const std::vector<double>& object_to_classify = newData.row.at(i);
int label_object_to_classify = newData.originalClass.at(i);
double nearest_neighbor_distance = DBL_MAX;
int nearest_neighbor_location = INT_MAX;
int nearest_neighbor_label = 0; // 1 or 2
for (int k = 0 ; k < newData.size(); k++) {
if (k != i) {
double num = 0;
for (int a = 0; a < newData.features(); a++) {
double sum = (object_to_classify.at(a) - newData.row.at(k).at(a));
num += pow(sum, 2.0);
};
double distance = sqrt(num);
if (distance < nearest_neighbor_distance) {
nearest_neighbor_distance = distance;
nearest_neighbor_location = k;
nearest_neighbor_label = newData.originalClass.at(nearest_neighbor_location);
};
};
};
if (label_object_to_classify == nearest_neighbor_label) {
number_correctly_classified++;
};
};
double accuracy = (double)number_correctly_classified / data.size();
return accuracy;
};