-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression2.cpp
More file actions
327 lines (276 loc) · 8.52 KB
/
Copy pathLinearRegression2.cpp
File metadata and controls
327 lines (276 loc) · 8.52 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//https://github.com/yacineMahdid/artificial-intelligence-and-machine-learning/blob/master/linear_regression_in_cpp/main.cpp
#include <iostream>
#include <math.h>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <sstream>
using namespace std; // stdout library for printing values
bool custom_sort(double a, double b) /* this custom sort function is defined to
sort on basis of min absolute value or error*/
{
double a1 = abs(a - 0);
double b1 = abs(b - 0);
return a1 < b1;
}
float mean(float *data, int length)
{
float total = 0;
for (int i = 0; i < length; i++)
{
total = total + data[i];
}
return (total / length);
}
// sum up the square of the residual
float total_sum_of_square(float *y, int length)
{
float total = 0;
float residual;
float y_mean = mean(y, length);
for (int i = 0; i < length; i++)
{
residual = (y[i] - y_mean);
total = total + (residual * residual);
}
return total;
}
// sum up the residual of the squared errors
float residual_sum_of_square(float *y_pred, float *y_true, int length)
{
float total = 0;
float residual;
for (int i = 0; i < length; i++)
{
residual = (y_true[i] - y_pred[i]);
total = total + (residual * residual);
}
return total;
}
// Coefficient of determination for goodness of fit of the regression
int r2(float *y_pred, float *y_true, int length)
{
float sum_squared_residual = residual_sum_of_square(y_pred, y_true, length);
float sum_squared_total = total_sum_of_square(y_true, length);
return (1 - ((sum_squared_residual / sum_squared_total)));
}
// wrapper function around residual sum of square in order to have a nicer
// interface to calculate MSE
float mean_squared_error(float *y_pred, float *y_true, int length)
{
return residual_sum_of_square(y_pred, y_true, length) / length;
}
#include "math.h"
// Experiment Variables
const char *FILENAME = "test.csv";
int MAX_ITERATION = 1000;
float LEARNING_RATE = 0.1;
/****************** DATASET ******************/
// Dataset class that holds the X and y information
// for a given csv file. Act a bit like a poor man dataframe
class Dataset
{
public:
float **X;
float *y;
int number_predictor;
int length;
Dataset() {}
// Constructor when reading a CSV file
Dataset(const char *filename)
{
// Variable Initialization
length = 0;
number_predictor = 0;
int index = 0;
// Reading File to get the number of x and y data points
std::ifstream infile(filename);
std::string line;
while (std::getline(infile, line))
{
length++;
// Calculate the number of predictors (runs only one time)
if (length == 1)
{
int i = 0;
while (line[i] != '\0')
{
if (line[i] == ',')
{
number_predictor++;
}
i++;
}
}
}
infile.close();
// Mallocating space for X and y
X = (float **)malloc(sizeof(float *) * length);
for (int i = 0; i < length; i++)
{
X[i] = (float *)malloc(sizeof(float) * number_predictor);
}
y = (float *)malloc(sizeof(float) * length);
// Rereading the file to extract x and y values
char comma;
std::ifstream samefile(filename);
int current_index = 0;
while (std::getline(samefile, line))
{
std::stringstream line_stream(line);
int current_predictor = 0;
float number;
while (line_stream >> number)
{
if (current_predictor == number_predictor)
{
y[current_index] = number;
}
else
{
X[current_index][current_predictor] = number;
current_predictor++;
}
if (line_stream.peek() == ',')
{
//Ignore character if there is ',' its similar to "break"
line_stream.ignore();
}
}
current_index++;
}
samefile.close();
}
};
/****************** WEIGHTS ******************/
class Weights
{
private:
int MAX_WEIGHTS;
public:
float *values;
int number_weights;
Weights(){};
Weights(int number_predictor)
{
number_weights = number_predictor;
values = (float *)std::calloc(number_weights, sizeof(float));
};
void update(float **X, float *y, float *y_pred, float learning_rate, int length)
{
float multiplier = learning_rate / length;
// Update each weights
for (int i = 0; i < number_weights; i++)
{
float sum = (sum_residual(X, y, y_pred, i, length));
// printf("Sum = %f\n", sum);
values[i] = values[i] - multiplier * sum;
}
}
float sum_residual(float **X, float *y, float *y_pred, int current_predictor, int length)
{
float total = 0;
float residual;
for (int i = 0; i < length; i++)
{
residual = (y_pred[i] - y[i]);
total = total + residual * X[i][current_predictor];
}
return total;
}
// Pretty print the weights of the model
void print_weights()
{
char function_string[1000];
// printf("Number weights = %d\n", number_weights);
strcpy(function_string, "y = ");
for (int i = 0; i < number_weights; i++)
{
// printf("Weights %d is = %f\n", i, values[i]);
char weight[20];
sprintf(weight, "%.2f * x%d", values[i], i);
strcat(function_string, weight);
if (i == number_weights - 1)
{
strcat(function_string, "\n");
}
else
{
strcat(function_string, " + ");
}
}
// printf("%s\n", function_string);
}
};
// Model class for Linear Regression
// Use MSE and gradient descent for optimization
// of the parameters
class LinearRegressionModel
{
// Models Variable
float **X;
float *y;
int length;
public:
Weights weights;
LinearRegressionModel(float **X_in, float *y_in, int length_in, int number_predictor)
{
X = X_in;
y = y_in;
length = length_in;
weights = Weights(number_predictor);
}
// Main training loop
void train(int max_iteration, float learning_rate)
{
float *y_pred = (float *)std::malloc(sizeof(float) * length);
while (max_iteration > 0)
{
// Will predict the y given the weights and the Xs
for (int i = 0; i < length; i++)
{
y_pred[i] = predict(X[i]);
}
weights.update(X, y, y_pred, learning_rate, length);
float mse = mean_squared_error(y_pred, y, length);
if (max_iteration % 100 == 0)
{
weights.print_weights();
// std::cout << "Iteration left: " << max_iteration << "; MSE = " << mse << "\n";
}
max_iteration--;
}
free(y_pred);
}
// Run the an array of predictor through the learned weight
float predict(float *x)
{
float prediction = 0;
for (int i = 0; i < weights.number_weights; i++)
{
prediction = prediction + weights.values[i] * x[i];
}
return prediction;
}
};
// int main()
// {
// // Variable Initialization
// int length_train;
// // std::cout << "Reading CSV file" << FILENAME << "\n";
// Dataset data = Dataset(FILENAME);
// // Training
// // std::cout << "Making LinearRegressionModel \n";
// LinearRegressionModel linear_reg = LinearRegressionModel(data.X, data.y, data.length, data.number_predictor);
// // std::cout << "Training \n";
// linear_reg.train(MAX_ITERATION, LEARNING_RATE);
// // Testing TODO: Testing is a bit clumsy right now, we could just keep a hold out of the data
// // std::cout << "Testing \n";
// float X_test[2];
// X_test[0] = 1;
// X_test[1] = 123;
// float y_test = linear_reg.predict(X_test);
// linear_reg.weights.print_weights();
// // std::cout << "Testing for X0 = " << X_test[0] << ", X1 = " << X_test[1] << "\n";
// // std::cout << "y = " << y_test << "\n";
// }