-
Notifications
You must be signed in to change notification settings - Fork 1
/
classifier.c
177 lines (143 loc) · 4.09 KB
/
classifier.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "datastructures.h"
const double PI = 3.14159265358979323846;
struct class {
struct rarray **tdata;
double **stats;
double prob;
};
struct bclassifier {
struct hashtable *classes;
char **classnames;
int nvars;
};
int getnumvars(char line[])
{
int i = 0; int vars = 0;
while (line[i] != '\0') {
if (line[i] == ',')
vars++;
i++;
}
return vars;
}
void installrarrays(struct hashtable *t, char *key, int vars)
{
struct rarray **s = (struct rarray **) malloc(vars*sizeof(struct rarray *));
struct class *c = malloc(sizeof(struct class));
for (int i = 0; i < vars; i++)
s[i] = getrarray();
c->tdata = s;
c->prob = 1;
install(t, key, c);
}
void getdatafromfile(struct bclassifier *b, char filename[])
{
FILE *fp = NULL;
char *key = NULL;
char line[256];
fp = fopen(filename, "r");
// first line to find the number of variables passed in
fgets(line, 256, fp);
b->nvars = getnumvars(line);
int cvar = 0; int totsamples = 0;
struct hashtable *t = newhashtable();
while (1) {
char *pch = strtok(line, ",");
while (pch != NULL) {
if (cvar == 0) {
key = pch;
if (lookup(t, key) == NULL)
installrarrays(t, key, b->nvars);
else {
struct class *c = (struct class *) lookup(t, key);
c->prob += 1;
}
}
else {
struct class *c = (struct class *) lookup(t, key);
struct rarray *s = c->tdata[cvar - 1];
rarray_push(s, atof(pch));
}
pch = strtok(NULL, ",");
cvar++;
}
cvar = 0;
totsamples++;
if (fgets(line, 80, fp) == NULL) break;
}
b->classnames = keys(t);
b->classes = t;
for (int i = 0; i < b->classes->numkeys; i++) {
struct class *c = (struct class *) lookup(t, b->classnames[i]);
c->prob = (c->prob) / totsamples;
}
}
struct bclassifier *getclassifier()
{
struct bclassifier *b = malloc(sizeof(struct bclassifier));
b->classes = newhashtable();
return b;
}
void destroyclassifier(struct bclassifier *b)
{
free (b);
}
double getmean(double stats[], int nsamples)
{
double sum = 0.0;
for (int i = 0; i < nsamples; i++)
sum += stats[i];
return sum / nsamples;
}
double getvariance(double stats[], int nsamples)
{
double var = 0.0;
double mean = getmean(stats, nsamples);
for (int i = 0; i < nsamples; i++)
var += (stats[i] - mean) * (stats[i] - mean);
return ( 1 / ( nsamples - 1.0 ) ) * var;
}
double getprob(double mean, double var, double val)
{
double a = 1 / (sqrt(2*PI*var));
double b = ( - (val - mean)*(val - mean) / (2*var) );
return a*exp(b);
}
void train(struct bclassifier *b, char filename[])
{
getdatafromfile(b, filename);
struct hashtable *t = b->classes;
char **classnames = b->classnames;
for (int k = 0; k < t->numkeys; k++) {
struct class *c = (struct class *) lookup(t, classnames[k]);
double **stats = malloc(b->nvars*sizeof(double *));
for (int v = 0; v < b->nvars; v++) {
struct rarray *tdata = c->tdata[v];
double *ms = malloc(2*sizeof(double));
ms[0] = getmean(tdata->v, tdata->size);
ms[1] = getvariance(tdata->v, tdata->size);
stats[v] = ms;
}
c->stats = stats;
}
}
int classify(struct bclassifier *b, double input[])
{
double cp, prob;
double cpmax = 0.0; int gmax = -1;
for (int g = 0; g < b->classes->numkeys; g++) {
struct class *c = (struct class *) lookup(b->classes, b->classnames[g]);
cp = c->prob;
for (int v = 0; v < b->nvars; v++)
cp = cp * getprob(c->stats[v][0], c->stats[v][1], input[v]);
if (cp > cpmax) {
cpmax = cp;
gmax = g;
}
}
return gmax;
}