-
Notifications
You must be signed in to change notification settings - Fork 8
/
helper.c
116 lines (95 loc) · 2.67 KB
/
helper.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
#include <linear.h>
#include <stdio.h>
#include <stdlib.h>
struct feature_node** build_feature_node(double* x, int n_rows, int n_cols, double bias) {
struct feature_node** fn_x;
struct feature_node* x_space;
double elem = 0;
int n_elems = 0, x_idx = 0, i = 0, j = 0;
for (i = 0; i < n_rows; i++) {
for (j = 0; j < n_cols; j++) {
elem = x[i*n_cols+j];
if (elem != 0)
++n_elems;
}
}
n_elems += n_rows; // for bias term
fn_x = calloc(n_rows, sizeof(struct feature_node *));
x_space = calloc(n_elems + n_rows, sizeof(struct feature_node));
x_idx = 0;
for (i = 0; i < n_rows; i++) {
fn_x[i] = &x_space[x_idx];
for (j = 0; j < n_cols; j++) {
elem = x[i*n_cols+j];
if (elem != 0) {
x_space[x_idx].index = j + 1;
x_space[x_idx].value = elem;
++x_idx;
}
}
if (bias >= 0) {
x_space[x_idx].index = j + 1;
x_space[x_idx].value = bias;
}
++x_idx;
x_space[x_idx].index = -1;
++x_idx;
}
return fn_x;
}
struct model* call_train(double* x, double* y, int n_rows, int n_cols, double bias,
int solver_type, double C, double p, double eps,
int nr_weight, int* weight_label, double* weight) {
int nElems = 0;
int i, j, x_idx;
double elem;
struct feature_node *x_space;
struct problem prob;
struct parameter param;
param.weight_label = weight_label;
param.weight = weight;
param.init_sol = NULL;
param.solver_type = 0;
param.eps = eps;
param.C = C;
param.nr_weight = nr_weight;
param.p = p;
prob.bias = bias;
prob.l = n_rows;
if (prob.bias >= 0) {
prob.n = n_cols + 1;
} else {
prob.n = n_cols;
}
prob.y = y;
prob.x = build_feature_node(x, n_rows, n_cols, prob.bias);
return train(&prob, ¶m);
}
double* call_predict(const struct model *model_, double* x, int n_rows, int n_cols) {
int i;
struct feature_node** fn_x;
double* result;
result = calloc(n_rows, sizeof(double));
fn_x = build_feature_node(x, n_rows, n_cols, -1);
for (i = 0; i < n_rows; ++i) {
result[i] = predict(model_, fn_x[i]);
}
return result;
}
double* call_predict_proba(const struct model *model_, double* x,
int n_rows, int n_cols, int n_classes) {
int i, j;
struct feature_node** fn_x;
double* result;
double* proba;
result = calloc(n_rows * n_classes, sizeof(double));
proba = calloc(n_classes, sizeof(double));
fn_x = build_feature_node(x, n_rows, n_cols, -1);
for (i = 0; i < n_rows; ++i) {
predict_probability(model_, fn_x[i], proba);
for (j = 0; j < n_classes; ++j)
result[i*n_classes+j] = proba[j];
}
free(proba);
return result;
}