Skip to content

Commit a9edc31

Browse files
committedJan 2, 2024
Logistic regression eval
1 parent cef2d32 commit a9edc31

File tree

5 files changed

+973
-2
lines changed

5 files changed

+973
-2
lines changed
 

‎examples/data/titanic_train.csv

+892
Large diffs are not rendered by default.

‎examples/logistic_regression_eval.c

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <math.h>
5+
#include "logistic_regression_eval.h"
6+
#include "../regression/logistic_regression.h"
7+
8+
TitanicSurvivor *loadTrainDataFromCsv()
9+
{
10+
int n_training = 890;
11+
FILE *stream = fopen("examples/data/titanic_train.csv", "r");
12+
if (stream == NULL)
13+
{
14+
perror("Unable to open the file.");
15+
exit(1);
16+
}
17+
char line[1024];
18+
TitanicSurvivor *train_set = malloc(sizeof(TitanicSurvivor) * n_training);
19+
int line_number = 0;
20+
21+
while (fgets(line, 1024, stream))
22+
{
23+
if (line_number > 0)
24+
{
25+
char *token = strtok(line, ",");
26+
int column = 1; // Start from the first column
27+
float firstColumn, thirdColumn;
28+
29+
while (token != NULL)
30+
{
31+
if (column == 2)
32+
{
33+
firstColumn = atof(token);
34+
}
35+
else if (column == 3)
36+
{
37+
thirdColumn = atof(token);
38+
break; // No need to tokenize further
39+
}
40+
token = strtok(NULL, ",");
41+
column++;
42+
}
43+
TitanicSurvivor survivor = {.survived = firstColumn, .pClass = thirdColumn};
44+
train_set[line_number] = survivor;
45+
}
46+
line_number++;
47+
}
48+
return train_set;
49+
}
50+
51+
void calculateLogisticRegression()
52+
{
53+
TitanicSurvivor *train_data = loadTrainDataFromCsv();
54+
int n_training = 890;
55+
56+
double x[n_training];
57+
double y[n_training];
58+
59+
for (int i = 0; i < n_training; i++)
60+
{
61+
x[i] = train_data[i].pClass;
62+
y[i] = train_data[i].survived;
63+
}
64+
65+
double *parameters = logisticRegression(x, y, n_training);
66+
67+
printf("\n %f * x + %f", parameters[0], parameters[1]);
68+
}

‎examples/logistic_regression_eval.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef LOGISTIC_REGRESSION_EVAL
2+
#define LOGISTIC_REGRESSION_EVAL
3+
4+
typedef struct {
5+
int pClass;
6+
int survived;
7+
} TitanicSurvivor;
8+
void calculateLogisticRegression();
9+
#endif

‎main.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdlib.h> // pulls in declaration of malloc, free
1111
#include "nn/tests/nn_test.h"
1212
#include "examples/linear_regression_eval.h"
13+
#include "examples/logistic_regression_eval.h"
1314
// 1 2 3
1415
// 4 5 6
1516

@@ -148,7 +149,8 @@ int main()
148149
// layers: {denseLayer, reluLayer, denseLayer, sigmoidLayer}
149150

150151
// test_nn();
151-
calculateRegression();
152+
// calculateRegression();
153+
calculateLogisticRegression();
152154
return 0;
153155
}
154156

‎regression/logistic_regression.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ double *logisticRegression(double xValues[], double yValues[], int n)
1616
double dA = 0;
1717
double dB = 0;
1818
// Can be increased here
19-
while (iter < 1000)
19+
while (iter < 5000)
2020
{
2121
double *predicted = malloc(n * sizeof(double *));
2222
for (int i = 0; i < n; i++)

0 commit comments

Comments
 (0)
Please sign in to comment.