-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy paththundersvm.py
More file actions
601 lines (528 loc) · 24.8 KB
/
Copy paththundersvm.py
File metadata and controls
601 lines (528 loc) · 24.8 KB
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
from sklearn.base import BaseEstimator
from sklearn.base import RegressorMixin, ClassifierMixin
ThundersvmBase = BaseEstimator
ThundersvmRegressorBase = RegressorMixin
ThundersvmClassifierBase = ClassifierMixin
import numpy as np
import scipy.sparse as sp
from sklearn.utils import check_X_y, column_or_1d, check_array
from sklearn.utils.validation import _num_samples
from ctypes import *
from os import path, curdir
from sys import platform
dirname = path.dirname(path.abspath(__file__))
if platform == "linux" or platform == "linux2":
shared_library_name = "libthundersvm.so"
elif platform == "win32":
shared_library_name = "thundersvm.dll"
elif platform == "darwin":
shared_library_name = "libthundersvm.dylib"
else:
raise EnvironmentError("OS not supported!")
if path.exists(path.abspath(path.join(dirname, shared_library_name))):
lib_path = path.abspath(path.join(dirname, shared_library_name))
else:
if platform == "linux" or platform == "linux2":
lib_path = path.join(dirname, shared_library_name)
elif platform == "win32":
lib_path = path.join(dirname, shared_library_name)
elif platform == "darwin":
lib_path = path.join(dirname, shared_library_name)
if path.exists(lib_path):
thundersvm = CDLL(lib_path)
else:
# try the build directory
if platform == "linux" or platform == "linux2":
lib_path = path.join(dirname, "../../build/lib", shared_library_name)
elif platform == "win32":
lib_path = path.join(dirname, "../../build/lib", shared_library_name)
elif platform == "darwin":
lib_path = path.join(dirname, "../../build/lib", shared_library_name)
if path.exists(lib_path):
thundersvm = CDLL(lib_path)
else:
raise FileNotFoundError("Please build the library first!")
SVM_TYPE = ['c_svc', 'nu_svc', 'one_class', 'epsilon_svr', 'nu_svr']
KERNEL_TYPE = ['linear', 'polynomial', 'rbf', 'sigmoid', 'precomputed']
class SvmModel(ThundersvmBase):
def __init__(self, kernel, degree,
gamma, coef0, C, nu, epsilon,
tol, probability, class_weight,
shrinking, cache_size, verbose,
max_iter, n_jobs, max_mem_size, random_state, gpu_id):
self.kernel = kernel
self.degree = degree
self.gamma = gamma
self.coef0 = coef0
self.C = C
self.nu = nu
self.epsilon = epsilon
self.tol = tol
self.probability = probability
self.class_weight = class_weight
self.shrinking = shrinking
self.cache_size = cache_size
self.verbose = verbose
self.max_iter = max_iter
self.n_jobs = n_jobs
self.random_state = random_state
self.max_mem_size = max_mem_size
self.gpu_id = gpu_id
self.model = None
thundersvm.model_new.restype = c_void_p
# self.model = thundersvm.model_new(SVM_TYPE.index(self._impl))
# if self.max_mem_size != -1:
# thundersvm.set_memory_size(c_void_p(self.model), self.max_mem_size)
def __del__(self):
if self.model is not None:
thundersvm.model_free(c_void_p(self.model))
def fit(self, X, y):
if self.model is not None:
thundersvm.model_free(c_void_p(self.model))
self.model = None
sparse = sp.isspmatrix(X)
self._sparse = sparse and not callable(self.kernel)
X, y = check_X_y(X, y, dtype=np.float64, order='C', accept_sparse='csr')
y = column_or_1d(y, warn=True).astype(np.float64)
solver_type = SVM_TYPE.index(self._impl)
if self.gamma == 'auto':
self._gamma = 1.0 / X.shape[1]
else:
self._gamma = self.gamma
if self.kernel not in KERNEL_TYPE:
print("The kernel parameter not recognized, please refer to the document.")
exit()
else:
kernel = KERNEL_TYPE.index(self.kernel)
fit = self._sparse_fit if self._sparse else self._dense_fit
thundersvm.model_new.restype = c_void_p
self.model = thundersvm.model_new(solver_type)
if self.max_mem_size != -1:
thundersvm.set_memory_size(c_void_p(self.model), self.max_mem_size)
fit(X, y, solver_type, kernel)
if self._train_succeed[0] == -1:
print("Training failed!")
return
self.n_sv = thundersvm.n_sv(c_void_p(self.model))
csr_row = (c_int * (self.n_sv + 1))()
csr_col = (c_int * (self.n_sv * self.n_features))()
csr_data = (c_float * (self.n_sv * self.n_features))()
data_size = (c_int * 1)()
sv_indices = (c_int * self.n_sv)()
thundersvm.get_sv(csr_row, csr_col, csr_data, data_size, sv_indices, c_void_p(self.model))
self.row = np.frombuffer(csr_row, dtype=np.int32)
self.col = np.frombuffer(csr_col, dtype=np.int32)[:data_size[0]]
self.data = np.frombuffer(csr_data, dtype=np.float32)[:data_size[0]]
self.support_vectors_ = sp.csr_matrix((self.data, self.col, self.row))
if not self._sparse:
self.support_vectors_ = self.support_vectors_.toarray(order='C')
self.support_ = np.frombuffer(sv_indices, dtype=np.int32).astype(int)
dual_coef = (c_float * ((self.n_classes - 1) * self.n_sv))()
thundersvm.get_coef(dual_coef, self.n_classes, self.n_sv, c_void_p(self.model))
self.dual_coef_ = np.frombuffer(dual_coef, dtype=np.float32)\
.astype(float)\
.reshape((self.n_classes - 1, self.n_sv))
rho_size = int(self.n_classes * (self.n_classes - 1) / 2)
self.n_binary_model = rho_size
rho = (c_float * rho_size)()
thundersvm.get_rho(rho, rho_size, c_void_p(self.model))
self.intercept_ = np.frombuffer(rho, dtype=np.float32).astype(float)
if self.kernel == 'linear':
coef = (c_float * (self.n_binary_model * self.n_features))()
thundersvm.get_linear_coef(coef, self.n_binary_model, self.n_features, c_void_p(self.model))
self.coef_ = np.frombuffer(coef, dtype=np.float32)\
.astype(float)\
.reshape((self.n_binary_model, self.n_features))
n_support_ = (c_int * self.n_classes)()
thundersvm.get_support_classes(n_support_, self.n_classes, c_void_p(self.model))
self.n_support_ = np.frombuffer(n_support_, dtype=np.int32).astype(int)
self.shape_fit_ = X.shape
return self
def _dense_fit(self, X, y, solver_type, kernel):
X = np.asarray(X, dtype=np.float32, order='C')
samples = X.shape[0]
features = X.shape[1]
X_1d = X.ravel()
data = X_1d.ctypes.data_as(POINTER(c_float))
kernel_type = kernel
y = np.asarray(y, dtype=np.float32, order='C')
label = y.ctypes.data_as(POINTER(c_float))
if self.class_weight is None:
weight_size = 0
self.class_weight = dict()
weight_label = (c_int * weight_size)()
weight_label[:] = list(self.class_weight.keys())
weight = (c_float * weight_size)()
weight[:] = list(self.class_weight.values())
elif self.class_weight == 'balanced':
y_unique = np.unique(y)
y_count = np.bincount(y.astype(int))
weight_label_list = []
weight_list = []
for n in range(0, len(y_count)):
if y_count[n] != 0:
weight_label_list.append(n)
weight_list.append(samples / (len(y_unique) * y_count[n]))
weight_size = len(weight_list)
weight_label = (c_int * weight_size)()
weight_label[:] = weight_label_list
weight = (c_float * weight_size)()
weight[:] = weight_list
else:
weight_size = len(self.class_weight)
weight_label = (c_int * weight_size)()
weight_label[:] = list(self.class_weight.keys())
weight = (c_float * weight_size)()
weight[:] = list(self.class_weight.values())
n_features = (c_int * 1)()
n_classes = (c_int * 1)()
self._train_succeed = (c_int * 1)()
thundersvm.dense_model_scikit(
samples, features, data, label, solver_type,
kernel_type, self.degree, c_float(self._gamma), c_float(self.coef0),
c_float(self.C), c_float(self.nu), c_float(self.epsilon), c_float(self.tol),
self.probability, weight_size, weight_label, weight,
self.verbose, self.max_iter, self.n_jobs, self.max_mem_size,
self.gpu_id,
n_features, n_classes, self._train_succeed, c_void_p(self.model))
self.n_features = n_features[0]
self.n_classes = n_classes[0]
def _sparse_fit(self, X, y, solver_type, kernel):
X.data = np.asarray(X.data, dtype=np.float32, order='C')
X.sort_indices()
kernel_type = kernel
data = X.data.ctypes.data_as(POINTER(c_float))
indices = X.indices.ctypes.data_as(POINTER(c_int32))
indptr = X.indptr.ctypes.data_as(POINTER(c_int32))
y = np.asarray(y, dtype=np.float32, order='C')
label = y.ctypes.data_as(POINTER(c_float))
if self.class_weight is None:
weight_size = 0
self.class_weight = dict()
weight_label = (c_int * weight_size)()
weight_label[:] = list(self.class_weight.keys())
weight = (c_float * weight_size)()
weight[:] = list(self.class_weight.values())
elif self.class_weight == 'balanced':
y_unique = np.unique(y)
y_count = np.bincount(y.astype(int))
weight_label_list = []
weight_list = []
for n in range(0, len(y_count)):
if y_count[n] != 0:
weight_label_list.append(n)
weight_list.append(X.shape[0] / (len(y_unique) * y_count[n]))
weight_size = len(weight_list)
weight_label = (c_int * weight_size)()
weight_label[:] = weight_label_list
weight = (c_float * weight_size)()
weight[:] = weight_list
else:
weight_size = len(self.class_weight)
weight_label = (c_int * weight_size)()
weight_label[:] = list(self.class_weight.keys())
weight = (c_float * weight_size)()
weight[:] = list(self.class_weight.values())
n_features = (c_int * 1)()
n_classes = (c_int * 1)()
self._train_succeed = (c_int * 1)()
thundersvm.sparse_model_scikit(
X.shape[0], data, indptr, indices, label, solver_type,
kernel_type, self.degree, c_float(self._gamma), c_float(self.coef0),
c_float(self.C), c_float(self.nu), c_float(self.epsilon), c_float(self.tol),
self.probability, weight_size, weight_label, weight,
self.verbose, self.max_iter, self.n_jobs, self.max_mem_size,
self.gpu_id,
n_features, n_classes, self._train_succeed, c_void_p(self.model))
self.n_features = n_features[0]
self.n_classes = n_classes[0]
def _validate_for_predict(self, X):
# check_is_fitted(self, 'support_')
sparse = sp.isspmatrix(X)
self._sparse = sparse and not callable(self.kernel)
X = check_array(X, accept_sparse='csr', dtype=np.float64, order="C")
if self._sparse and not sp.isspmatrix(X):
X = sp.csr_matrix(X)
if self._sparse:
X.sort_indices()
if sp.issparse(X) and not self._sparse and not callable(self.kernel):
raise ValueError(
"cannot use sparse input in %r trained on dense data"
% type(self).__name__)
return X
def predict(self, X):
X = self._validate_for_predict(X)
predict = self._sparse_predict if self._sparse else self._dense_predict
return predict(X)
def predict_proba(self, X):
n_classes = (c_int * 1)()
thundersvm.get_n_classes(c_void_p(self.model), n_classes)
self.n_classes = n_classes[0]
if self.probability == 0:
print("Should fit with probability = 1")
return
else:
size = X.shape[0] * self.n_classes
samples = X.shape[0]
self.predict_pro_ptr = (c_float * size)()
X = self._validate_for_predict(X)
if self._sparse:
self._sparse_predict(X)
else:
self._dense_predict(X)
# size = X.shape[0] * self.n_classes
# self.predict_pro_ptr = (c_float * size)()
# X = np.asarray(X, dtype=np.float64, order='C')
#
# self.predict_label_ptr = (c_float * X.shape[0])()
# samples = X.shape[0]
# features = X.shape[1]
# X_1d = X.ravel()
#
# data = (c_float * X_1d.size)()
# data[:] = X_1d
# thundersvm.dense_predict(
# samples, features, data,
# c_void_p(self.model),
# self.predict_label_ptr)
thundersvm.get_pro(c_void_p(self.model), self.predict_pro_ptr)
self.predict_prob = np.frombuffer(self.predict_pro_ptr, dtype=np.float32)\
.reshape((samples, self.n_classes))
return self.predict_prob
def _dense_predict(self, X):
self.predict_label_ptr = (c_float * X.shape[0])()
X = np.asarray(X, dtype=np.float64, order='C')
samples = X.shape[0]
features = X.shape[1]
X_1d = X.ravel()
data = (c_float * X_1d.size)()
data[:] = X_1d
thundersvm.dense_predict(
samples, features, data,
c_void_p(self.model),
self.predict_label_ptr, self.verbose)
self.predict_label = np.frombuffer(self.predict_label_ptr, dtype=np.float32)
return self.predict_label
def _sparse_predict(self, X):
self.predict_label_ptr = (c_float * X.shape[0])()
data = (c_float * X.data.size)()
data[:] = X.data
indices = (c_int * X.indices.size)()
indices[:] = X.indices
indptr = (c_int * X.indptr.size)()
indptr[:] = X.indptr
thundersvm.sparse_predict(
X.shape[0], data, indptr, indices,
c_void_p(self.model),
self.predict_label_ptr, self.verbose)
self.predict_label = np.frombuffer(self.predict_label_ptr, dtype=np.float32)
return self.predict_label
def decision_function(self, X):
X = self._validate_for_predict(X)
n_binary_model = (c_int * 1)()
thundersvm.get_n_binary_models(c_void_p(self.model), n_binary_model)
self.n_binary_model = n_binary_model[0]
if not (self._impl in ['c_svc', 'nu_svc', 'one_class']):
print("Not support decision_function!")
return
if self._sparse:
dec_func = self._sparse_decision_function(X)
else:
dec_func = self._dense_decision_function(X)
return dec_func
def _dense_decision_function(self, X):
X = check_array(X, dtype=np.float64, order="C")
samples = X.shape[0]
features = X.shape[1]
X_1d = X.ravel()
data = (c_float * X_1d.size)()
data[:] = X_1d
dec_size = X.shape[0] * self.n_binary_model
dec_value_ptr = (c_float * dec_size)()
thundersvm.dense_decision(
samples, features, data, c_void_p(self.model), dec_size, dec_value_ptr
)
self.dec_values = np.frombuffer(dec_value_ptr, dtype=np.float32)\
.astype(float)\
.reshape((X.shape[0], self.n_binary_model))
return self.dec_values
def _sparse_decision_function(self, X):
X.data = np.asarray(X.data, dtype=np.float64, order='C')
data = (c_float * X.data.size)()
data[:] = X.data
indices = (c_int * X.indices.size)()
indices[:] = X.indices
indptr = (c_int * X.indptr.size)()
indptr[:] = X.indptr
dec_size = X.shape[0] * self.n_binary_model
dec_value_ptr = (c_float * dec_size)()
thundersvm.sparse_decision(
X.shape[0], data, indptr, indices,
c_void_p(self.model), dec_size, dec_value_ptr)
self.dec_values = np.frombuffer(dec_value_ptr, dtype=np.float32)\
.reshape((X.shape[0], self.n_binary_model))
return self.dec_values
def save_to_file(self, path):
if self.model is None:
raise ValueError("Cannot serialize model before fitting")
thundersvm.save_to_file_scikit(c_void_p(self.model), path.encode('utf-8'))
def save_to_string(self):
if self.model is None:
raise ValueError("Cannot serialize model before fitting")
thundersvm.save_to_string_scikit.restype = c_void_p
sp = thundersvm.save_to_string_scikit(c_void_p(self.model))
retval = string_at(sp)
thundersvm.free_string(cast(sp, c_void_p))
return retval
def load_from_file(self, path):
if self.model is None:
thundersvm.model_new.restype = c_void_p
self.model = thundersvm.model_new(SVM_TYPE.index(self._impl))
if self.max_mem_size != -1:
thundersvm.set_memory_size(c_void_p(self.model), self.max_mem_size)
thundersvm.load_from_file_scikit(c_void_p(self.model), path.encode('utf-8'))
self._post_load_init()
def load_from_string(self, data):
if self.model is None:
thundersvm.model_new.restype = c_void_p
self.model = thundersvm.model_new(SVM_TYPE.index(self._impl))
if self.max_mem_size != -1:
thundersvm.set_memory_size(c_void_p(self.model), self.max_mem_size)
thundersvm.load_from_string_scikit(c_void_p(self.model), data)
self._post_load_init()
def _post_load_init(self):
degree = (c_int * 1)()
gamma = (c_float * 1)()
coef0 = (c_float * 1)()
probability = (c_int * 1)()
kernel = (c_char * 20)()
thundersvm.init_model_param(kernel, degree, gamma,
coef0, probability, c_void_p(self.model))
n_classes = (c_int * 1)()
thundersvm.get_n_classes(c_void_p(self.model), n_classes)
self.n_classes = n_classes[0]
n_support_ = (c_int * self.n_classes)()
thundersvm.get_support_classes(n_support_, self.n_classes, c_void_p(self.model))
self.n_support_ = np.frombuffer(n_support_, dtype=np.int32).astype(int)
self.n_sv = thundersvm.n_sv(c_void_p(self.model))
n_feature = (c_int * 1)()
thundersvm.get_sv_max_index(c_void_p(self.model), n_feature)
self.n_features = n_feature[0]
csr_row = (c_int * (self.n_sv + 1))()
csr_col = (c_int * (self.n_sv * self.n_features))()
csr_data = (c_float * (self.n_sv * self.n_features))()
data_size = (c_int * 1)()
sv_indices = (c_int * self.n_sv)()
thundersvm.get_sv(csr_row, csr_col, csr_data, data_size, sv_indices, c_void_p(self.model))
self.row = np.frombuffer(csr_row, dtype=np.int32)
self.col = np.frombuffer(csr_col, dtype=np.int32)[:data_size[0]]
self.data = np.frombuffer(csr_data, dtype=np.float32)[:data_size[0]]
self.support_vectors_ = sp.csr_matrix((self.data, self.col, self.row))
# if self._sparse == False:
# self.support_vectors_ = self.support_vectors_.toarray(order = 'C')
self.support_ = np.frombuffer(sv_indices, dtype=np.int32)
dual_coef = (c_float * ((self.n_classes - 1) * self.n_sv))()
thundersvm.get_coef(dual_coef, self.n_classes, self.n_sv, c_void_p(self.model))
self.dual_coef_ = np.frombuffer(dual_coef, dtype=np.float32)\
.astype(float)\
.reshape((self.n_classes - 1, self.n_sv))
rho_size = int(self.n_classes * (self.n_classes - 1) / 2)
self.n_binary_model = rho_size
rho = (c_float * rho_size)()
thundersvm.get_rho(rho, rho_size, c_void_p(self.model))
self.intercept_ = np.frombuffer(rho, dtype=np.float32).astype(float)
if self.kernel == 'linear':
coef = (c_float * (self.n_binary_model * self.n_features))()
thundersvm.get_linear_coef(coef, self.n_binary_model, self.n_features, c_void_p(self.model))
self.coef_ = np.frombuffer(coef, dtype=np.float32) \
.astype(float) \
.reshape((self.n_binary_model, self.n_features))
self.kernel = kernel.value.decode()
self.degree = degree[0]
if gamma[0] != 0.0:
self.gamma = gamma[0]
self.coef0 = coef0[0]
self.probability = probability[0]
def __getstate__(self):
state = self.__dict__.copy()
state['predict_label_ptr'] = None
state['_train_succeed'] = None
if state['model'] is not None:
state['_saved_as_str'] = self.save_to_string()
state['model'] = None
return state
def __setstate__(self, state):
self.__dict__.update(state)
if '_saved_as_str' in state:
self.load_from_string(state['_saved_as_str'])
class SVC(SvmModel, ClassifierMixin):
_impl = 'c_svc'
def __init__(self, kernel='rbf', degree=3,
gamma='auto', coef0=0.0, C=1.0,
tol=0.001, probability=False, class_weight=None,
shrinking=False, cache_size=None, verbose=False,
max_iter=-1, n_jobs=-1, max_mem_size=-1, random_state=None, decision_function_shape='ovo', gpu_id=0):
self.decision_function_shape = decision_function_shape
super(SVC, self).__init__(
kernel=kernel, degree=degree, gamma=gamma,
coef0=coef0, C=C, nu=0., epsilon=0.,
tol=tol, probability=probability,
class_weight=class_weight, shrinking=shrinking,
cache_size=cache_size, verbose=verbose,
max_iter=max_iter, n_jobs=n_jobs, max_mem_size=max_mem_size, random_state=random_state, gpu_id=gpu_id)
class NuSVC(SvmModel, ClassifierMixin):
_impl = 'nu_svc'
def __init__(self, kernel='rbf', degree=3, gamma='auto',
coef0=0.0, nu=0.5, tol=0.001,
probability=False, shrinking=False, cache_size=None, verbose=False,
max_iter=-1, n_jobs=-1, max_mem_size=-1, random_state=None, decision_function_shape='ovo', gpu_id=0):
self.decision_function_shape = decision_function_shape
super(NuSVC, self).__init__(
kernel=kernel, degree=degree, gamma=gamma,
coef0=coef0, C=0., nu=nu, epsilon=0.,
tol=tol, probability=probability, class_weight=None,
shrinking=shrinking, cache_size=cache_size, verbose=verbose,
max_iter=max_iter, n_jobs=n_jobs, max_mem_size=max_mem_size, random_state=random_state, gpu_id=gpu_id
)
class OneClassSVM(SvmModel):
_impl = 'one_class'
def __init__(self, kernel='rbf', degree=3, gamma='auto',
coef0=0.0, nu=0.5, tol=0.001,
shrinking=False, cache_size=None, verbose=False,
max_iter=-1, n_jobs=-1, max_mem_size=-1, random_state=None, gpu_id=0):
super(OneClassSVM, self).__init__(
kernel=kernel, degree=degree, gamma=gamma,
coef0=coef0, C=0., nu=nu, epsilon=0.,
tol=tol, probability=False, class_weight=None,
shrinking=shrinking, cache_size=cache_size, verbose=verbose,
max_iter=max_iter, n_jobs=n_jobs, max_mem_size=max_mem_size, random_state=random_state, gpu_id=gpu_id
)
def fit(self, X, y=None):
super(OneClassSVM, self).fit(X, np.ones(_num_samples(X)))
class SVR(SvmModel, RegressorMixin):
_impl = 'epsilon_svr'
def __init__(self, kernel='rbf', degree=3, gamma='auto',
coef0=0.0, C=1.0, epsilon=0.1,
tol=0.001, probability=False,
shrinking=False, cache_size=None, verbose=False,
max_iter=-1, n_jobs=-1, max_mem_size=-1, gpu_id=0):
super(SVR, self).__init__(
kernel=kernel, degree=degree, gamma=gamma,
coef0=coef0, C=C, nu=0., epsilon=epsilon,
tol=tol, probability=probability, class_weight=None,
shrinking=shrinking, cache_size=cache_size, verbose=verbose,
max_iter=max_iter, n_jobs=n_jobs, max_mem_size=max_mem_size, random_state=None, gpu_id=gpu_id
)
class NuSVR(SvmModel, RegressorMixin):
_impl = 'nu_svr'
def __init__(self, kernel='rbf', degree=3, gamma='auto',
coef0=0.0, nu=0.5, C=1.0, tol=0.001, probability=False,
shrinking=False, cache_size=None, verbose=False,
max_iter=-1, n_jobs=-1, max_mem_size=-1, gpu_id=0):
super(NuSVR, self).__init__(
kernel=kernel, degree=degree, gamma=gamma,
coef0=coef0, nu=nu, C=C, epsilon=0.,
tol=tol, probability=probability, class_weight=None,
shrinking=shrinking, cache_size=cache_size, verbose=verbose,
max_iter=max_iter, n_jobs=n_jobs, max_mem_size=max_mem_size, random_state=None, gpu_id=gpu_id
)