-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression.c
More file actions
262 lines (224 loc) · 8.06 KB
/
Copy pathLinearRegression.c
File metadata and controls
262 lines (224 loc) · 8.06 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "LinearRegression.h"
#include "float.h"
LinearRegression * init_model(Feature *feats, long predictor_count, bool has_intercept)
{
LinearRegression *model = malloc(sizeof(LinearRegression));
if (!model) {
fprintf(stderr, "Memory allocation error for LinearRegression\n");
exit(1);
}
model->has_intercept = has_intercept;
model->weight_count = predictor_count;
model->weights = malloc(sizeof(long double) * predictor_count);
model->weight_names = malloc(sizeof(char *) * predictor_count);
if (!model->weights || !model->weight_names) {
fprintf(stderr, "Memory allocation error for weights or weight_names\n");
exit(1);
}
// Initialize weights to zero.
for (long i = 0; i < predictor_count; i++) {
model->weights[i] = 0.0L;
}
if (has_intercept) {
// First weight name is "Intercept"
model->weight_names[0] = malloc(strlen("Intercept") + 1);
if (!model->weight_names[0]) {
fprintf(stderr, "Memory allocation error for intercept name\n");
exit(1);
}
strcpy(model->weight_names[0], "Intercept");
// Copy the remaining predictor names from feats.
for (long i = 1; i < predictor_count; i++) {
model->weight_names[i] = malloc(strlen(feats[i - 1].name) + 1);
if (!model->weight_names[i]) {
fprintf(stderr, "Memory allocation error for feature name %ld\n", i);
exit(1);
}
strcpy(model->weight_names[i], feats[i - 1].name);
}
} else {
// Without an intercept, copy predictor names directly.
for (long i = 0; i < predictor_count; i++) {
model->weight_names[i] = malloc(strlen(feats[i].name) + 1);
if (!model->weight_names[i]) {
fprintf(stderr, "Memory allocation error for feature name %ld\n", i);
exit(1);
}
strcpy(model->weight_names[i], feats[i].name);
}
}
return model;
}
void free_model(LinearRegression *model) {
if (model == NULL)
return;
// Free the weights array.
free(model->weights);
// Free each individual string in weight_names.
if (model->weight_names) {
for (long i = 0; i < model->weight_count; i++) {
free(model->weight_names[i]);
}
free(model->weight_names);
}
// Finally, free the model structure itself.
free(model);
}
LinearRegression * train_model(Feature *feats, Output * output, long feat_count, bool has_intercept)
{
long datapoints = feats[0].data.size;
// Total number of weights = predictor features plus intercept (if applicable).
long predictor_count = feat_count + (has_intercept ? 1 : 0);
// Initialize model using the correct number of weights.
LinearRegression *model = init_model(feats, predictor_count, has_intercept);
/* --- Build design matrix X --- */
// X has "datapoints" rows and "predictor_count" columns.
Matrix X = empty_matr(datapoints, predictor_count);
for (long i = 0; i < datapoints; i++) {
long colIndex = 0;
if (has_intercept) {
// First column is intercept (all ones)
mat(X)[i][0] = 1.0L;
colIndex = 1;
}
// Fill remaining columns with predictor data from feats.
for (long j = 0; j < feat_count; j++) {
mat(X)[i][colIndex] = vec(feats[j].data)[i];
colIndex++;
}
}
Vector y_source = output->data;
// Compute weights using the normal equation: w = (X^T X)^(-1) X^T y
Matrix X_T = t_matrix(&X);
Matrix XTX = mul_matr(&X_T, &X);
Matrix XTX_inv = inv_matr(&XTX);
Matrix Y = empty_matr(y_source.size, 1);
for (long i = 0; i < y_source.size; i++) {
mat(Y)[i][0] = vec(y_source)[i];
}
Matrix XTy = mul_matr(&X_T, &Y);
Matrix w_mat = mul_matr(&XTX_inv, &XTy);
// Store the computed weights into the model.
for (long i = 0; i < predictor_count; i++) {
model->weights[i] = mat(w_mat)[i][0];
}
/* --- Free intermediate matrices --- */
free_matr(&X);
free_matr(&X_T);
free_matr(&XTX);
free_matr(&XTX_inv);
free_matr(&Y);
free_matr(&XTy);
free_matr(&w_mat);
return model;
}
long double run_model(LinearRegression *model, data_row input)
{
long double result = 0.0L;
if (model->has_intercept) {
result += model->weights[0]; // Intercept term.
// Multiply each predictor weight with its corresponding input value.
for (long i = 1; i < model->weight_count; i++) {
result += model->weights[i] * input[i - 1];
}
} else {
// No intercept: weights align directly with input columns.
for (long i = 0; i < model->weight_count; i++) {
result += model->weights[i] * input[i];
}
}
return result;
}
void save_model(LinearRegression *model, char *file_path)
{
FILE *fp = fopen(file_path, "wb");
if (fp == NULL) {
fprintf(stderr, "Error opening file for writing.\n");
goto error;
}
// Write weight_count as a long.
if (fwrite(&model->weight_count, sizeof(long), 1, fp) != 1 ||
fwrite(&model->has_intercept, sizeof(bool), 1, fp) != 1) {
fprintf(stderr, "Error writing model header.\n");
goto error;
}
for (long i = 0; i < model->weight_count; i++) {
if (fwrite(&model->weights[i], sizeof(long double), 1, fp) != 1) {
fprintf(stderr, "Error writing weight value.\n");
goto error;
}
int name_len = strlen(model->weight_names[i]) + 1;
if (fwrite(&name_len, sizeof(int), 1, fp) != 1) {
fprintf(stderr, "Error writing name length.\n");
goto error;
}
if (fwrite(model->weight_names[i], sizeof(char), name_len, fp) != (size_t)name_len) {
fprintf(stderr, "Error writing weight name.\n");
goto error;
}
}
fclose(fp);
return;
error:
if (fp != NULL) fclose(fp);
exit(1);
}
LinearRegression * load_model(char *file_path)
{
FILE *fp = fopen(file_path, "rb");
if (fp == NULL) {
fprintf(stderr, "Error opening file for reading.\n");
return NULL;
}
LinearRegression *model = malloc(sizeof(LinearRegression));
if (!model) {
fprintf(stderr, "Memory allocation error for LinearRegression\n");
goto error;
}
if (fread(&model->weight_count, sizeof(long), 1, fp) != 1 ||
fread(&model->has_intercept, sizeof(bool), 1, fp) != 1) {
fprintf(stderr, "Error reading model header.\n");
goto error;
}
model->weights = malloc(sizeof(long double) * model->weight_count);
model->weight_names = malloc(sizeof(char *) * model->weight_count);
if (!model->weights || !model->weight_names) {
fprintf(stderr, "Memory allocation error for weights or names\n");
goto error;
}
for (long i = 0; i < model->weight_count; i++) {
if (fread(&model->weights[i], sizeof(long double), 1, fp) != 1) {
fprintf(stderr, "Error reading weight value.\n");
goto error;
}
int name_len = 0;
if (fread(&name_len, sizeof(int), 1, fp) != 1) {
fprintf(stderr, "Error reading name length.\n");
goto error;
}
model->weight_names[i] = malloc(name_len);
if (!model->weight_names[i]) {
fprintf(stderr, "Memory allocation error for weight name\n");
goto error;
}
if (fread(model->weight_names[i], sizeof(char), name_len, fp) != (size_t)name_len) {
fprintf(stderr, "Error reading weight name.\n");
goto error;
}
}
fclose(fp);
return model;
error:
if (fp) fclose(fp);
if (model) {
if (model->weight_names) {
for (long i = 0; i < model->weight_count; i++) {
free(model->weight_names[i]);
}
free(model->weight_names);
}
free(model->weights);
free(model);
}
return NULL;
}