-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk_nearest.c
More file actions
165 lines (144 loc) · 4.9 KB
/
k_nearest.c
File metadata and controls
165 lines (144 loc) · 4.9 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <math.h>
#include <sys/time.h>
#include "simpletimer.h"
#include "parse.h"
#include "vec.h"
data_t features[ROWS][FEATURE_LENGTH] __attribute__((aligned(32)));
data_t timer_ref_MD,timer_ref_ED,timer_ref_CS;
data_t timer_opt_MD,timer_opt_ED,timer_opt_CS;
data_t abs_diff(data_t x, data_t y){
data_t diff = x-y;
return fabs(diff);
}
data_t mult(data_t x,data_t y){
data_t m = x*y;
return m;
}
data_t manhattan_distance(data_t *x, data_t *y, int length){
data_t distance=0;
int i =0;
for(i=0;i<length;i++){
distance+=abs_diff(x[i],y[i]);
}
return distance;
}
data_t squared_eucledean_distance(data_t *x,data_t *y, int length){
data_t distance=0;
int i = 0;
for(i=0;i<length;i++){
distance+= mult(abs_diff(x[i],y[i]),abs_diff(x[i],y[i]));
}
return distance;
}
data_t norm(data_t *x, int length){
data_t n = 0;
int i=0;
for (i=0;i<length;i++){
n += mult(x[i],x[i]);
}
n = sqrt(n);
return n;
}
data_t cosine_similarity(data_t *x, data_t *y, int length){
data_t sim=0;
int i=0;
for(i=0;i<length;i++){
sim += mult(x[i],y[i]);
}
sim = sim / mult(norm(x,FEATURE_LENGTH),norm(y,FEATURE_LENGTH));
return sim;
}
data_t *ref_classify_MD(unsigned int lookFor, unsigned int *found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i,closest_point=0;
data_t min_distance,current_distance;
timer_start(&stv);
min_distance = manhattan_distance(features[lookFor],features[0],FEATURE_LENGTH);
result[0] = min_distance;
for(i=1;i<ROWS-1;i++){
current_distance = manhattan_distance(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance<min_distance){
min_distance=current_distance;
closest_point=i;
}
}
timer_ref_MD = timer_end(stv);
printf("Calculation using reference MD took: %10.6f \n", timer_ref_MD);
*found=closest_point;
return result;
}
data_t *ref_classify_ED(unsigned int lookFor, unsigned int *found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i,closest_point=0;
data_t min_distance,current_distance;
timer_start(&stv);
min_distance = squared_eucledean_distance(features[lookFor],features[0],FEATURE_LENGTH);
result[0] = min_distance;
for(i=1;i<ROWS-1;i++){
current_distance = squared_eucledean_distance(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance<min_distance){
min_distance=current_distance;
closest_point=i;
}
}
timer_ref_ED = timer_end(stv);
printf("Calculation using reference ED took: %10.6f \n", timer_ref_ED);
*found = closest_point;
return result;
}
//Don't touch this function
data_t *ref_classify_CS(unsigned int lookFor, unsigned int* found) {
data_t *result =(data_t*)malloc(sizeof(data_t)*(ROWS-1));
struct timeval stv, etv;
int i,closest_point=0;
data_t min_distance,current_distance;
timer_start(&stv);
min_distance = cosine_similarity(features[lookFor],features[0],FEATURE_LENGTH);
result[0] = min_distance;
for(i=1;i<ROWS-1;i++){
current_distance = cosine_similarity(features[lookFor],features[i],FEATURE_LENGTH);
result[i]=current_distance;
if(current_distance>min_distance){
min_distance=current_distance;
closest_point=i;
}
}
timer_ref_CS = timer_end(stv);
printf("Calculation using reference CS took: %10.6f \n", timer_ref_CS);
*found = closest_point;
return result;
}
int main(int argc, char **argv){
char* dataset_name=DATASET;
int i,j;
struct timeval stv, etv;
unsigned int lookFor=ROWS-1, located;
//PARSE CSV
//holds the information regarding author and title
char metadata[ROWS][2][20];
timer_start(&stv);
parse_csv(dataset_name, features, metadata);
printf("Parsing took %9.6f s \n\n", timer_end(stv));
printf("Classifying using MD:");
printf("<Record %d, author =\"%s\", title=\"%s\">\n",lookFor,metadata[lookFor][0],metadata[lookFor][1]);
ref_classify_MD(lookFor, &located);
printf("Best match: ");
printf("<Record %d, author =\"%s\", title=\"%s\">\n\n",located,metadata[located][0],metadata[located][1]);
printf("Classifying using ED:");
printf("<Record %d, author =\"%s\", title=\"%s\">\n",lookFor,metadata[lookFor][0],metadata[lookFor][1]);
ref_classify_ED(lookFor, &located);
printf("Best match: ");
printf("<Record %d, author =\"%s\", title=\"%s\">\n\n",located,metadata[located][0],metadata[located][1]);
printf("Classifying using CS (cosine similarity):");
printf("<Record %d, author =\"%s\", title=\"%s\">\n",lookFor,metadata[lookFor][0],metadata[lookFor][1]);
ref_classify_CS(lookFor, &located);
printf("Best match: ");
printf("<Record %d, author =\"%s\", title=\"%s\">\n\n",located,metadata[located][0],metadata[located][1]);
}