forked from scikit-learn/scikit-learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_libsvm.pyx
917 lines (763 loc) · 26 KB
/
_libsvm.pyx
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
"""
Binding for libsvm_skl
----------------------
These are the bindings for libsvm_skl, which is a fork of libsvm[1]
that adds to libsvm some capabilities, like index of support vectors
and efficient representation of dense matrices.
These are low-level routines, but can be used for flexibility or
performance reasons. See sklearn.svm for a higher-level API.
Low-level memory management is done in libsvm_helper.c. If we happen
to run out of memory a MemoryError will be raised. In practice this is
not very helpful since high chances are malloc fails inside svm.cpp,
where no sort of memory checks are done.
[1] https://www.csie.ntu.edu.tw/~cjlin/libsvm/
Notes
-----
The signature mode='c' is somewhat superficial, since we already
check that arrays are C-contiguous in svm.py
Authors
-------
2010: Fabian Pedregosa <[email protected]>
Gael Varoquaux <[email protected]>
"""
import numpy as np
from libc.stdlib cimport free
from ..utils._cython_blas cimport _dot
from ..utils._typedefs cimport float64_t, int32_t, intp_t
include "_libsvm.pxi"
cdef extern from *:
ctypedef struct svm_parameter:
pass
################################################################################
# Internal variables
LIBSVM_KERNEL_TYPES = ['linear', 'poly', 'rbf', 'sigmoid', 'precomputed']
################################################################################
# Wrapper functions
def fit(
const float64_t[:, ::1] X,
const float64_t[::1] Y,
int svm_type=0,
kernel='rbf',
int degree=3,
double gamma=0.1,
double coef0=0.0,
double tol=1e-3,
double C=1.0,
double nu=0.5,
double epsilon=0.1,
const float64_t[::1] class_weight=np.empty(0),
const float64_t[::1] sample_weight=np.empty(0),
int shrinking=1,
int probability=0,
double cache_size=100.,
int max_iter=-1,
int random_seed=0,
):
"""
Train the model using libsvm (low-level method)
Parameters
----------
X : array-like, dtype=float64 of shape (n_samples, n_features)
Y : array, dtype=float64 of shape (n_samples,)
target vector
svm_type : {0, 1, 2, 3, 4}, default=0
Type of SVM: C_SVC, NuSVC, OneClassSVM, EpsilonSVR or NuSVR
respectively.
kernel : {'linear', 'rbf', 'poly', 'sigmoid', 'precomputed'}, default="rbf"
Kernel to use in the model: linear, polynomial, RBF, sigmoid
or precomputed.
degree : int32, default=3
Degree of the polynomial kernel (only relevant if kernel is
set to polynomial).
gamma : float64, default=0.1
Gamma parameter in rbf, poly and sigmoid kernels. Ignored by other
kernels.
coef0 : float64, default=0
Independent parameter in poly/sigmoid kernel.
tol : float64, default=1e-3
Numeric stopping criterion (WRITEME).
C : float64, default=1
C parameter in C-Support Vector Classification.
nu : float64, default=0.5
An upper bound on the fraction of training errors and a lower bound of
the fraction of support vectors. Should be in the interval (0, 1].
epsilon : double, default=0.1
Epsilon parameter in the epsilon-insensitive loss function.
class_weight : array, dtype=float64, shape (n_classes,), \
default=np.empty(0)
Set the parameter C of class i to class_weight[i]*C for
SVC. If not given, all classes are supposed to have
weight one.
sample_weight : array, dtype=float64, shape (n_samples,), \
default=np.empty(0)
Weights assigned to each sample.
shrinking : int, default=1
Whether to use the shrinking heuristic.
probability : int, default=0
Whether to enable probability estimates.
cache_size : float64, default=100
Cache size for gram matrix columns (in megabytes).
max_iter : int (-1 for no limit), default=-1
Stop solver after this many iterations regardless of accuracy
(XXX Currently there is no API to know whether this kicked in.)
random_seed : int, default=0
Seed for the random number generator used for probability estimates.
Returns
-------
support : array of shape (n_support,)
Index of support vectors.
support_vectors : array of shape (n_support, n_features)
Support vectors (equivalent to X[support]). Will return an
empty array in the case of precomputed kernel.
n_class_SV : array of shape (n_class,)
Number of support vectors in each class.
sv_coef : array of shape (n_class-1, n_support)
Coefficients of support vectors in decision function.
intercept : array of shape (n_class*(n_class-1)/2,)
Intercept in decision function.
probA, probB : array of shape (n_class*(n_class-1)/2,)
Probability estimates, empty array for probability=False.
n_iter : ndarray of shape (max(1, (n_class * (n_class - 1) // 2)),)
Number of iterations run by the optimization routine to fit the model.
"""
cdef svm_parameter param
cdef svm_problem problem
cdef svm_model *model
cdef const char *error_msg
cdef intp_t SV_len
if len(sample_weight) == 0:
sample_weight = np.ones(X.shape[0], dtype=np.float64)
else:
assert sample_weight.shape[0] == X.shape[0], (
f"sample_weight and X have incompatible shapes: sample_weight has "
f"{sample_weight.shape[0]} samples while X has {X.shape[0]}"
)
kernel_index = LIBSVM_KERNEL_TYPES.index(kernel)
set_problem(
&problem,
<char*> &X[0, 0],
<char*> &Y[0],
<char*> &sample_weight[0],
<intp_t*> X.shape,
kernel_index,
)
if problem.x == NULL:
raise MemoryError("Seems we've run out of memory")
cdef int32_t[::1] class_weight_label = np.arange(
class_weight.shape[0], dtype=np.int32
)
set_parameter(
¶m,
svm_type,
kernel_index,
degree,
gamma,
coef0,
nu,
cache_size,
C,
tol,
epsilon,
shrinking,
probability,
<int> class_weight.shape[0],
<char*> &class_weight_label[0] if class_weight_label.size > 0 else NULL,
<char*> &class_weight[0] if class_weight.size > 0 else NULL,
max_iter,
random_seed,
)
error_msg = svm_check_parameter(&problem, ¶m)
if error_msg:
# for SVR: epsilon is called p in libsvm
error_repl = error_msg.decode('utf-8').replace("p < 0", "epsilon < 0")
raise ValueError(error_repl)
cdef BlasFunctions blas_functions
blas_functions.dot = _dot[double]
# this does the real work
cdef int fit_status = 0
with nogil:
model = svm_train(&problem, ¶m, &fit_status, &blas_functions)
# from here until the end, we just copy the data returned by
# svm_train
SV_len = get_l(model)
n_class = get_nr(model)
cdef int[::1] n_iter = np.empty(max(1, n_class * (n_class - 1) // 2), dtype=np.intc)
copy_n_iter(<char*> &n_iter[0], model)
cdef float64_t[:, ::1] sv_coef = np.empty((n_class-1, SV_len), dtype=np.float64)
copy_sv_coef(<char*> &sv_coef[0, 0] if sv_coef.size > 0 else NULL, model)
# the intercept is just model.rho but with sign changed
cdef float64_t[::1] intercept = np.empty(
int((n_class*(n_class-1))/2), dtype=np.float64
)
copy_intercept(<char*> &intercept[0], model, <intp_t*> intercept.shape)
cdef int32_t[::1] support = np.empty(SV_len, dtype=np.int32)
copy_support(<char*> &support[0] if support.size > 0 else NULL, model)
# copy model.SV
cdef float64_t[:, ::1] support_vectors
if kernel_index == 4:
# precomputed kernel
support_vectors = np.empty((0, 0), dtype=np.float64)
else:
support_vectors = np.empty((SV_len, X.shape[1]), dtype=np.float64)
copy_SV(
<char*> &support_vectors[0, 0] if support_vectors.size > 0 else NULL,
model,
<intp_t*> support_vectors.shape,
)
cdef int32_t[::1] n_class_SV
if svm_type == 0 or svm_type == 1:
n_class_SV = np.empty(n_class, dtype=np.int32)
copy_nSV(<char*> &n_class_SV[0] if n_class_SV.size > 0 else NULL, model)
else:
# OneClass and SVR are considered to have 2 classes
n_class_SV = np.array([SV_len, SV_len], dtype=np.int32)
cdef float64_t[::1] probA
cdef float64_t[::1] probB
if probability != 0:
if svm_type < 2: # SVC and NuSVC
probA = np.empty(int(n_class*(n_class-1)/2), dtype=np.float64)
probB = np.empty(int(n_class*(n_class-1)/2), dtype=np.float64)
copy_probB(<char*> &probB[0], model, <intp_t*> probB.shape)
else:
probA = np.empty(1, dtype=np.float64)
probB = np.empty(0, dtype=np.float64)
copy_probA(<char*> &probA[0], model, <intp_t*> probA.shape)
else:
probA = np.empty(0, dtype=np.float64)
probB = np.empty(0, dtype=np.float64)
svm_free_and_destroy_model(&model)
free(problem.x)
return (
support.base,
support_vectors.base,
n_class_SV.base,
sv_coef.base,
intercept.base,
probA.base,
probB.base,
fit_status,
n_iter.base,
)
cdef void set_predict_params(
svm_parameter *param,
int svm_type,
kernel,
int degree,
double gamma,
double coef0,
double cache_size,
int probability,
int nr_weight,
char *weight_label,
char *weight,
) except *:
"""Fill param with prediction time-only parameters."""
# training-time only parameters
cdef double C = 0.0
cdef double epsilon = 0.1
cdef int max_iter = 0
cdef double nu = 0.5
cdef int shrinking = 0
cdef double tol = 0.1
cdef int random_seed = -1
kernel_index = LIBSVM_KERNEL_TYPES.index(kernel)
set_parameter(
param,
svm_type,
kernel_index,
degree,
gamma,
coef0,
nu,
cache_size,
C,
tol,
epsilon,
shrinking,
probability,
nr_weight,
weight_label,
weight,
max_iter,
random_seed,
)
def predict(
const float64_t[:, ::1] X,
const int32_t[::1] support,
const float64_t[:, ::1] SV,
const int32_t[::1] nSV,
const float64_t[:, ::1] sv_coef,
const float64_t[::1] intercept,
const float64_t[::1] probA=np.empty(0),
const float64_t[::1] probB=np.empty(0),
int svm_type=0,
kernel='rbf',
int degree=3,
double gamma=0.1,
double coef0=0.0,
const float64_t[::1] class_weight=np.empty(0),
const float64_t[::1] sample_weight=np.empty(0),
double cache_size=100.0,
):
"""
Predict target values of X given a model (low-level method)
Parameters
----------
X : array-like, dtype=float of shape (n_samples, n_features)
support : array of shape (n_support,)
Index of support vectors in training set.
SV : array of shape (n_support, n_features)
Support vectors.
nSV : array of shape (n_class,)
Number of support vectors in each class.
sv_coef : array of shape (n_class-1, n_support)
Coefficients of support vectors in decision function.
intercept : array of shape (n_class*(n_class-1)/2)
Intercept in decision function.
probA, probB : array of shape (n_class*(n_class-1)/2,)
Probability estimates.
svm_type : {0, 1, 2, 3, 4}, default=0
Type of SVM: C_SVC, NuSVC, OneClassSVM, EpsilonSVR or NuSVR
respectively.
kernel : {'linear', 'rbf', 'poly', 'sigmoid', 'precomputed'}, default="rbf"
Kernel to use in the model: linear, polynomial, RBF, sigmoid
or precomputed.
degree : int32, default=3
Degree of the polynomial kernel (only relevant if kernel is
set to polynomial).
gamma : float64, default=0.1
Gamma parameter in rbf, poly and sigmoid kernels. Ignored by other
kernels.
coef0 : float64, default=0.0
Independent parameter in poly/sigmoid kernel.
Returns
-------
dec_values : array
Predicted values.
"""
cdef float64_t[::1] dec_values
cdef svm_parameter param
cdef svm_model *model
cdef int rv
cdef int32_t[::1] class_weight_label = np.arange(
class_weight.shape[0], dtype=np.int32
)
set_predict_params(
¶m,
svm_type,
kernel,
degree,
gamma,
coef0,
cache_size,
0,
<int>class_weight.shape[0],
<char*> &class_weight_label[0] if class_weight_label.size > 0 else NULL,
<char*> &class_weight[0] if class_weight.size > 0 else NULL,
)
model = set_model(
¶m,
<int> nSV.shape[0],
<char*> &SV[0, 0] if SV.size > 0 else NULL,
<intp_t*> SV.shape,
<char*> &support[0] if support.size > 0 else NULL,
<intp_t*> support.shape,
<intp_t*> sv_coef.strides,
<char*> &sv_coef[0, 0] if sv_coef.size > 0 else NULL,
<char*> &intercept[0],
<char*> &nSV[0],
<char*> &probA[0] if probA.size > 0 else NULL,
<char*> &probB[0] if probB.size > 0 else NULL,
)
cdef BlasFunctions blas_functions
blas_functions.dot = _dot[double]
# TODO: use check_model
try:
dec_values = np.empty(X.shape[0])
with nogil:
rv = copy_predict(
<char*> &X[0, 0],
model,
<intp_t*> X.shape,
<char*> &dec_values[0],
&blas_functions,
)
if rv < 0:
raise MemoryError("We've run out of memory")
finally:
free_model(model)
return dec_values.base
def predict_proba(
const float64_t[:, ::1] X,
const int32_t[::1] support,
const float64_t[:, ::1] SV,
const int32_t[::1] nSV,
float64_t[:, ::1] sv_coef,
float64_t[::1] intercept,
float64_t[::1] probA=np.empty(0),
float64_t[::1] probB=np.empty(0),
int svm_type=0,
kernel='rbf',
int degree=3,
double gamma=0.1,
double coef0=0.0,
float64_t[::1] class_weight=np.empty(0),
float64_t[::1] sample_weight=np.empty(0),
double cache_size=100.0,
):
"""
Predict probabilities
svm_model stores all parameters needed to predict a given value.
For speed, all real work is done at the C level in function
copy_predict (libsvm_helper.c).
We have to reconstruct model and parameters to make sure we stay
in sync with the python object.
See sklearn.svm.predict for a complete list of parameters.
Parameters
----------
X : array-like, dtype=float of shape (n_samples, n_features)
support : array of shape (n_support,)
Index of support vectors in training set.
SV : array of shape (n_support, n_features)
Support vectors.
nSV : array of shape (n_class,)
Number of support vectors in each class.
sv_coef : array of shape (n_class-1, n_support)
Coefficients of support vectors in decision function.
intercept : array of shape (n_class*(n_class-1)/2,)
Intercept in decision function.
probA, probB : array of shape (n_class*(n_class-1)/2,)
Probability estimates.
svm_type : {0, 1, 2, 3, 4}, default=0
Type of SVM: C_SVC, NuSVC, OneClassSVM, EpsilonSVR or NuSVR
respectively.
kernel : {'linear', 'rbf', 'poly', 'sigmoid', 'precomputed'}, default="rbf"
Kernel to use in the model: linear, polynomial, RBF, sigmoid
or precomputed.
degree : int32, default=3
Degree of the polynomial kernel (only relevant if kernel is
set to polynomial).
gamma : float64, default=0.1
Gamma parameter in rbf, poly and sigmoid kernels. Ignored by other
kernels.
coef0 : float64, default=0.0
Independent parameter in poly/sigmoid kernel.
Returns
-------
dec_values : array
Predicted values.
"""
cdef float64_t[:, ::1] dec_values
cdef svm_parameter param
cdef svm_model *model
cdef int32_t[::1] class_weight_label = np.arange(
class_weight.shape[0], dtype=np.int32
)
cdef int rv
set_predict_params(
¶m,
svm_type,
kernel,
degree,
gamma,
coef0,
cache_size,
1,
<int> class_weight.shape[0],
<char*> &class_weight_label[0] if class_weight_label.size > 0 else NULL,
<char*> &class_weight[0] if class_weight.size > 0 else NULL,
)
model = set_model(
¶m,
<int> nSV.shape[0],
<char*> &SV[0, 0] if SV.size > 0 else NULL,
<intp_t*> SV.shape,
<char*> &support[0],
<intp_t*> support.shape,
<intp_t*> sv_coef.strides,
<char*> &sv_coef[0, 0],
<char*> &intercept[0],
<char*> &nSV[0],
<char*> &probA[0] if probA.size > 0 else NULL,
<char*> &probB[0] if probB.size > 0 else NULL,
)
cdef intp_t n_class = get_nr(model)
cdef BlasFunctions blas_functions
blas_functions.dot = _dot[double]
try:
dec_values = np.empty((X.shape[0], n_class), dtype=np.float64)
with nogil:
rv = copy_predict_proba(
<char*> &X[0, 0],
model,
<intp_t*> X.shape,
<char*> &dec_values[0, 0],
&blas_functions,
)
if rv < 0:
raise MemoryError("We've run out of memory")
finally:
free_model(model)
return dec_values.base
def decision_function(
const float64_t[:, ::1] X,
const int32_t[::1] support,
const float64_t[:, ::1] SV,
const int32_t[::1] nSV,
const float64_t[:, ::1] sv_coef,
const float64_t[::1] intercept,
const float64_t[::1] probA=np.empty(0),
const float64_t[::1] probB=np.empty(0),
int svm_type=0,
kernel='rbf',
int degree=3,
double gamma=0.1,
double coef0=0.0,
const float64_t[::1] class_weight=np.empty(0),
const float64_t[::1] sample_weight=np.empty(0),
double cache_size=100.0,
):
"""
Predict margin (libsvm name for this is predict_values)
We have to reconstruct model and parameters to make sure we stay
in sync with the python object.
Parameters
----------
X : array-like, dtype=float, size=[n_samples, n_features]
support : array, shape=[n_support]
Index of support vectors in training set.
SV : array, shape=[n_support, n_features]
Support vectors.
nSV : array, shape=[n_class]
Number of support vectors in each class.
sv_coef : array, shape=[n_class-1, n_support]
Coefficients of support vectors in decision function.
intercept : array, shape=[n_class*(n_class-1)/2]
Intercept in decision function.
probA, probB : array, shape=[n_class*(n_class-1)/2]
Probability estimates.
svm_type : {0, 1, 2, 3, 4}, optional
Type of SVM: C_SVC, NuSVC, OneClassSVM, EpsilonSVR or NuSVR
respectively. 0 by default.
kernel : {'linear', 'rbf', 'poly', 'sigmoid', 'precomputed'}, optional
Kernel to use in the model: linear, polynomial, RBF, sigmoid
or precomputed. 'rbf' by default.
degree : int32, optional
Degree of the polynomial kernel (only relevant if kernel is
set to polynomial), 3 by default.
gamma : float64, optional
Gamma parameter in rbf, poly and sigmoid kernels. Ignored by other
kernels. 0.1 by default.
coef0 : float64, optional
Independent parameter in poly/sigmoid kernel. 0 by default.
Returns
-------
dec_values : array
Predicted values.
"""
cdef float64_t[:, ::1] dec_values
cdef svm_parameter param
cdef svm_model *model
cdef intp_t n_class
cdef int32_t[::1] class_weight_label = np.arange(
class_weight.shape[0], dtype=np.int32
)
cdef int rv
set_predict_params(
¶m,
svm_type,
kernel,
degree,
gamma,
coef0,
cache_size,
0,
<int> class_weight.shape[0],
<char*> &class_weight_label[0] if class_weight_label.size > 0 else NULL,
<char*> &class_weight[0] if class_weight.size > 0 else NULL,
)
model = set_model(
¶m,
<int> nSV.shape[0],
<char*> &SV[0, 0] if SV.size > 0 else NULL,
<intp_t*> SV.shape,
<char*> &support[0],
<intp_t*> support.shape,
<intp_t*> sv_coef.strides,
<char*> &sv_coef[0, 0],
<char*> &intercept[0],
<char*> &nSV[0],
<char*> &probA[0] if probA.size > 0 else NULL,
<char*> &probB[0] if probB.size > 0 else NULL,
)
if svm_type > 1:
n_class = 1
else:
n_class = get_nr(model)
n_class = n_class * (n_class - 1) // 2
cdef BlasFunctions blas_functions
blas_functions.dot = _dot[double]
try:
dec_values = np.empty((X.shape[0], n_class), dtype=np.float64)
with nogil:
rv = copy_predict_values(
<char*> &X[0, 0],
model,
<intp_t*> X.shape,
<char*> &dec_values[0, 0],
n_class,
&blas_functions,
)
if rv < 0:
raise MemoryError("We've run out of memory")
finally:
free_model(model)
return dec_values.base
def cross_validation(
const float64_t[:, ::1] X,
const float64_t[::1] Y,
int n_fold,
int svm_type=0,
kernel='rbf',
int degree=3,
double gamma=0.1,
double coef0=0.0,
double tol=1e-3,
double C=1.0,
double nu=0.5,
double epsilon=0.1,
float64_t[::1] class_weight=np.empty(0),
float64_t[::1] sample_weight=np.empty(0),
int shrinking=0,
int probability=0,
double cache_size=100.0,
int max_iter=-1,
int random_seed=0,
):
"""
Binding of the cross-validation routine (low-level routine)
Parameters
----------
X : array-like, dtype=float of shape (n_samples, n_features)
Y : array, dtype=float of shape (n_samples,)
target vector
n_fold : int32
Number of folds for cross validation.
svm_type : {0, 1, 2, 3, 4}, default=0
Type of SVM: C_SVC, NuSVC, OneClassSVM, EpsilonSVR or NuSVR
respectively.
kernel : {'linear', 'rbf', 'poly', 'sigmoid', 'precomputed'}, default='rbf'
Kernel to use in the model: linear, polynomial, RBF, sigmoid
or precomputed.
degree : int32, default=3
Degree of the polynomial kernel (only relevant if kernel is
set to polynomial).
gamma : float64, default=0.1
Gamma parameter in rbf, poly and sigmoid kernels. Ignored by other
kernels.
coef0 : float64, default=0.0
Independent parameter in poly/sigmoid kernel.
tol : float64, default=1e-3
Numeric stopping criterion (WRITEME).
C : float64, default=1
C parameter in C-Support Vector Classification.
nu : float64, default=0.5
An upper bound on the fraction of training errors and a lower bound of
the fraction of support vectors. Should be in the interval (0, 1].
epsilon : double, default=0.1
Epsilon parameter in the epsilon-insensitive loss function.
class_weight : array, dtype=float64, shape (n_classes,), \
default=np.empty(0)
Set the parameter C of class i to class_weight[i]*C for
SVC. If not given, all classes are supposed to have
weight one.
sample_weight : array, dtype=float64, shape (n_samples,), \
default=np.empty(0)
Weights assigned to each sample.
shrinking : int, default=1
Whether to use the shrinking heuristic.
probability : int, default=0
Whether to enable probability estimates.
cache_size : float64, default=100
Cache size for gram matrix columns (in megabytes).
max_iter : int (-1 for no limit), default=-1
Stop solver after this many iterations regardless of accuracy
(XXX Currently there is no API to know whether this kicked in.)
random_seed : int, default=0
Seed for the random number generator used for probability estimates.
Returns
-------
target : array, float
"""
cdef svm_parameter param
cdef svm_problem problem
cdef const char *error_msg
if len(sample_weight) == 0:
sample_weight = np.ones(X.shape[0], dtype=np.float64)
else:
assert sample_weight.shape[0] == X.shape[0], (
f"sample_weight and X have incompatible shapes: sample_weight has "
f"{sample_weight.shape[0]} samples while X has {X.shape[0]}"
)
if X.shape[0] < n_fold:
raise ValueError("Number of samples is less than number of folds")
# set problem
kernel_index = LIBSVM_KERNEL_TYPES.index(kernel)
set_problem(
&problem,
<char*> &X[0, 0],
<char*> &Y[0],
<char*> &sample_weight[0] if sample_weight.size > 0 else NULL,
<intp_t*> X.shape,
kernel_index,
)
if problem.x == NULL:
raise MemoryError("Seems we've run out of memory")
cdef int32_t[::1] class_weight_label = np.arange(
class_weight.shape[0], dtype=np.int32
)
# set parameters
set_parameter(
¶m,
svm_type,
kernel_index,
degree,
gamma,
coef0,
nu,
cache_size,
C,
tol,
tol,
shrinking,
probability,
<int> class_weight.shape[0],
<char*> &class_weight_label[0] if class_weight_label.size > 0 else NULL,
<char*> &class_weight[0] if class_weight.size > 0 else NULL,
max_iter,
random_seed,
)
error_msg = svm_check_parameter(&problem, ¶m)
if error_msg:
raise ValueError(error_msg)
cdef float64_t[::1] target
cdef BlasFunctions blas_functions
blas_functions.dot = _dot[double]
try:
target = np.empty((X.shape[0]), dtype=np.float64)
with nogil:
svm_cross_validation(
&problem,
¶m,
n_fold,
<double *> &target[0],
&blas_functions,
)
finally:
free(problem.x)
return target.base
def set_verbosity_wrap(int verbosity):
"""
Control verbosity of libsvm library
"""
set_verbosity(verbosity)