-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNN_Array.cpp
203 lines (168 loc) · 5.47 KB
/
KNN_Array.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
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
#include <cmath>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <chrono>
#include <vector>
#define HAVE_STRUCT_TIMESPEC
#include <pthread.h>
using namespace std;
const int num_threads = 8;
const int best_record_each_thread = 5;
const int num_record_to_sort = num_threads * best_record_each_thread;
const int k_value = 3;
class Knn {
private:
int neighbours_number;
public:
Knn(int k) : neighbours_number(k) {}
int predict_class(double* dataset[], const double* target, int dataset_size, int feature_size) {
double* distances[3];
int zeros_count = 0;
int ones_count = 0;
// Allocate memory for distances and index order
distances[0] = new double[dataset_size];
distances[1] = new double[dataset_size];
distances[2] = new double[dataset_size];
get_knn(dataset, target, distances, dataset_size, feature_size);
//for (int i = 0; i < 100; i++) {
// cout << i + 1 << ") " << distances[0][i] << ", " << distances[1][i] << ", " << distances[2][i] << endl;
//}
selectionSort(distances, dataset_size);
/*for (int i = 0; i < num_record_to_sort; i++) {
cout << distances[0][i] << "," << distances[1][i] << "," << distances[2][i] << endl;
}*/
cout << "First K value: " << endl;
//Count label occurrences in the K nearest neighbors
int count = 0;
for (int i = 0; count < neighbours_number; i++) {
if (distances[1][i] == 0 && distances[0][i] > 0) {
zeros_count += 1;
cout << "0: " << distances[0][i] << endl;
//cout << "0: " << distances[0][i] << "," << distances[2][i] << endl;
count++;
}
else if (distances[1][i] == 1 && distances[0][i] > 0) {
ones_count += 1;
cout << "1: " << distances[0][i] << endl;
//cout << "1: " << distances[0][i] << "," << distances[2][i] << endl;
count++;
}
}
int prediction = (zeros_count > ones_count) ? 0 : 1;
// Clean up memory
delete[] distances[0];
delete[] distances[1];
delete[] distances[2];
return prediction;
}
private:
static void selectionSort(double** distances, int dataset_size) {
for (int i = 0; i < dataset_size - 1; i++) {
int min_index = i;
for (int j = i + 1; j < dataset_size; j++) {
if (distances[0][j] < distances[0][min_index]) {
min_index = j;
}
}
if (min_index != i) {
// Swap distances for all dimensions
for (int x = 0; x < 3; x++) {
double temp = distances[x][i];
distances[x][i] = distances[x][min_index];
distances[x][min_index] = temp;
}
}
}
}
double euclidean_distance(const double* x, const double* y, int feature_size) {
double l2 = 0.0;
for (int i = 1; i < feature_size; i++) {
l2 += pow((x[i] - y[i]), 2);
}
return sqrt(l2);
}
void get_knn(double* x[], const double* y, double* distances[3], int dataset_size, int feature_size) {
int count = 0;
for (int i = 0; i < dataset_size; i++) {
if (x[i] == y) continue; // do not use the same point
distances[0][count] = this->euclidean_distance(y, x[i], feature_size);
distances[1][count] = x[i][0]; // Store outcome label
distances[2][count] = i; // Store index
count++;
}
cout << "Number of euclidean run:" << count << endl;
}
};
std::vector<double> parseLine(const std::string& line) {
std::vector<double> row;
std::istringstream iss(line);
std::string value;
while (std::getline(iss, value, ',')) {
try {
double num = std::stod(value);
row.push_back(num);
}
catch (const std::invalid_argument&) {
std::cerr << "Invalid data in CSV: " << value << std::endl;
}
}
return row;
}
int main() {
std::string filename = "diabetes_binary.csv";
//const int dataset_size = 253681;
const int dataset_size = 53681;
const int feature_size = 22;
double** dataset = new double* [dataset_size];
//double target[feature_size] = { 0.0, 0.0, 0.0, 1.0, 24.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 3.0, 0.0, 0.0, 0.0, 2.0, 5.0, 3.0 };
double target[feature_size] = { 1.0, 1.0, 1.0, 1.0, 30.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 5.0, 30.0, 30.0, 1.0, 0.0, 9.0, 5.0, 1.0 };
// Allocate memory for dataset and target
for (int i = 0; i < dataset_size; i++) {
dataset[i] = new double[feature_size];
}
// Read data from CSV and populate dataset and target
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error opening file: " << filename << std::endl;
return 1;
}
std::string header;
std::getline(file, header);
std::string line;
int index = 0;
while (std::getline(file, line) && index < dataset_size) {
std::vector<double> row = parseLine(line);
for (int j = 0; j < feature_size; j++) {
dataset[index][j] = row[j];
}
index++;
}
std::cout << "Number of records: " << index << std::endl;
//Knn
#pragma region Knn
cout << "\nKNN: " << endl;
chrono::steady_clock::time_point knnBegin = chrono::steady_clock::now();
Knn knn(k_value); // Use K=3
int prediction = knn.predict_class(dataset, target, dataset_size, feature_size);
cout << "KNN Prediction: " << prediction << endl;
if (prediction == 0) {
cout << "Predicted class: Negative" << endl;
}
else if (prediction == 1) {
cout << "Predicted class: Prediabetes or Diabetes" << endl;
}
else {
cout << "Prediction could not be made." << endl;
}
chrono::steady_clock::time_point knnEnd = chrono::steady_clock::now();
cout << "Classification Time = " << chrono::duration_cast<chrono::microseconds>(knnEnd - knnBegin).count() << "[µs]" << endl;
#pragma endregion
// Deallocate memory for dataset
for (int i = 0; i < dataset_size; i++) {
delete[] dataset[i];
}
return 0;
}