-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.c
272 lines (233 loc) · 7.1 KB
/
training.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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#define MAX_COLUMNS 6
#define MAX_TRAIN_DATA_NUMBER 28594
#define LEARNING_RATE 0.05
#define EPOCHS 1000
#define MAX_TEST_DATA_NUMBER 7149
float train_data[MAX_TRAIN_DATA_NUMBER][MAX_COLUMNS];
float test_data[MAX_TEST_DATA_NUMBER][MAX_COLUMNS];
int train_data_number = 0;
int test_data_number = 0;
float weights[MAX_COLUMNS-1] = {0};
float max_weights[MAX_COLUMNS-1] = {0};
void readTrainData(const char *filePath)
{
FILE *file = fopen(filePath, "r");
if (file == NULL)
{
perror("An error occurred while opening the file");
exit(EXIT_FAILURE);
}
char row[MAX_TRAIN_DATA_NUMBER];
char *token;
int i;
while (fgets(row, sizeof(row), file) && train_data_number < MAX_TRAIN_DATA_NUMBER)
{
i = 0;
token = strtok(row, " ");
while (token != NULL && i < MAX_COLUMNS)
{
train_data[train_data_number][i] = atof(token);
token = strtok(NULL, " ");
i++;
}
train_data_number++;
}
fclose(file);
}
void readTestData(const char *filePath)
{
FILE *file = fopen(filePath, "r");
if (file == NULL)
{
perror("An error occurred while opening the file");
exit(EXIT_FAILURE);
}
char satir[MAX_TEST_DATA_NUMBER];
char *token;
int i;
while (fgets(satir, sizeof(satir), file) && test_data_number < MAX_TEST_DATA_NUMBER)
{
i = 0;
token = strtok(satir, " ");
while (token != NULL && i < MAX_COLUMNS)
{
test_data[test_data_number][i] = atof(token);
token = strtok(NULL, " ");
i++;
}
test_data_number++;
}
fclose(file);
}
void initializeWeights()
{
srand(time(NULL));
for (int i = 0; i < MAX_COLUMNS - 1; i++)
{
weights[i] = ((float)rand() / RAND_MAX) - 0.5;
}
}
float sigmoid(float z)
{
return 1.0 / (1.0 + exp(-z));
}
float calculateTrainAcc(int epoch)
{
int true_pred = 0;
for (int i = 0; i < train_data_number; i++)
{
float prediction = 0;
for (int j = 0; j < MAX_COLUMNS - 1; j++)
{
prediction += train_data[i][j] * weights[j];
}
float h = sigmoid(prediction);
int tahmin = h >= 0.5 ? 1.000000 : 0.000000;
if (tahmin == train_data[i][MAX_COLUMNS - 1])
{
true_pred++;
}
}
float acc = (float)true_pred / train_data_number * 100;
printf("%d. Epoch Accuracy : %.2f%%\n", epoch, acc);
return acc;
}
void trainLogReg()
{
float max_acc = 0;
float acc = 0;
float mse = 0;
float min_mse = HUGE_VALF;
int max_epoch = 0;
initializeWeights();
for (int epoch = 0; epoch < EPOCHS; epoch++)
{
for (int i = 0; i < train_data_number; i++)
{
float prediction = 0;
for (int j = 0; j < MAX_COLUMNS - 1; j++)
{
prediction += train_data[i][j] * weights[j];
}
float h = sigmoid(prediction);
float error = train_data[i][MAX_COLUMNS - 1] - h;
for (int j = 0; j < MAX_COLUMNS - 1; j++)
{
weights[j] += LEARNING_RATE * error * train_data[i][j];
}
}
float total_squared_error = 0;
for (int i = 0; i < train_data_number; i++)
{
float prediction = 0;
for (int j = 0; j < MAX_COLUMNS - 1; j++)
{
prediction += train_data[i][j] * weights[j];
}
float h = sigmoid(prediction);
float squared_error = pow(train_data[i][MAX_COLUMNS - 1] - h, 2); // Kare hata hesabı
total_squared_error += squared_error; // Toplam kare hataya ekleniyor.
}
mse = total_squared_error / train_data_number;
acc = calculateTrainAcc(epoch);
mse = total_squared_error / train_data_number;
if(acc > max_acc)
{
max_acc = acc;
max_epoch = epoch;
min_mse = mse;
memcpy(max_weights, weights, sizeof(weights));
}
}
FILE *file = fopen("TrainResults.txt", "w");
if (file == NULL)
{
perror("An error occurred while creating the file");
exit(EXIT_FAILURE);
}
fprintf(file, "Min Loss : %.6f\n", min_mse);
fprintf(file, "Max Epoch : %d\n", max_epoch);
fprintf(file, "Max Accuracy : %.2f%%\n", max_acc);
fprintf(file, "Max Weights : \n");
for (int j = 0; j < MAX_COLUMNS - 1; j++)
{
fprintf(file, "W%d: %.6f\n", j + 1, max_weights[j]);
}
fclose(file);
}
void test()
{
int true_pred = 0;
int false_pred = 0;
int true_positive = 0;
int false_negative = 0;
int true_negative = 0;
int false_positive = 0;
float total_squared_error = 0;
for (int i = 0; i < test_data_number; i++)
{
float prediction = 0;
for (int j = 0; j < MAX_COLUMNS - 1; j++)
{
prediction += test_data[i][j] * max_weights[j];
}
float h = sigmoid(prediction);
float squared_error = pow(test_data[i][MAX_COLUMNS - 1] - h, 2); // Kare hata hesabı
total_squared_error += squared_error; // Toplam kare hataya ekleniyor.
int pred = h >= 0.5 ? 1 : 0;
if (pred == test_data[i][MAX_COLUMNS - 1])
{
true_pred++;
if (pred == 1)
true_positive++;
else
true_negative++;
}
else
{
false_pred++;
if (pred == 1)
false_positive++;
else
false_negative++;
}
}
float accuracy = (float)true_pred / (true_pred + false_pred) * 100;
float precision = (float)true_positive / (true_positive + false_positive);
float recall = (float)true_positive / (true_positive + false_negative);
float f1_score = 2 * (precision * recall) / (precision + recall);
float mse = total_squared_error / test_data_number;
FILE *file = fopen("TestResults.txt", "w");
if (file == NULL)
{
perror("An error occurred while creating the file");
exit(EXIT_FAILURE);
}
fprintf(file, "Test Accuracy : %.2f%%\n", accuracy);
fprintf(file, "True Prediction Number : %d\n", true_pred);
fprintf(file, "Precision: %.2f\n", precision);
fprintf(file, "Recall: %.2f\n", recall);
fprintf(file, "F1 Score: %.2f\n", f1_score);
fprintf(file, "MSE: %.6f\n", mse);
fprintf(file, "\n\n");
fprintf(file, "\t\tConfusion Matrix:\n\n");
fprintf(file, "%20s %10s %10s\n", " ", "Predicted 0", "Predicted 1");
fprintf(file, "%20s %10d %10d\n", "Actual 0", true_negative, false_positive);
fprintf(file, "%20s %10d %10d\n", "Actual 1", false_negative, true_positive);
fclose(file);
}
int main()
{
const char *filePathTrain = "dataset/train_smoke_detection_iot.csv";
readTrainData(filePathTrain);
const char *filePathTest = "dataset/test_smoke_detection_iot.csv";
readTestData(filePathTest);
trainLogReg();
test();
return EXIT_SUCCESS;
}