This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SVM.c
150 lines (134 loc) · 3.28 KB
/
SVM.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
#include "svm.h"
#include <stdio.h>
#include <stdbool.h>
#include "math.h"
float *PCA_transform(float *);
#ifdef REGRESSION
void svr_test_dataset();
#else
void svc_test_dataset();
#endif
#ifdef DS_TEST
void svm_test_dataset(bool isRegression)
{
#ifdef REGRESSION
svr_test_dataset();
#else
svc_test_dataset();
#endif
}
#ifndef REGRESSION
void svc_test_dataset()
{
int predictedLabels[N_TEST];
int nCorrect = 0;
int i = 0;
nCorrect = 0;
for (i = 0; i < N_TEST; i++)
{
predictedLabels[i] = svm_classification(X_test[i]);
if (predictedLabels[i] == y_test[i])
{
nCorrect++;
}
}
printf("\nLinear SVM rate: %f\n", (float)nCorrect * 100.0f / (float)N_TEST);
fflush(stdout);
}
#endif
#ifdef REGRESSION
void svr_test_dataset()
{
float predictions[N_TEST];
int i = 0;
float y_sum = 0;
float y_mean = 0;
float SS_res = 0;
float SS_tot = 0;
for (i = 0; i < N_TEST; i++)
{
predictions[i] = svm_regression(X_test[i]);
#ifdef MINMAX_NORMALIZATION
printf("\nLinear SVM Error: %f %f %f\n", predictions[i] - (y_test[i]/S_Y), predictions[i], (y_test[i]/S_Y));
y_sum += (y_test[i]/S_Y);
y_mean = y_sum/i+1;
#elif defined (STANDARD_NORMALIZATION)
y_sum += (y_test[i] * S_Y + U_Y);
y_mean = y_sum/i+1;
printf("\nLinear SVM Error: %f %f %f\n", predictions[i] - (y_test[i] * S_Y + U_Y), predictions[i], (y_test[i] * S_Y + U_Y));
#endif
}
for (i = 0; i < N_TEST; i++)
{
predictions[i] = svm_regression(X_test[i]);
#ifdef MINMAX_NORMALIZATION
SS_res += pow(((y_test[i]/S_Y) - predictions[i]),2);
SS_tot += pow((y_test[i]/S_Y) - y_mean),2);
#elif defined (STANDARD_NORMALIZATION)
SS_res += pow(((y_test[i] * S_Y + U_Y) - predictions[i]),2);
SS_tot += pow(((y_test[i] * S_Y + U_Y) - y_mean),2);
#endif
}
float R_squared = 1 - (SS_res/SS_tot);
printf("R-squared = %f\n", R_squared);
}
#endif
#endif
#ifndef REGRESSION
int svm_classification(float X[])
{
int m;
if (N_CLASS == 2)
{
float y = bias[0];
int k;
for (k = 0; k < N_FEATURE; k++)
{
y += support_vectors[0][k] * X[k];
}
if (y < 0)
{
return 0;
}
else
{
return 1;
}
}
else
{
float bestDistance = -1000000;
for (m = 0; m < N_CLASS; m++)
{
float y = bias[m];
int k;
for (k = 0; k < N_FEATURE; k++)
{
y += support_vectors[m][k] * X[k];
}
if (y > bestDistance)
{
bestDistance = y;
return m;
}
}
}
}
#endif
#ifdef REGRESSION
float svm_regression(float X[])
{
float y = bias[0];
int k;
for (k = 0; k < N_FEATURE; k++)
{
y += support_vectors[0][k] * X[k];
}
#ifdef MINMAX_NORMALIZATION
y = y / S_Y;
#elif defined (STANDARD_NORMALIZATION)
y = y * S_Y + U_Y;
#endif
return y;
}
#endif