-
Notifications
You must be signed in to change notification settings - Fork 2
/
knn_input_generator.c
85 lines (73 loc) · 2.67 KB
/
knn_input_generator.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
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<string.h>
#define POINTS_MIN -100
#define POINTS_MAX 200
void generateRandomCoords(float* x, int spacedim, int numels);
void init(float* coords, float* coordsnew, int* classes, int spacedim, int numels, int classes_num, int newels);
void writeInput(float* coords, float* coordsnew, int* classes, int spacedim, int classes_num, int numels, int newels);
int main(int argc, char** argv)
{
if (argc < 5)
{
perror("use: knn-generator <number-of-samples> <number-of-new-points> <number-of-classes> <space-dimension>");
exit(1);
}
srand(time(NULL));
int numels = atoi(argv[1]); //number of existing points
int newels = atoi(argv[2]); //number of points we want classify
int classes_num = atoi(argv[3]); //number of classes
int spacedim = atoi(argv[4]); //dimension of space
float* coords; //coords of existing points with a class
float* coordsnew; //coords of points we want classify
int* classes; //array with a class foreach points
int totalElements = numels+newels;
//*** allocation ***
coords = malloc(sizeof(float)*totalElements*spacedim);
coordsnew = malloc(sizeof(float)*newels*spacedim);
classes = malloc(sizeof(int)*(totalElements));
//*** end-allocation ***
init(coords, coordsnew, classes, spacedim, numels, classes_num, newels);
writeInput(coords, coordsnew, classes, spacedim, classes_num, numels, newels);
return 0;
}
void init(float* coords, float* coordsnew, int* classes, int spacedim, int numels, int classes_num, int newels)
{
int i;
for (i = 0; i < numels; i++)
classes[i] = rand()%classes_num;
generateRandomCoords(coords, spacedim, numels);
generateRandomCoords(coordsnew, spacedim, newels);
}
void generateRandomCoords(float* x, int spacedim, int numels)
{
int i;
for (i = 0; i < numels; i++)
{
int j;
for (j = 0; j < spacedim; j++)
x[i*spacedim+j] = (float)rand()/(float)(RAND_MAX/POINTS_MAX) + (float)(POINTS_MIN);
}
}
void writeInput(float* coords, float* coordsnew, int* classes, int spacedim, int classes_num, int numels, int newels)
{
FILE *fp;
fp = fopen("data", "w");
int i, j;
fprintf(fp, "%d,%d,%d,%d\n", numels, newels, classes_num, spacedim);
for(i=0; i<numels; i++)
{
for (j = 0; j < spacedim; j++)
fprintf(fp, "%lf,", coords[i*spacedim+j]);
fprintf(fp, "%d\n", classes[i]);
}
for(i = 0; i < newels; i++)
{
for (j = 0; j < spacedim; j++)
fprintf(fp, "%lf,", coordsnew[i*spacedim+j]);
fprintf(fp, "-1\n");
}
fclose(fp);
}