-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.cpp
More file actions
122 lines (96 loc) · 3.01 KB
/
Copy pathtemplate.cpp
File metadata and controls
122 lines (96 loc) · 3.01 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
#include<stdio.h>
#include <math.h>
#include <vector>
#define PI acos(-1.0)
#define LOOK_DST 50
#define KERNAL_BANDWITH 100
//YOUR CODE
double euclid_distance(int x, int y){
return sqrt((x-y)*(x-y));
}
double gaussian_kernel(double distance, double bandwidth){
return (1/(bandwidth*sqrt(2*PI)))*exp(-0.5*((distance/bandwidth)*(distance/bandwidth)));
}
std::vector<int> neighbourhood_points(int X[], int x_centeroid, int x_len, double distance){
std::vector<int> neighbours;
for (int i =0; i < x_len; i++){
double dst = euclid_distance(X[i], x_centeroid);
if(dst <= distance)neighbours.push_back(X[i]);
}
return neighbours;
}
int* initializeKMeans(int data[], int data_len, int k){
const int size = 3;
int min,max;
for (size_t i = 0; i < data_len; i++)
{
if(!min || data[i] < min)min = data[i];
if(!max || data[i] > max)max = data[i];
}
int means[size];
for (size_t i = 0; i < k; i++)
{
means[i] = min + rand() % max;
}
return means;
}
int updateMean(int n, int mean, int neighbour){
return (mean*(n-1)+neighbour)/float(n);
}
int classify(int means[], int k, int neighbour){
int min;
int index = -1;
for (size_t i = 0; i < k; i++)
{
int distance = euclid_distance(neighbour, means[i]);
if(distance < min || !min){
min = distance;dygbh
index = i;
}
}
return index;
}
//input:
// int[] data : array of data including previous data so far, with length of num_prev_data.
// num_prev_data : number of data above
int predict(int* data, int num_prev_data){
//your new data should be data[num_prev_data-1];
int value = data[num_prev_data-1];
//NEW///////////////////////////////////////////////////////////
int* means = initializeKMeans(data, num_prev_data,3);
std::vector<int> clusterSizes[3];
int belongsTo[num_prev_data];
//NEW///////////////////////////////////////////////////////////
//output: 0 for black, 1 for gray and 2 for white.
if(value > 300)return 2;
if(value > 200)return 1;
else return 0;
}
//////////////////////////////////////////////////////////
//////////////////DO NOT MODIFY BELOW/////////////////////
//////////////////////////////////////////////////////////
int main(void){
float average_score = 0;
for(int f=1; f <= 4; f++){
char filename[10];
sprintf(filename, "data%d.txt", f);
FILE* fp = fopen(filename, "r");
int N;
fscanf(fp, "%d", &N);
int data[N];
int score = 0;
for(int i=0; i < N; i++){
int d, label;
fscanf(fp, "%d %d", &d, &label);
data[i] = d;
int pred = predict(data, i+1);
if(pred == label)score++;
}
printf("round %d: your score: %f\n",f, score / float(N));
average_score += score/float(N);
//score the prediction
fclose(fp);
}
average_score /= 4;
printf("average score: %f\n", average_score);
}