$(CXX) $(CFLAGS) -c tron.cpp
lib: linear.cpp linear.h linear.def tron.obj
- $(CXX) $(CFLAGS) -LD linear.cpp tron.obj blas\*.c -Fe$(TARGET)\liblinear -link -DEF:linear.def
+ $(CXX) $(CFLAGS) -LD linear.cpp tron.obj blas\*.c -Fe$(TARGET)\liblinear -link -DEF:linear.def
clean:
-erase /Q *.obj $(TARGET)\*.exe $(TARGET)\*.dll $(TARGET)\*.exp $(TARGET)\*.lib
AR = ar rcv
-RANLIB = ranlib
+RANLIB = ranlib
HEADERS = blas.h blasp.h
-FILES = dnrm2.o daxpy.o ddot.o dscal.o
+FILES = dnrm2.o daxpy.o ddot.o dscal.o
-CFLAGS = $(OPTFLAGS)
+CFLAGS = $(OPTFLAGS)
FFLAGS = $(OPTFLAGS)
blas: $(FILES) $(HEADERS)
- $(AR) blas.a $(FILES)
+ $(AR) blas.a $(FILES)
$(RANLIB) blas.a
clean:
double *beta, double *c, int *ldc);
int dgemv_(char *trans, int *m, int *n, double *alpha, double *a,
- int *lda, double *x, int *incx, double *beta, double *y,
+ int *lda, double *x, int *incx, double *beta, double *y,
int *incy);
int dger_(int *m, int *n, double *alpha, double *x, int *incx,
int drotg_(double *sa, double *sb, double *c, double *s);
int dsbmv_(char *uplo, int *n, int *k, double *alpha, double *a,
- int *lda, double *x, int *incx, double *beta, double *y,
+ int *lda, double *x, int *incx, double *beta, double *y,
int *incy);
int dscal_(int *n, double *sa, double *sx, int *incx);
double *x, int *incx);
int dtrmm_(char *side, char *uplo, char *transa, char *diag, int *m,
- int *n, double *alpha, double *a, int *lda, double *b,
+ int *n, double *alpha, double *a, int *lda, double *b,
int *ldb);
int dtrmv_(char *uplo, char *trans, char *diag, int *n, double *a,
int *lda, double *x, int *incx);
int dtrsm_(char *side, char *uplo, char *transa, char *diag, int *m,
- int *n, double *alpha, double *a, int *lda, double *b,
+ int *n, double *alpha, double *a, int *lda, double *b,
int *ldb);
int dtrsv_(char *uplo, char *trans, char *diag, int *n, double *a,
float *beta, float *c, int *ldc);
int sgemv_(char *trans, int *m, int *n, float *alpha, float *a,
- int *lda, float *x, int *incx, float *beta, float *y,
+ int *lda, float *x, int *incx, float *beta, float *y,
int *incy);
int sger_(int *m, int *n, float *alpha, float *x, int *incx,
int srotg_(float *sa, float *sb, float *c, float *s);
int ssbmv_(char *uplo, int *n, int *k, float *alpha, float *a,
- int *lda, float *x, int *incx, float *beta, float *y,
+ int *lda, float *x, int *incx, float *beta, float *y,
int *incy);
int sscal_(int *n, float *sa, float *sx, int *incx);
float *x, int *incx);
int strmm_(char *side, char *uplo, char *transa, char *diag, int *m,
- int *n, float *alpha, float *a, int *lda, float *b,
+ int *n, float *alpha, float *a, int *lda, float *b,
int *ldb);
int strmv_(char *uplo, char *trans, char *diag, int *n, float *a,
int *lda, float *x, int *incx);
int strsm_(char *side, char *uplo, char *transa, char *diag, int *m,
- int *n, float *alpha, float *a, int *lda, float *b,
+ int *n, float *alpha, float *a, int *lda, float *b,
int *ldb);
int strsv_(char *uplo, char *trans, char *diag, int *n, float *a,
long int i, m, ix, iy, nn, iincx, iincy;
register double ssa;
- /* constant times a vector plus a vector.
- uses unrolled loop for increments equal to one.
- jack dongarra, linpack, 3/11/78.
+ /* constant times a vector plus a vector.
+ uses unrolled loop for increments equal to one.
+ jack dongarra, linpack, 3/11/78.
modified 12/3/93, array(1) declarations changed to array(*) */
/* Dereference inputs */
double stemp;
long int ix, iy;
- /* forms the dot product of two vectors.
- uses unrolled loops for increments equal to one.
- jack dongarra, linpack, 3/11/78.
+ /* forms the dot product of two vectors.
+ uses unrolled loops for increments equal to one.
+ jack dongarra, linpack, 3/11/78.
modified 12/3/93, array(1) declarations changed to array(*) */
/* Dereference inputs */
long int ix, nn, iincx;
double norm, scale, absxi, ssq, temp;
-/* DNRM2 returns the euclidean norm of a vector via the function
- name, so that
+/* DNRM2 returns the euclidean norm of a vector via the function
+ name, so that
- DNRM2 := sqrt( x'*x )
+ DNRM2 := sqrt( x'*x )
- -- This version written on 25-October-1982.
- Modified on 14-October-1993 to inline the call to SLASSQ.
+ -- This version written on 25-October-1982.
+ Modified on 14-October-1993 to inline the call to SLASSQ.
Sven Hammarling, Nag Ltd. */
/* Dereference inputs */
if (nn == 1)
{
norm = fabs(x[0]);
- }
+ }
else
{
scale = 0.0;
ssq = 1.0;
- /* The following loop is equivalent to this call to the LAPACK
+ /* The following loop is equivalent to this call to the LAPACK
auxiliary routine: CALL SLASSQ( N, X, INCX, SCALE, SSQ ) */
for (ix=(nn-1)*iincx; ix>=0; ix-=iincx)
long int i, m, nincx, nn, iincx;
double ssa;
- /* scales a vector by a constant.
- uses unrolled loops for increment equal to 1.
- jack dongarra, linpack, 3/11/78.
- modified 3/93 to return if incx .le. 0.
+ /* scales a vector by a constant.
+ uses unrolled loops for increment equal to 1.
+ jack dongarra, linpack, 3/11/78.
+ modified 3/93 to return if incx .le. 0.
modified 12/3/93, array(1) declarations changed to array(*) */
/* Dereference inputs */
int i, s, iter = 0;
double *xTx = new double[l];
int max_iter = 1000;
- int *index = new int[l];
+ int *index = new int[l];
double *alpha = new double[2*l]; // store alpha and C - alpha
schar *y = new schar[l];
int max_inner_iter = 100; // for inner Newton
y[i] = -1;
}
}
-
+
// Initial alpha can be set here. Note that
// 0 < alpha[i] < upper_bound[GETI(i)]
// alpha[2*i] + alpha[2*i+1] = upper_bound[GETI(i)]
int l, n;
double *y;
struct feature_node **x;
- double bias; /* < 0 if no bias term */
+ double bias; /* < 0 if no bias term */
};
enum { L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL, L2R_L2LOSS_SVR = 11, L2R_L2LOSS_SVR_DUAL, L2R_L1LOSS_SVR_DUAL }; /* solver_type */
../linear.o: ../linear.cpp ../linear.h
make -C .. linear.o
-../tron.o: ../tron.cpp ../tron.h
+../tron.o: ../tron.cpp ../tron.h
make -C .. tron.o
../blas/blas.a: ../blas/*.c ../blas/*.h
#ifdef MX_API_VER
#if MX_API_VER < 0x07030000
typedef int mwIndex;
-#endif
-#endif
+#endif
+#endif
#ifndef max
#define max(x,y) (((x)>(y))?(x):(y))
#endif
static char* readline(FILE *input)
{
int len;
-
+
if(fgets(line,max_line_len,input) == NULL)
return NULL;
if(fp ==NULL)
{
- mexPrintf("can't open output file %s\n",filename);
+ mexPrintf("can't open output file %s\n",filename);
return;
}
low = jc[i], high = jc[i+1];
for(k=low;k<high;k++)
- fprintf(fp," %lu:%g", (size_t)ir[k]+1, samples[k]);
+ fprintf(fp," %lu:%g", (size_t)ir[k]+1, samples[k]);
fprintf(fp,"\n");
}
fake_answer(nlhs, plhs);
return;
}
-
+
// Transform the input Matrix to libsvm format
if(nrhs == 3)
{
char filename[256];
if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2]))
{
- mexPrintf("Error: label vector and instance matrix must be double\n");
+ mexPrintf("Error: label vector and instance matrix must be double\n");
return;
}
-
- mxGetString(prhs[0], filename, mxGetN(prhs[0])+1);
+
+ mxGetString(prhs[0], filename, mxGetN(prhs[0])+1);
if(mxIsSparse(prhs[2]))
libsvmwrite(filename, prhs[1], prhs[2]);
else
{
- mexPrintf("Instance_matrix must be sparse\n");
+ mexPrintf("Instance_matrix must be sparse\n");
return;
}
}
else
{
- exit_with_help();
+ exit_with_help();
return;
}
}
"-s type : set type of solver (default 1)\n"
" for multi-class classification\n"
" 0 -- L2-regularized logistic regression (primal)\n"
- " 1 -- L2-regularized L2-loss support vector classification (dual)\n"
+ " 1 -- L2-regularized L2-loss support vector classification (dual)\n"
" 2 -- L2-regularized L2-loss support vector classification (primal)\n"
" 3 -- L2-regularized L1-loss support vector classification (dual)\n"
" 4 -- support vector classification by Crammer and Singer\n"
"-c cost : set the parameter C (default 1)\n"
"-p epsilon : set the epsilon in loss function of SVR (default 0.1)\n"
"-e epsilon : set tolerance of termination criterion\n"
- " -s 0 and 2\n"
- " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n"
- " where f is the primal function and pos/neg are # of\n"
+ " -s 0 and 2\n"
+ " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n"
+ " where f is the primal function and pos/neg are # of\n"
" positive/negative data (default 0.01)\n"
" -s 11\n"
- " |f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.001)\n"
+ " |f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.001)\n"
" -s 1, 3, 4 and 7\n"
" Dual maximal violation <= eps; similar to libsvm (default 0.1)\n"
" -s 5 and 6\n"
else
start_C = -1.0;
find_parameter_C(&prob, ¶m, nr_fold, start_C, max_C, best_C, best_rate);
- mexPrintf("Best C = %lf CV accuracy = %g%%\n", *best_C, 100.0**best_rate);
+ mexPrintf("Best C = %lf CV accuracy = %g%%\n", *best_C, 100.0**best_rate);
}
double retval = 0.0;
cross_validation(&prob,¶m,nr_fold,target);
- if(param.solver_type == L2R_L2LOSS_SVR ||
- param.solver_type == L2R_L1LOSS_SVR_DUAL ||
+ if(param.solver_type == L2R_L2LOSS_SVR ||
+ param.solver_type == L2R_L1LOSS_SVR_DUAL ||
param.solver_type == L2R_L2LOSS_SVR_DUAL)
{
for(i=0;i<prob.l;i++)
{
switch(param.solver_type)
{
- case L2R_LR:
+ case L2R_LR:
case L2R_L2LOSS_SVC:
param.eps = 0.01;
break;
case L2R_L2LOSS_SVR:
param.eps = 0.001;
break;
- case L2R_L2LOSS_SVC_DUAL:
- case L2R_L1LOSS_SVC_DUAL:
- case MCSVM_CS:
- case L2R_LR_DUAL:
+ case L2R_L2LOSS_SVC_DUAL:
+ case L2R_L1LOSS_SVC_DUAL:
+ case MCSVM_CS:
+ case L2R_LR_DUAL:
param.eps = 0.1;
break;
- case L1R_L2LOSS_SVC:
+ case L1R_L2LOSS_SVC:
case L1R_LR:
param.eps = 0.01;
break;
mexPrintf("Length of label vector does not match # of instances.\n");
return -1;
}
-
+
// each column is one instance
labels = mxGetPr(label_vec);
samples = mxGetPr(instance_mat_col);
if (flag_find_C)
{
double best_C, best_rate, *ptr;
-
- do_find_parameter_C(&best_C, &best_rate);
-
+
+ do_find_parameter_C(&best_C, &best_rate);
+
plhs[0] = mxCreateDoubleMatrix(2, 1, mxREAL);
ptr = mxGetPr(plhs[0]);
ptr[0] = best_C;
Show the index and the value of a node.
- >>> print(node)
+ >>> print(node)
- Function: gen_feature_nodearray(xi [,feature_max=None [,issparse=True]])
x: a Python list/tuple of l data instances. Each element of x must be
an instance of list/tuple/dictionary type.
- bias: if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term
+ bias: if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term
added (default -1)
You can also modify the bias value by
structure. If '-v' is specified, cross validation is
conducted and the returned model is just a scalar: cross-validation
accuracy for classification and mean-squared error for regression.
- If the '-C' option is specified, the best parameter C is found
- by cross validation. The returned model is a tuple of the best C
- and the corresponding cross-validation accuracy. The parameter
+ If the '-C' option is specified, the best parameter C is found
+ by cross validation. The returned model is a tuple of the best C
+ and the corresponding cross-validation accuracy. The parameter
selection utility is supported by only -s 0 and -s 2.
squared error, and squared correlation coefficient (for
regression).
- p_vals: a list of decision values or probability estimates (if '-b 1'
+ p_vals: a list of decision values or probability estimates (if '-b 1'
is specified). If k is the number of classes, for decision values,
each element includes results of predicting k binary-class
- SVMs. If k = 2 and solver is not MCSVM_CS, only one decision value
- is returned. For probabilities, each element contains k values
+ SVMs. If k = 2 and solver is not MCSVM_CS, only one decision value
+ is returned. For probabilities, each element contains k values
indicating the probability that the testing instance is in each class.
Note that the order of classes here is the same as 'model.label'
field in the model structure.
__all__ = ['liblinear', 'feature_node', 'gen_feature_nodearray', 'problem',
'parameter', 'model', 'toPyModel', 'L2R_LR', 'L2R_L2LOSS_SVC_DUAL',
- 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL', 'MCSVM_CS',
- 'L1R_L2LOSS_SVC', 'L1R_LR', 'L2R_LR_DUAL', 'L2R_L2LOSS_SVR',
+ 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL', 'MCSVM_CS',
+ 'L1R_L2LOSS_SVC', 'L1R_LR', 'L2R_LR_DUAL', 'L2R_L2LOSS_SVR',
'L2R_L2LOSS_SVR_DUAL', 'L2R_L1LOSS_SVR_DUAL', 'print_null']
try:
raise Exception('LIBLINEAR library not found.')
L2R_LR = 0
-L2R_L2LOSS_SVC_DUAL = 1
-L2R_L2LOSS_SVC = 2
+L2R_L2LOSS_SVC_DUAL = 1
+L2R_L2LOSS_SVC = 2
L2R_L1LOSS_SVC_DUAL = 3
-MCSVM_CS = 4
-L1R_L2LOSS_SVC = 5
-L1R_LR = 6
-L2R_LR_DUAL = 7
+MCSVM_CS = 4
+L1R_L2LOSS_SVC = 5
+L1R_LR = 6
+L2R_LR_DUAL = 7
L2R_L2LOSS_SVR = 11
L2R_L2LOSS_SVR_DUAL = 12
L2R_L1LOSS_SVR_DUAL = 13
PRINT_STRING_FUN = CFUNCTYPE(None, c_char_p)
-def print_null(s):
- return
+def print_null(s):
+ return
-def genFields(names, types):
+def genFields(names, types):
return list(zip(names, types))
-def fillprototype(f, restype, argtypes):
+def fillprototype(f, restype, argtypes):
f.restype = restype
f.argtypes = argtypes
if feature_max:
assert(isinstance(feature_max, int))
index_range = filter(lambda j: j <= feature_max, index_range)
- if issparse:
+ if issparse:
index_range = filter(lambda j:xi[j] != 0, index_range)
index_range = sorted(index_range)
ret[idx].index = j
ret[idx].value = xi[j]
max_idx = 0
- if index_range :
+ if index_range :
max_idx = index_range[-1]
return ret, max_idx
self.y = (c_double * l)()
for i, yi in enumerate(y): self.y[i] = y[i]
- self.x = (POINTER(feature_node) * l)()
+ self.x = (POINTER(feature_node) * l)()
for i, xi in enumerate(self.x_space): self.x[i] = xi
self.set_bias(bias)
def set_bias(self, bias):
if self.bias == bias:
- return
- if bias >= 0 and self.bias < 0:
+ return
+ if bias >= 0 and self.bias < 0:
self.n += 1
node = feature_node(self.n, bias)
- if bias < 0 and self.bias >= 0:
+ if bias < 0 and self.bias >= 0:
self.n -= 1
node = feature_node(-1, bias)
def __str__(self):
s = ''
attrs = parameter._names + list(self.__dict__.keys())
- values = map(lambda attr: getattr(self, attr), attrs)
+ values = map(lambda attr: getattr(self, attr), attrs)
for attr, val in zip(attrs, values):
s += (' %s: %s\n' % (attr, val))
s = s.strip()
liblinear.set_print_string_function(self.print_func)
self.weight_label = (c_int*self.nr_weight)()
self.weight = (c_double*self.nr_weight)()
- for i in range(self.nr_weight):
+ for i in range(self.nr_weight):
self.weight[i] = weight[i]
self.weight_label[i] = weight_label[i]
#!/usr/bin/env python
import os, sys
-sys.path = [os.path.dirname(os.path.abspath(__file__))] + sys.path
+sys.path = [os.path.dirname(os.path.abspath(__file__))] + sys.path
from liblinear import *
from liblinear import __all__ as liblinear_all
from ctypes import c_double
if param.flag_find_C:
nr_fold = param.nr_fold
best_C = c_double()
- best_rate = c_double()
+ best_rate = c_double()
max_C = 1024
if param.flag_C_specified:
start_C = param.C