Aluode/PerceptionLabPortable
0
1"""
2Wrapper for liblinear
3
4Author: fabian.pedregosa@inria.fr
5"""
6
7import numpy as np
8
9from ..utils._cython_blas cimport _dot, _axpy, _scal, _nrm2
10from ..utils._typedefs cimport float32_t, float64_t, int32_t
11
12include "_liblinear.pxi"
13
14
15def train_wrap(
16 object X,
17 const float64_t[::1] Y,
18 bint is_sparse,
19 int solver_type,
20 double eps,
21 double bias,
22 double C,
23 const float64_t[:] class_weight,
24 int max_iter,
25 unsigned random_seed,
26 double epsilon,
27 const float64_t[::1] sample_weight
28):
29 cdef parameter *param
30 cdef problem *problem
31 cdef model *model
32 cdef char_const_ptr error_msg
33 cdef int len_w
34 cdef bint X_has_type_float64 = X.dtype == np.float64
35 cdef char * X_data_bytes_ptr
36 cdef const float64_t[::1] X_data_64
37 cdef const float32_t[::1] X_data_32
38 cdef const int32_t[::1] X_indices
39 cdef const int32_t[::1] X_indptr
40
41 if is_sparse:
42 X_indices = X.indices
43 X_indptr = X.indptr
44 if X_has_type_float64:
45 X_data_64 = X.data
46 X_data_bytes_ptr = <char *> &X_data_64[0]
47 else:
48 X_data_32 = X.data
49 X_data_bytes_ptr = <char *> &X_data_32[0]
50
51 problem = csr_set_problem(
52 X_data_bytes_ptr,
53 X_has_type_float64,
54 <char *> &X_indices[0],
55 <char *> &X_indptr[0],
56 (<int32_t>X.shape[0]),
57 (<int32_t>X.shape[1]),
58 (<int32_t>X.nnz),
59 bias,
60 <char *> &sample_weight[0],
61 <char *> &Y[0]
62 )
63 else:
64 X_as_1d_array = X.reshape(-1)
65 if X_has_type_float64:
66 X_data_64 = X_as_1d_array
67 X_data_bytes_ptr = <char *> &X_data_64[0]
68 else:
69 X_data_32 = X_as_1d_array
70 X_data_bytes_ptr = <char *> &X_data_32[0]
71
72 problem = set_problem(
73 X_data_bytes_ptr,
74 X_has_type_float64,
75 (<int32_t>X.shape[0]),
76 (<int32_t>X.shape[1]),
77 (<int32_t>np.count_nonzero(X)),
78 bias,
79 <char *> &sample_weight[0],
80 <char *> &Y[0]
81 )
82
83 cdef int32_t[::1] class_weight_label = np.arange(class_weight.shape[0], dtype=np.intc)
84 param = set_parameter(
85 solver_type,
86 eps,
87 C,
88 class_weight.shape[0],
89 <char *> &class_weight_label[0] if class_weight_label.size > 0 else NULL,
90 <char *> &class_weight[0] if class_weight.size > 0 else NULL,
91 max_iter,
92 random_seed,
93 epsilon
94 )
95
96 error_msg = check_parameter(problem, param)
97 if error_msg:
98 free_problem(problem)
99 free_parameter(param)
100 raise ValueError(error_msg)
101
102 cdef BlasFunctions blas_functions
103 blas_functions.dot = _dot[double]
104 blas_functions.axpy = _axpy[double]
105 blas_functions.scal = _scal[double]
106 blas_functions.nrm2 = _nrm2[double]
107
108 # early return
109 with nogil:
110 model = train(problem, param, &blas_functions)
111
112 # FREE
113 free_problem(problem)
114 free_parameter(param)
115 # destroy_param(param) don't call this or it will destroy class_weight_label and class_weight
116
117 # coef matrix holder created as fortran since that's what's used in liblinear
118 cdef float64_t[::1, :] w
119 cdef int nr_class = get_nr_class(model)
120
121 cdef int labels_ = nr_class
122 if nr_class == 2:
123 labels_ = 1
124 cdef int32_t[::1] n_iter = np.zeros(labels_, dtype=np.intc)
125 get_n_iter(model, <int *> &n_iter[0])
126
127 cdef int nr_feature = get_nr_feature(model)
128 if bias > 0:
129 nr_feature = nr_feature + 1
130 if nr_class == 2 and solver_type != 4: # solver is not Crammer-Singer
131 w = np.empty((1, nr_feature), order='F')
132 copy_w(&w[0, 0], model, nr_feature)
133 else:
134 len_w = (nr_class) * nr_feature
135 w = np.empty((nr_class, nr_feature), order='F')
136 copy_w(&w[0, 0], model, len_w)
137
138 free_and_destroy_model(&model)
139
140 return w.base, n_iter.base
141
142
143def set_verbosity_wrap(int verbosity):
144 """
145 Control verbosity of libsvm library
146 """
147 set_verbosity(verbosity)
148 