-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivide_dataset.c
122 lines (102 loc) · 2.7 KB
/
divide_dataset.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
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 <stdlib.h>
#include "utils.h"
typedef struct divideResult
{
double **first;
size_t firstRows;
double **second;
size_t secondRows;
} divideResult;
double findSplitRow(double **X, int rows, int cols, size_t iFeauture, int threshold)
{
double nSplit;
for (int i = 0; i < rows; i++)
{
if (X[i][iFeauture] == threshold)
{
nSplit = i;
return nSplit;
};
}
return 0;
}
double **createSplit(double **X, int subsetSize, int start, int end)
{
// Allocate memory for subset
double **subset = (double **)malloc(subsetSize * sizeof(double *));
size_t index = 0;
for (int i = start; i < end; i++)
{
subset[index] = X[i];
index += 1;
};
return subset;
}
divideResult divideDataset(double **X, int rows, int cols, size_t iFeauture, int threshold)
{
// Find number of row to split
int nSplit = findSplitRow(X, rows, cols, iFeauture, threshold);
// Split data
double **first = createSplit(X, nSplit, 0, nSplit);
double **second = createSplit(X, rows - nSplit, nSplit, rows);
// Fill divideResult
divideResult result;
result.first = first;
result.firstRows = nSplit;
result.second = second;
result.secondRows = rows - nSplit;
return result;
}
void freeDivideResult(divideResult *result)
{
if (result == NULL)
{
return;
}
// Free the first matrix
if (result->first != NULL)
{
free(result->first);
result->first = NULL;
}
// Free the second matrix
if (result->second != NULL)
{
free(result->second);
result->second = NULL;
}
// Reset the row counts
result->firstRows = 0;
result->secondRows = 0;
}
int main()
{
// Init variavles
size_t iFeauture = 0;
int threshold = 5;
// Init X
size_t rows = 5, cols = 2;
double init_X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double **X = allocateMatrix(rows, cols);
// Fill x
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
X[i][j] = init_X[i * cols + j];
};
};
printf("Matrix X with %d rows and %d cols.\n", rows, cols);
printMatrix(X, rows, cols, 0);
// Divide dataset
divideResult result = divideDataset(X, rows, cols, iFeauture, threshold);
// Print result
printf("Matrix first with %d rows and %d cols.\n", result.firstRows, cols);
printMatrix(result.first, result.firstRows, cols, 0);
printf("Matrix second with %d rows and %d cols.\n", result.secondRows, cols);
printMatrix(result.second, result.secondRows, cols, 0);
// Free memory
freeMatrix(X, rows);
freeDivideResult(&result);
}