2010-04-19 18:33:30 +08:00
|
|
|
"""
|
|
|
|
|
Wrapper for liblinear
|
|
|
|
|
|
|
|
|
|
Author: fabian.pedregosa@inria.fr
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
cimport numpy as np
|
2019-02-22 02:44:05 +08:00
|
|
|
|
|
|
|
|
from ..utils._cython_blas cimport _dot, _axpy, _scal, _nrm2
|
|
|
|
|
|
2019-11-27 00:37:26 +08:00
|
|
|
include "_liblinear.pxi"
|
|
|
|
|
|
2013-01-08 01:33:04 +08:00
|
|
|
np.import_array()
|
|
|
|
|
|
2010-04-19 18:33:30 +08:00
|
|
|
|
2015-10-20 02:46:15 +08:00
|
|
|
def train_wrap(X, np.ndarray[np.float64_t, ndim=1, mode='c'] Y,
|
2013-04-19 12:52:14 +08:00
|
|
|
bint is_sparse, int solver_type, double eps, double bias,
|
|
|
|
|
double C, np.ndarray[np.float64_t, ndim=1] class_weight,
|
2015-09-16 03:42:09 +08:00
|
|
|
int max_iter, unsigned random_seed, double epsilon,
|
2015-10-20 02:46:15 +08:00
|
|
|
np.ndarray[np.float64_t, ndim=1, mode='c'] sample_weight):
|
2010-04-19 18:33:30 +08:00
|
|
|
cdef parameter *param
|
|
|
|
|
cdef problem *problem
|
|
|
|
|
cdef model *model
|
2012-09-12 22:00:55 +08:00
|
|
|
cdef char_const_ptr error_msg
|
2010-04-19 21:37:51 +08:00
|
|
|
cdef int len_w
|
2010-04-19 18:33:30 +08:00
|
|
|
|
2013-04-19 12:52:14 +08:00
|
|
|
if is_sparse:
|
|
|
|
|
problem = csr_set_problem(
|
2019-07-31 19:13:28 +08:00
|
|
|
(<np.ndarray>X.data).data, X.dtype == np.float64,
|
2013-04-19 12:52:14 +08:00
|
|
|
(<np.ndarray[np.int32_t, ndim=1, mode='c']>X.indices).data,
|
|
|
|
|
(<np.ndarray[np.int32_t, ndim=1, mode='c']>X.indptr).data,
|
2019-07-31 19:13:28 +08:00
|
|
|
(<np.int32_t>X.shape[0]), (<np.int32_t>X.shape[1]),
|
|
|
|
|
(<np.int32_t>X.nnz), bias, sample_weight.data, Y.data)
|
2013-04-19 12:52:14 +08:00
|
|
|
else:
|
|
|
|
|
problem = set_problem(
|
2019-07-31 19:13:28 +08:00
|
|
|
(<np.ndarray>X).data, X.dtype == np.float64,
|
|
|
|
|
(<np.int32_t>X.shape[0]), (<np.int32_t>X.shape[1]),
|
|
|
|
|
(<np.int32_t>np.count_nonzero(X)), bias, sample_weight.data,
|
|
|
|
|
Y.data)
|
2010-04-19 18:33:30 +08:00
|
|
|
|
2012-12-12 16:31:44 +08:00
|
|
|
cdef np.ndarray[np.int32_t, ndim=1, mode='c'] \
|
2014-08-01 07:01:13 +08:00
|
|
|
class_weight_label = np.arange(class_weight.shape[0], dtype=np.intc)
|
2012-12-12 16:31:44 +08:00
|
|
|
param = set_parameter(solver_type, eps, C, class_weight.shape[0],
|
2013-04-19 12:52:14 +08:00
|
|
|
class_weight_label.data, class_weight.data,
|
2014-11-24 23:47:03 +08:00
|
|
|
max_iter, random_seed, epsilon)
|
2010-04-19 18:33:30 +08:00
|
|
|
|
|
|
|
|
error_msg = check_parameter(problem, param)
|
|
|
|
|
if error_msg:
|
|
|
|
|
free_problem(problem)
|
|
|
|
|
free_parameter(param)
|
|
|
|
|
raise ValueError(error_msg)
|
2019-02-22 02:44:05 +08:00
|
|
|
|
|
|
|
|
cdef BlasFunctions blas_functions
|
|
|
|
|
blas_functions.dot = _dot[double]
|
|
|
|
|
blas_functions.axpy = _axpy[double]
|
|
|
|
|
blas_functions.scal = _scal[double]
|
|
|
|
|
blas_functions.nrm2 = _nrm2[double]
|
2012-12-25 02:19:37 +08:00
|
|
|
|
2010-04-19 18:33:30 +08:00
|
|
|
# early return
|
2013-04-14 08:38:43 +08:00
|
|
|
with nogil:
|
2019-02-22 02:44:05 +08:00
|
|
|
model = train(problem, param, &blas_functions)
|
2010-04-19 18:33:30 +08:00
|
|
|
|
2019-05-29 00:34:45 +08:00
|
|
|
### FREE
|
|
|
|
|
free_problem(problem)
|
|
|
|
|
free_parameter(param)
|
|
|
|
|
# destroy_param(param) don't call this or it will destroy class_weight_label and class_weight
|
|
|
|
|
|
2011-02-06 02:27:03 +08:00
|
|
|
# coef matrix holder created as fortran since that's what's used in liblinear
|
2012-12-25 02:19:37 +08:00
|
|
|
cdef np.ndarray[np.float64_t, ndim=2, mode='fortran'] w
|
2010-04-19 18:33:30 +08:00
|
|
|
cdef int nr_class = get_nr_class(model)
|
2014-07-31 23:32:55 +08:00
|
|
|
|
|
|
|
|
cdef int labels_ = nr_class
|
|
|
|
|
if nr_class == 2:
|
|
|
|
|
labels_ = 1
|
2014-08-01 07:01:13 +08:00
|
|
|
cdef np.ndarray[np.int32_t, ndim=1, mode='c'] n_iter = np.zeros(labels_, dtype=np.intc)
|
2014-07-31 23:32:55 +08:00
|
|
|
get_n_iter(model, <int *>n_iter.data)
|
|
|
|
|
|
2010-04-19 18:33:30 +08:00
|
|
|
cdef int nr_feature = get_nr_feature(model)
|
2010-04-19 21:24:23 +08:00
|
|
|
if bias > 0: nr_feature = nr_feature + 1
|
2013-08-01 21:14:19 +08:00
|
|
|
if nr_class == 2 and solver_type != 4: # solver is not Crammer-Singer
|
2011-02-04 21:23:44 +08:00
|
|
|
w = np.empty((1, nr_feature),order='F')
|
2011-02-04 22:03:31 +08:00
|
|
|
copy_w(w.data, model, nr_feature)
|
2010-04-19 21:37:51 +08:00
|
|
|
else:
|
2010-08-19 19:06:02 +08:00
|
|
|
len_w = (nr_class) * nr_feature
|
2012-12-25 02:19:37 +08:00
|
|
|
w = np.empty((nr_class, nr_feature),order='F')
|
2011-02-04 22:03:31 +08:00
|
|
|
copy_w(w.data, model, len_w)
|
2010-04-19 21:24:23 +08:00
|
|
|
|
2010-10-22 16:05:30 +08:00
|
|
|
free_and_destroy_model(&model)
|
2010-04-19 18:33:30 +08:00
|
|
|
|
2014-07-31 23:32:55 +08:00
|
|
|
return w, n_iter
|
2010-04-19 18:33:30 +08:00
|
|
|
|
2012-01-17 06:15:45 +08:00
|
|
|
|
2012-04-13 04:27:02 +08:00
|
|
|
def set_verbosity_wrap(int verbosity):
|
|
|
|
|
"""
|
|
|
|
|
Control verbosity of libsvm library
|
|
|
|
|
"""
|
|
|
|
|
set_verbosity(verbosity)
|