-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
53 lines (47 loc) · 1.62 KB
/
utils.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "utils.h"
Dataset readDataset(char *filename, SplitType split) {
if (split == TRAIN) {
printf("Loading training dataset from file '%s'\n", filename);
} else if (split == TEST) {
printf("Loading testing dataset from file '%s'\n", filename);
} else{
fprintf(stderr, "Invalid split type\n");
exit(1);
}
FILE *file = fopen(filename, "r");
if (file == NULL) {
// perror("Error opening file");
fprintf(stderr, "Error opening file '%s' -- %s\n", filename, strerror(errno));
exit(1);
}
Dataset dataset;
// Specify the split type (i.e, TRAIN or TEST)
dataset.split = split;
// Read the number of instances and features
fscanf(file, "%d", &dataset.instances);
fscanf(file, "%d", &dataset.features);
// Allocate memory for the input and output arrays
dataset.input = (int **)malloc(dataset.instances * sizeof(int *));
dataset.output = (int *)malloc(dataset.instances * sizeof(int));
for (int i = 0; i < dataset.instances; i++) {
dataset.input[i] = (int *)malloc(dataset.features * sizeof(int));
for (int j = 0; j < dataset.features; j++) {
fscanf(file, "%d", &dataset.input[i][j]);
}
// Add label of the instance to the output array
fscanf(file, "%d", &dataset.output[i]);
}
fclose(file);
return dataset;
}
void freeDataset(Dataset dataset) {
for (int i = 0; i < dataset.instances; i++) {
free(dataset.input[i]);
}
free(dataset.input);
free(dataset.output);
}