-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn.c
100 lines (81 loc) · 2.5 KB
/
knn.c
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
/* Mertcan OZKAN ~ knn.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "csv_read.h"
#include "knn.h"
//d(a,b) = sqrt( (a1-b1)^2 + (a2-b2)^2 + (a3-b3)^2 + (a4-b4)^2 )
float nearest_neighbors(vector_t *data){
int comparisons = 3;
// printf("\nEnter the desired number of comparisons to nearby neighbors: ");
// scanf("%d", &comparisons);
int i, j, maximum = 0;
int desicion_counter[possible_output_number];
int data_estimate;
for(i = 0; i < comparisons; i++){
// printf("Neighbor #%d\tClass: %s\tVector Length %lf\n", i+1, flower[i].type, flower[i].vectorLength/1000);
for(j = 0; j< possible_output_number; j++) {
if(data[i].type == j){
desicion_counter[j]++;
}
}
for(j = 0; j< possible_output_number; j++) {
if(desicion_counter[j] > maximum){
data_estimate = data[i].type;
maximum = desicion_counter[j];
}
}
}
if(data_estimate > possible_output_number) {
fprintf(stderr, "Nearest Neighbor cannot be found.\n");
return -1;
}else{
printf("Out of %d nearest neighbors, the most likely classification for the input flower is type %d.\n", i+1, data_estimate);
return data_estimate;
}
}
void *evaluate_distance(data_t *knn_data, data_t input_data, int array_elm){
vector_t *data = (vector_t *) calloc(TRAINING_DATA_SIZE + 1, sizeof(vector_t));
for(int i = 0; i < array_elm; i++){
float d_input[input_size];
int counter = 0;
while(counter < input_size){
d_input[counter] = (input_data.input[counter] - knn_data[i].input[counter]);
counter ++;
}
if(data == NULL){
printf("The given node cannot be NULL");
return 0;
}else{
data[i].type = knn_data[i].output;
float d_input_sum = 0;
for(counter = 0; counter < input_size; counter++)
d_input_sum += d_input[counter] * d_input[counter];
data[i].vectorLength = (sqrt(d_input_sum)*1000);
}
}
// void qsort(base, num, size, comp);
qsort(data, array_elm, sizeof(*data), sort_data);
if(input_data.output == nearest_neighbors(data)) {
accuracy_rate++;
}
}
data_t read_user_input(){
data_t user_data;
int comparisons;
user_data.input = malloc((input_size) * sizeof(float));
printf("Enter the requested information in order to classify a type.\n");
int counter = 0;
while(counter < input_size) {
printf("Enter the user input: ");
scanf("%f", &user_data.input[counter]);
counter++;
}
return user_data;
}
int sort_data(const void* x, const void* y){
int l = (int)((vector_t*)x)->vectorLength;
int r = (int)((vector_t*)y)->vectorLength;
return(l-r);
}