liblinear-ruby 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/ext/blasp.h +8 -8
- data/ext/daxpy.c +3 -3
- data/ext/ddot.c +3 -3
- data/ext/dnrm2.c +7 -7
- data/ext/dscal.c +4 -4
- data/ext/liblinear_wrap.cxx +382 -382
- data/ext/linear.cpp +44 -55
- data/ext/linear.h +5 -1
- data/ext/tron.cpp +13 -5
- data/ext/tron.h +1 -1
- data/lib/liblinear.rb +2 -0
- data/lib/liblinear/version.rb +1 -1
- metadata +2 -41
- data/liblinear-2.1/COPYRIGHT +0 -31
- data/liblinear-2.1/Makefile +0 -37
- data/liblinear-2.1/Makefile.win +0 -24
- data/liblinear-2.1/README +0 -600
- data/liblinear-2.1/blas/Makefile +0 -22
- data/liblinear-2.1/blas/blas.h +0 -25
- data/liblinear-2.1/blas/blasp.h +0 -438
- data/liblinear-2.1/blas/daxpy.c +0 -57
- data/liblinear-2.1/blas/ddot.c +0 -58
- data/liblinear-2.1/blas/dnrm2.c +0 -70
- data/liblinear-2.1/blas/dscal.c +0 -52
- data/liblinear-2.1/heart_scale +0 -270
- data/liblinear-2.1/linear.cpp +0 -3053
- data/liblinear-2.1/linear.def +0 -22
- data/liblinear-2.1/linear.h +0 -79
- data/liblinear-2.1/matlab/Makefile +0 -49
- data/liblinear-2.1/matlab/README +0 -208
- data/liblinear-2.1/matlab/libsvmread.c +0 -212
- data/liblinear-2.1/matlab/libsvmwrite.c +0 -119
- data/liblinear-2.1/matlab/linear_model_matlab.c +0 -176
- data/liblinear-2.1/matlab/linear_model_matlab.h +0 -2
- data/liblinear-2.1/matlab/make.m +0 -22
- data/liblinear-2.1/matlab/predict.c +0 -341
- data/liblinear-2.1/matlab/train.c +0 -492
- data/liblinear-2.1/predict.c +0 -243
- data/liblinear-2.1/python/Makefile +0 -4
- data/liblinear-2.1/python/README +0 -380
- data/liblinear-2.1/python/liblinear.py +0 -323
- data/liblinear-2.1/python/liblinearutil.py +0 -270
- data/liblinear-2.1/train.c +0 -449
- data/liblinear-2.1/tron.cpp +0 -241
- data/liblinear-2.1/tron.h +0 -35
- data/liblinear-2.1/windows/liblinear.dll +0 -0
- data/liblinear-2.1/windows/libsvmread.mexw64 +0 -0
- data/liblinear-2.1/windows/libsvmwrite.mexw64 +0 -0
- data/liblinear-2.1/windows/predict.exe +0 -0
- data/liblinear-2.1/windows/predict.mexw64 +0 -0
- data/liblinear-2.1/windows/train.exe +0 -0
- data/liblinear-2.1/windows/train.mexw64 +0 -0
@@ -1,119 +0,0 @@
|
|
1
|
-
#include <stdio.h>
|
2
|
-
#include <stdlib.h>
|
3
|
-
#include <string.h>
|
4
|
-
#include "mex.h"
|
5
|
-
|
6
|
-
#ifdef MX_API_VER
|
7
|
-
#if MX_API_VER < 0x07030000
|
8
|
-
typedef int mwIndex;
|
9
|
-
#endif
|
10
|
-
#endif
|
11
|
-
|
12
|
-
void exit_with_help()
|
13
|
-
{
|
14
|
-
mexPrintf(
|
15
|
-
"Usage: libsvmwrite('filename', label_vector, instance_matrix);\n"
|
16
|
-
);
|
17
|
-
}
|
18
|
-
|
19
|
-
static void fake_answer(int nlhs, mxArray *plhs[])
|
20
|
-
{
|
21
|
-
int i;
|
22
|
-
for(i=0;i<nlhs;i++)
|
23
|
-
plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
|
24
|
-
}
|
25
|
-
|
26
|
-
void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat)
|
27
|
-
{
|
28
|
-
FILE *fp = fopen(filename,"w");
|
29
|
-
mwIndex *ir, *jc, k, low, high;
|
30
|
-
size_t i, l, label_vector_row_num;
|
31
|
-
double *samples, *labels;
|
32
|
-
mxArray *instance_mat_col; // instance sparse matrix in column format
|
33
|
-
|
34
|
-
if(fp ==NULL)
|
35
|
-
{
|
36
|
-
mexPrintf("can't open output file %s\n",filename);
|
37
|
-
return;
|
38
|
-
}
|
39
|
-
|
40
|
-
// transpose instance matrix
|
41
|
-
{
|
42
|
-
mxArray *prhs[1], *plhs[1];
|
43
|
-
prhs[0] = mxDuplicateArray(instance_mat);
|
44
|
-
if(mexCallMATLAB(1, plhs, 1, prhs, "transpose"))
|
45
|
-
{
|
46
|
-
mexPrintf("Error: cannot transpose instance matrix\n");
|
47
|
-
return;
|
48
|
-
}
|
49
|
-
instance_mat_col = plhs[0];
|
50
|
-
mxDestroyArray(prhs[0]);
|
51
|
-
}
|
52
|
-
|
53
|
-
// the number of instance
|
54
|
-
l = mxGetN(instance_mat_col);
|
55
|
-
label_vector_row_num = mxGetM(label_vec);
|
56
|
-
|
57
|
-
if(label_vector_row_num!=l)
|
58
|
-
{
|
59
|
-
mexPrintf("Length of label vector does not match # of instances.\n");
|
60
|
-
return;
|
61
|
-
}
|
62
|
-
|
63
|
-
// each column is one instance
|
64
|
-
labels = mxGetPr(label_vec);
|
65
|
-
samples = mxGetPr(instance_mat_col);
|
66
|
-
ir = mxGetIr(instance_mat_col);
|
67
|
-
jc = mxGetJc(instance_mat_col);
|
68
|
-
|
69
|
-
for(i=0;i<l;i++)
|
70
|
-
{
|
71
|
-
fprintf(fp,"%g", labels[i]);
|
72
|
-
|
73
|
-
low = jc[i], high = jc[i+1];
|
74
|
-
for(k=low;k<high;k++)
|
75
|
-
fprintf(fp," %lu:%g", (size_t)ir[k]+1, samples[k]);
|
76
|
-
|
77
|
-
fprintf(fp,"\n");
|
78
|
-
}
|
79
|
-
|
80
|
-
fclose(fp);
|
81
|
-
return;
|
82
|
-
}
|
83
|
-
|
84
|
-
void mexFunction( int nlhs, mxArray *plhs[],
|
85
|
-
int nrhs, const mxArray *prhs[] )
|
86
|
-
{
|
87
|
-
if(nlhs > 0)
|
88
|
-
{
|
89
|
-
exit_with_help();
|
90
|
-
fake_answer(nlhs, plhs);
|
91
|
-
return;
|
92
|
-
}
|
93
|
-
|
94
|
-
// Transform the input Matrix to libsvm format
|
95
|
-
if(nrhs == 3)
|
96
|
-
{
|
97
|
-
char filename[256];
|
98
|
-
if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2]))
|
99
|
-
{
|
100
|
-
mexPrintf("Error: label vector and instance matrix must be double\n");
|
101
|
-
return;
|
102
|
-
}
|
103
|
-
|
104
|
-
mxGetString(prhs[0], filename, mxGetN(prhs[0])+1);
|
105
|
-
|
106
|
-
if(mxIsSparse(prhs[2]))
|
107
|
-
libsvmwrite(filename, prhs[1], prhs[2]);
|
108
|
-
else
|
109
|
-
{
|
110
|
-
mexPrintf("Instance_matrix must be sparse\n");
|
111
|
-
return;
|
112
|
-
}
|
113
|
-
}
|
114
|
-
else
|
115
|
-
{
|
116
|
-
exit_with_help();
|
117
|
-
return;
|
118
|
-
}
|
119
|
-
}
|
@@ -1,176 +0,0 @@
|
|
1
|
-
#include <stdlib.h>
|
2
|
-
#include <string.h>
|
3
|
-
#include "linear.h"
|
4
|
-
|
5
|
-
#include "mex.h"
|
6
|
-
|
7
|
-
#ifdef MX_API_VER
|
8
|
-
#if MX_API_VER < 0x07030000
|
9
|
-
typedef int mwIndex;
|
10
|
-
#endif
|
11
|
-
#endif
|
12
|
-
|
13
|
-
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
|
14
|
-
|
15
|
-
#define NUM_OF_RETURN_FIELD 6
|
16
|
-
|
17
|
-
static const char *field_names[] = {
|
18
|
-
"Parameters",
|
19
|
-
"nr_class",
|
20
|
-
"nr_feature",
|
21
|
-
"bias",
|
22
|
-
"Label",
|
23
|
-
"w",
|
24
|
-
};
|
25
|
-
|
26
|
-
const char *model_to_matlab_structure(mxArray *plhs[], struct model *model_)
|
27
|
-
{
|
28
|
-
int i;
|
29
|
-
int nr_w;
|
30
|
-
double *ptr;
|
31
|
-
mxArray *return_model, **rhs;
|
32
|
-
int out_id = 0;
|
33
|
-
int n, w_size;
|
34
|
-
|
35
|
-
rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD);
|
36
|
-
|
37
|
-
// Parameters
|
38
|
-
// for now, only solver_type is needed
|
39
|
-
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
|
40
|
-
ptr = mxGetPr(rhs[out_id]);
|
41
|
-
ptr[0] = model_->param.solver_type;
|
42
|
-
out_id++;
|
43
|
-
|
44
|
-
// nr_class
|
45
|
-
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
|
46
|
-
ptr = mxGetPr(rhs[out_id]);
|
47
|
-
ptr[0] = model_->nr_class;
|
48
|
-
out_id++;
|
49
|
-
|
50
|
-
if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
|
51
|
-
nr_w=1;
|
52
|
-
else
|
53
|
-
nr_w=model_->nr_class;
|
54
|
-
|
55
|
-
// nr_feature
|
56
|
-
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
|
57
|
-
ptr = mxGetPr(rhs[out_id]);
|
58
|
-
ptr[0] = model_->nr_feature;
|
59
|
-
out_id++;
|
60
|
-
|
61
|
-
// bias
|
62
|
-
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
|
63
|
-
ptr = mxGetPr(rhs[out_id]);
|
64
|
-
ptr[0] = model_->bias;
|
65
|
-
out_id++;
|
66
|
-
|
67
|
-
if(model_->bias>=0)
|
68
|
-
n=model_->nr_feature+1;
|
69
|
-
else
|
70
|
-
n=model_->nr_feature;
|
71
|
-
|
72
|
-
w_size = n;
|
73
|
-
// Label
|
74
|
-
if(model_->label)
|
75
|
-
{
|
76
|
-
rhs[out_id] = mxCreateDoubleMatrix(model_->nr_class, 1, mxREAL);
|
77
|
-
ptr = mxGetPr(rhs[out_id]);
|
78
|
-
for(i = 0; i < model_->nr_class; i++)
|
79
|
-
ptr[i] = model_->label[i];
|
80
|
-
}
|
81
|
-
else
|
82
|
-
rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
|
83
|
-
out_id++;
|
84
|
-
|
85
|
-
// w
|
86
|
-
rhs[out_id] = mxCreateDoubleMatrix(nr_w, w_size, mxREAL);
|
87
|
-
ptr = mxGetPr(rhs[out_id]);
|
88
|
-
for(i = 0; i < w_size*nr_w; i++)
|
89
|
-
ptr[i]=model_->w[i];
|
90
|
-
out_id++;
|
91
|
-
|
92
|
-
/* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */
|
93
|
-
return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names);
|
94
|
-
|
95
|
-
/* Fill struct matrix with input arguments */
|
96
|
-
for(i = 0; i < NUM_OF_RETURN_FIELD; i++)
|
97
|
-
mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i]));
|
98
|
-
/* return */
|
99
|
-
plhs[0] = return_model;
|
100
|
-
mxFree(rhs);
|
101
|
-
|
102
|
-
return NULL;
|
103
|
-
}
|
104
|
-
|
105
|
-
const char *matlab_matrix_to_model(struct model *model_, const mxArray *matlab_struct)
|
106
|
-
{
|
107
|
-
int i, num_of_fields;
|
108
|
-
int nr_w;
|
109
|
-
double *ptr;
|
110
|
-
int id = 0;
|
111
|
-
int n, w_size;
|
112
|
-
mxArray **rhs;
|
113
|
-
|
114
|
-
num_of_fields = mxGetNumberOfFields(matlab_struct);
|
115
|
-
rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields);
|
116
|
-
|
117
|
-
for(i=0;i<num_of_fields;i++)
|
118
|
-
rhs[i] = mxGetFieldByNumber(matlab_struct, 0, i);
|
119
|
-
|
120
|
-
model_->nr_class=0;
|
121
|
-
nr_w=0;
|
122
|
-
model_->nr_feature=0;
|
123
|
-
model_->w=NULL;
|
124
|
-
model_->label=NULL;
|
125
|
-
|
126
|
-
// Parameters
|
127
|
-
ptr = mxGetPr(rhs[id]);
|
128
|
-
model_->param.solver_type = (int)ptr[0];
|
129
|
-
id++;
|
130
|
-
|
131
|
-
// nr_class
|
132
|
-
ptr = mxGetPr(rhs[id]);
|
133
|
-
model_->nr_class = (int)ptr[0];
|
134
|
-
id++;
|
135
|
-
|
136
|
-
if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
|
137
|
-
nr_w=1;
|
138
|
-
else
|
139
|
-
nr_w=model_->nr_class;
|
140
|
-
|
141
|
-
// nr_feature
|
142
|
-
ptr = mxGetPr(rhs[id]);
|
143
|
-
model_->nr_feature = (int)ptr[0];
|
144
|
-
id++;
|
145
|
-
|
146
|
-
// bias
|
147
|
-
ptr = mxGetPr(rhs[id]);
|
148
|
-
model_->bias = ptr[0];
|
149
|
-
id++;
|
150
|
-
|
151
|
-
if(model_->bias>=0)
|
152
|
-
n=model_->nr_feature+1;
|
153
|
-
else
|
154
|
-
n=model_->nr_feature;
|
155
|
-
w_size = n;
|
156
|
-
|
157
|
-
// Label
|
158
|
-
if(mxIsEmpty(rhs[id]) == 0)
|
159
|
-
{
|
160
|
-
model_->label = Malloc(int, model_->nr_class);
|
161
|
-
ptr = mxGetPr(rhs[id]);
|
162
|
-
for(i=0;i<model_->nr_class;i++)
|
163
|
-
model_->label[i] = (int)ptr[i];
|
164
|
-
}
|
165
|
-
id++;
|
166
|
-
|
167
|
-
ptr = mxGetPr(rhs[id]);
|
168
|
-
model_->w=Malloc(double, w_size*nr_w);
|
169
|
-
for(i = 0; i < w_size*nr_w; i++)
|
170
|
-
model_->w[i]=ptr[i];
|
171
|
-
id++;
|
172
|
-
mxFree(rhs);
|
173
|
-
|
174
|
-
return NULL;
|
175
|
-
}
|
176
|
-
|
data/liblinear-2.1/matlab/make.m
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
% This make.m is for MATLAB and OCTAVE under Windows, Mac, and Unix
|
2
|
-
function make()
|
3
|
-
try
|
4
|
-
% This part is for OCTAVE
|
5
|
-
if(exist('OCTAVE_VERSION', 'builtin'))
|
6
|
-
mex libsvmread.c
|
7
|
-
mex libsvmwrite.c
|
8
|
-
mex -I.. train.c linear_model_matlab.c ../linear.cpp ../tron.cpp ../blas/daxpy.c ../blas/ddot.c ../blas/dnrm2.c ../blas/dscal.c
|
9
|
-
mex -I.. predict.c linear_model_matlab.c ../linear.cpp ../tron.cpp ../blas/daxpy.c ../blas/ddot.c ../blas/dnrm2.c ../blas/dscal.c
|
10
|
-
% This part is for MATLAB
|
11
|
-
% Add -largeArrayDims on 64-bit machines of MATLAB
|
12
|
-
else
|
13
|
-
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmread.c
|
14
|
-
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmwrite.c
|
15
|
-
mex CFLAGS="\$CFLAGS -std=c99" -I.. -largeArrayDims train.c linear_model_matlab.c ../linear.cpp ../tron.cpp ../blas/daxpy.c ../blas/ddot.c ../blas/dnrm2.c ../blas/dscal.c
|
16
|
-
mex CFLAGS="\$CFLAGS -std=c99" -I.. -largeArrayDims predict.c linear_model_matlab.c ../linear.cpp ../tron.cpp ../blas/daxpy.c ../blas/ddot.c ../blas/dnrm2.c ../blas/dscal.c
|
17
|
-
end
|
18
|
-
catch err
|
19
|
-
fprintf('Error: %s failed (line %d)\n', err.stack(1).file, err.stack(1).line);
|
20
|
-
disp(err.message);
|
21
|
-
fprintf('=> Please check README for detailed instructions.\n');
|
22
|
-
end
|
@@ -1,341 +0,0 @@
|
|
1
|
-
#include <stdio.h>
|
2
|
-
#include <stdlib.h>
|
3
|
-
#include <string.h>
|
4
|
-
#include "linear.h"
|
5
|
-
|
6
|
-
#include "mex.h"
|
7
|
-
#include "linear_model_matlab.h"
|
8
|
-
|
9
|
-
#ifdef MX_API_VER
|
10
|
-
#if MX_API_VER < 0x07030000
|
11
|
-
typedef int mwIndex;
|
12
|
-
#endif
|
13
|
-
#endif
|
14
|
-
|
15
|
-
#define CMD_LEN 2048
|
16
|
-
|
17
|
-
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
|
18
|
-
|
19
|
-
int print_null(const char *s,...) {}
|
20
|
-
int (*info)(const char *fmt,...);
|
21
|
-
|
22
|
-
int col_format_flag;
|
23
|
-
|
24
|
-
void read_sparse_instance(const mxArray *prhs, int index, struct feature_node *x, int feature_number, double bias)
|
25
|
-
{
|
26
|
-
int j;
|
27
|
-
mwIndex *ir, *jc, low, high, i;
|
28
|
-
double *samples;
|
29
|
-
|
30
|
-
ir = mxGetIr(prhs);
|
31
|
-
jc = mxGetJc(prhs);
|
32
|
-
samples = mxGetPr(prhs);
|
33
|
-
|
34
|
-
// each column is one instance
|
35
|
-
j = 0;
|
36
|
-
low = jc[index], high = jc[index+1];
|
37
|
-
for(i=low; i<high && (int) (ir[i])<feature_number; i++)
|
38
|
-
{
|
39
|
-
x[j].index = (int) ir[i]+1;
|
40
|
-
x[j].value = samples[i];
|
41
|
-
j++;
|
42
|
-
}
|
43
|
-
if(bias>=0)
|
44
|
-
{
|
45
|
-
x[j].index = feature_number+1;
|
46
|
-
x[j].value = bias;
|
47
|
-
j++;
|
48
|
-
}
|
49
|
-
x[j].index = -1;
|
50
|
-
}
|
51
|
-
|
52
|
-
static void fake_answer(int nlhs, mxArray *plhs[])
|
53
|
-
{
|
54
|
-
int i;
|
55
|
-
for(i=0;i<nlhs;i++)
|
56
|
-
plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
|
57
|
-
}
|
58
|
-
|
59
|
-
void do_predict(int nlhs, mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag)
|
60
|
-
{
|
61
|
-
int label_vector_row_num, label_vector_col_num;
|
62
|
-
int feature_number, testing_instance_number;
|
63
|
-
int instance_index;
|
64
|
-
double *ptr_label, *ptr_predict_label;
|
65
|
-
double *ptr_prob_estimates, *ptr_dec_values, *ptr;
|
66
|
-
struct feature_node *x;
|
67
|
-
mxArray *pplhs[1]; // instance sparse matrix in row format
|
68
|
-
mxArray *tplhs[3]; // temporary storage for plhs[]
|
69
|
-
|
70
|
-
int correct = 0;
|
71
|
-
int total = 0;
|
72
|
-
double error = 0;
|
73
|
-
double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;
|
74
|
-
|
75
|
-
int nr_class=get_nr_class(model_);
|
76
|
-
int nr_w;
|
77
|
-
double *prob_estimates=NULL;
|
78
|
-
|
79
|
-
if(nr_class==2 && model_->param.solver_type!=MCSVM_CS)
|
80
|
-
nr_w=1;
|
81
|
-
else
|
82
|
-
nr_w=nr_class;
|
83
|
-
|
84
|
-
// prhs[1] = testing instance matrix
|
85
|
-
feature_number = get_nr_feature(model_);
|
86
|
-
testing_instance_number = (int) mxGetM(prhs[1]);
|
87
|
-
if(col_format_flag)
|
88
|
-
{
|
89
|
-
feature_number = (int) mxGetM(prhs[1]);
|
90
|
-
testing_instance_number = (int) mxGetN(prhs[1]);
|
91
|
-
}
|
92
|
-
|
93
|
-
label_vector_row_num = (int) mxGetM(prhs[0]);
|
94
|
-
label_vector_col_num = (int) mxGetN(prhs[0]);
|
95
|
-
|
96
|
-
if(label_vector_row_num!=testing_instance_number)
|
97
|
-
{
|
98
|
-
mexPrintf("Length of label vector does not match # of instances.\n");
|
99
|
-
fake_answer(nlhs, plhs);
|
100
|
-
return;
|
101
|
-
}
|
102
|
-
if(label_vector_col_num!=1)
|
103
|
-
{
|
104
|
-
mexPrintf("label (1st argument) should be a vector (# of column is 1).\n");
|
105
|
-
fake_answer(nlhs, plhs);
|
106
|
-
return;
|
107
|
-
}
|
108
|
-
|
109
|
-
ptr_label = mxGetPr(prhs[0]);
|
110
|
-
|
111
|
-
// transpose instance matrix
|
112
|
-
if(col_format_flag)
|
113
|
-
pplhs[0] = (mxArray *)prhs[1];
|
114
|
-
else
|
115
|
-
{
|
116
|
-
mxArray *pprhs[1];
|
117
|
-
pprhs[0] = mxDuplicateArray(prhs[1]);
|
118
|
-
if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose"))
|
119
|
-
{
|
120
|
-
mexPrintf("Error: cannot transpose testing instance matrix\n");
|
121
|
-
fake_answer(nlhs, plhs);
|
122
|
-
return;
|
123
|
-
}
|
124
|
-
}
|
125
|
-
|
126
|
-
|
127
|
-
prob_estimates = Malloc(double, nr_class);
|
128
|
-
|
129
|
-
tplhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL);
|
130
|
-
if(predict_probability_flag)
|
131
|
-
tplhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL);
|
132
|
-
else
|
133
|
-
tplhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_w, mxREAL);
|
134
|
-
|
135
|
-
ptr_predict_label = mxGetPr(tplhs[0]);
|
136
|
-
ptr_prob_estimates = mxGetPr(tplhs[2]);
|
137
|
-
ptr_dec_values = mxGetPr(tplhs[2]);
|
138
|
-
x = Malloc(struct feature_node, feature_number+2);
|
139
|
-
for(instance_index=0;instance_index<testing_instance_number;instance_index++)
|
140
|
-
{
|
141
|
-
int i;
|
142
|
-
double target_label, predict_label;
|
143
|
-
|
144
|
-
target_label = ptr_label[instance_index];
|
145
|
-
|
146
|
-
// prhs[1] and prhs[1]^T are sparse
|
147
|
-
read_sparse_instance(pplhs[0], instance_index, x, feature_number, model_->bias);
|
148
|
-
|
149
|
-
if(predict_probability_flag)
|
150
|
-
{
|
151
|
-
predict_label = predict_probability(model_, x, prob_estimates);
|
152
|
-
ptr_predict_label[instance_index] = predict_label;
|
153
|
-
for(i=0;i<nr_class;i++)
|
154
|
-
ptr_prob_estimates[instance_index + i * testing_instance_number] = prob_estimates[i];
|
155
|
-
}
|
156
|
-
else
|
157
|
-
{
|
158
|
-
double *dec_values = Malloc(double, nr_class);
|
159
|
-
predict_label = predict_values(model_, x, dec_values);
|
160
|
-
ptr_predict_label[instance_index] = predict_label;
|
161
|
-
|
162
|
-
for(i=0;i<nr_w;i++)
|
163
|
-
ptr_dec_values[instance_index + i * testing_instance_number] = dec_values[i];
|
164
|
-
free(dec_values);
|
165
|
-
}
|
166
|
-
|
167
|
-
if(predict_label == target_label)
|
168
|
-
++correct;
|
169
|
-
error += (predict_label-target_label)*(predict_label-target_label);
|
170
|
-
sump += predict_label;
|
171
|
-
sumt += target_label;
|
172
|
-
sumpp += predict_label*predict_label;
|
173
|
-
sumtt += target_label*target_label;
|
174
|
-
sumpt += predict_label*target_label;
|
175
|
-
|
176
|
-
++total;
|
177
|
-
}
|
178
|
-
|
179
|
-
if(check_regression_model(model_))
|
180
|
-
{
|
181
|
-
info("Mean squared error = %g (regression)\n",error/total);
|
182
|
-
info("Squared correlation coefficient = %g (regression)\n",
|
183
|
-
((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
|
184
|
-
((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt))
|
185
|
-
);
|
186
|
-
}
|
187
|
-
else
|
188
|
-
info("Accuracy = %g%% (%d/%d)\n", (double) correct/total*100,correct,total);
|
189
|
-
|
190
|
-
// return accuracy, mean squared error, squared correlation coefficient
|
191
|
-
tplhs[1] = mxCreateDoubleMatrix(3, 1, mxREAL);
|
192
|
-
ptr = mxGetPr(tplhs[1]);
|
193
|
-
ptr[0] = (double)correct/total*100;
|
194
|
-
ptr[1] = error/total;
|
195
|
-
ptr[2] = ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
|
196
|
-
((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt));
|
197
|
-
|
198
|
-
free(x);
|
199
|
-
if(prob_estimates != NULL)
|
200
|
-
free(prob_estimates);
|
201
|
-
|
202
|
-
switch(nlhs)
|
203
|
-
{
|
204
|
-
case 3:
|
205
|
-
plhs[2] = tplhs[2];
|
206
|
-
plhs[1] = tplhs[1];
|
207
|
-
case 1:
|
208
|
-
case 0:
|
209
|
-
plhs[0] = tplhs[0];
|
210
|
-
}
|
211
|
-
}
|
212
|
-
|
213
|
-
void exit_with_help()
|
214
|
-
{
|
215
|
-
mexPrintf(
|
216
|
-
"Usage: [predicted_label, accuracy, decision_values/prob_estimates] = predict(testing_label_vector, testing_instance_matrix, model, 'liblinear_options','col')\n"
|
217
|
-
" [predicted_label] = predict(testing_label_vector, testing_instance_matrix, model, 'liblinear_options','col')\n"
|
218
|
-
"liblinear_options:\n"
|
219
|
-
"-b probability_estimates: whether to output probability estimates, 0 or 1 (default 0); currently for logistic regression only\n"
|
220
|
-
"-q quiet mode (no outputs)\n"
|
221
|
-
"col: if 'col' is setted testing_instance_matrix is parsed in column format, otherwise is in row format\n"
|
222
|
-
"Returns:\n"
|
223
|
-
" predicted_label: prediction output vector.\n"
|
224
|
-
" accuracy: a vector with accuracy, mean squared error, squared correlation coefficient.\n"
|
225
|
-
" prob_estimates: If selected, probability estimate vector.\n"
|
226
|
-
);
|
227
|
-
}
|
228
|
-
|
229
|
-
void mexFunction( int nlhs, mxArray *plhs[],
|
230
|
-
int nrhs, const mxArray *prhs[] )
|
231
|
-
{
|
232
|
-
int prob_estimate_flag = 0;
|
233
|
-
struct model *model_;
|
234
|
-
char cmd[CMD_LEN];
|
235
|
-
info = &mexPrintf;
|
236
|
-
col_format_flag = 0;
|
237
|
-
|
238
|
-
if(nlhs == 2 || nlhs > 3 || nrhs > 5 || nrhs < 3)
|
239
|
-
{
|
240
|
-
exit_with_help();
|
241
|
-
fake_answer(nlhs, plhs);
|
242
|
-
return;
|
243
|
-
}
|
244
|
-
if(nrhs == 5)
|
245
|
-
{
|
246
|
-
mxGetString(prhs[4], cmd, mxGetN(prhs[4])+1);
|
247
|
-
if(strcmp(cmd, "col") == 0)
|
248
|
-
{
|
249
|
-
col_format_flag = 1;
|
250
|
-
}
|
251
|
-
}
|
252
|
-
|
253
|
-
if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
|
254
|
-
mexPrintf("Error: label vector and instance matrix must be double\n");
|
255
|
-
fake_answer(nlhs, plhs);
|
256
|
-
return;
|
257
|
-
}
|
258
|
-
|
259
|
-
if(mxIsStruct(prhs[2]))
|
260
|
-
{
|
261
|
-
const char *error_msg;
|
262
|
-
|
263
|
-
// parse options
|
264
|
-
if(nrhs>=4)
|
265
|
-
{
|
266
|
-
int i, argc = 1;
|
267
|
-
char *argv[CMD_LEN/2];
|
268
|
-
|
269
|
-
// put options in argv[]
|
270
|
-
mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1);
|
271
|
-
if((argv[argc] = strtok(cmd, " ")) != NULL)
|
272
|
-
while((argv[++argc] = strtok(NULL, " ")) != NULL)
|
273
|
-
;
|
274
|
-
|
275
|
-
for(i=1;i<argc;i++)
|
276
|
-
{
|
277
|
-
if(argv[i][0] != '-') break;
|
278
|
-
++i;
|
279
|
-
if(i>=argc && argv[i-1][1] != 'q')
|
280
|
-
{
|
281
|
-
exit_with_help();
|
282
|
-
fake_answer(nlhs, plhs);
|
283
|
-
return;
|
284
|
-
}
|
285
|
-
switch(argv[i-1][1])
|
286
|
-
{
|
287
|
-
case 'b':
|
288
|
-
prob_estimate_flag = atoi(argv[i]);
|
289
|
-
break;
|
290
|
-
case 'q':
|
291
|
-
info = &print_null;
|
292
|
-
i--;
|
293
|
-
break;
|
294
|
-
default:
|
295
|
-
mexPrintf("unknown option\n");
|
296
|
-
exit_with_help();
|
297
|
-
fake_answer(nlhs, plhs);
|
298
|
-
return;
|
299
|
-
}
|
300
|
-
}
|
301
|
-
}
|
302
|
-
|
303
|
-
model_ = Malloc(struct model, 1);
|
304
|
-
error_msg = matlab_matrix_to_model(model_, prhs[2]);
|
305
|
-
if(error_msg)
|
306
|
-
{
|
307
|
-
mexPrintf("Error: can't read model: %s\n", error_msg);
|
308
|
-
free_and_destroy_model(&model_);
|
309
|
-
fake_answer(nlhs, plhs);
|
310
|
-
return;
|
311
|
-
}
|
312
|
-
|
313
|
-
if(prob_estimate_flag)
|
314
|
-
{
|
315
|
-
if(!check_probability_model(model_))
|
316
|
-
{
|
317
|
-
mexPrintf("probability output is only supported for logistic regression\n");
|
318
|
-
prob_estimate_flag=0;
|
319
|
-
}
|
320
|
-
}
|
321
|
-
|
322
|
-
if(mxIsSparse(prhs[1]))
|
323
|
-
do_predict(nlhs, plhs, prhs, model_, prob_estimate_flag);
|
324
|
-
else
|
325
|
-
{
|
326
|
-
mexPrintf("Testing_instance_matrix must be sparse; "
|
327
|
-
"use sparse(Testing_instance_matrix) first\n");
|
328
|
-
fake_answer(nlhs, plhs);
|
329
|
-
}
|
330
|
-
|
331
|
-
// destroy model_
|
332
|
-
free_and_destroy_model(&model_);
|
333
|
-
}
|
334
|
-
else
|
335
|
-
{
|
336
|
-
mexPrintf("model file should be a struct array\n");
|
337
|
-
fake_answer(nlhs, plhs);
|
338
|
-
}
|
339
|
-
|
340
|
-
return;
|
341
|
-
}
|