1
from ctypes import POINTER, c_double, c_int
3
from dataset import LibSvmPrecomputedDataSet
13
def __init__(self, kernel,
14
tolerance=0.001, shrinking=True, cache_size=40):
19
- `tolerance`: tolerance of termination criterion
20
- `shrinking`: whether to use the shrinking heuristics
21
- `cache_size` kernel evaluation cache size (MB)
24
self.tolerance = tolerance
25
self.shrinking = shrinking
26
self.cache_size = cache_size
29
param = libsvm.svm_parameter()
30
param.kernel_type = kernel.kernel_type
31
param.degree = getattr(kernel, 'degree', 0)
32
param.gamma = getattr(kernel, 'gamma', 0.0)
33
param.coef0 = getattr(kernel, 'coef0', 0.0)
37
param.shrinking = shrinking
38
param.cache_size = cache_size
40
# defaults for optional parameters
42
param.weight = POINTER(c_double)()
43
param.weight_label = POINTER(c_int)()
44
param.probability = False
48
def fit(self, dataset, PredictorType=LibSvmPredictor):
49
if self.kernel.kernel_type == libsvm.PRECOMPUTED and \
50
not isinstance(dataset, LibSvmPrecomputedDataSet):
51
raise ValueError, 'kernel requires a precomputed dataset'
52
problem = dataset._create_svm_problem()
53
dataset._update_svm_parameter(self.param)
54
self._check_problem_param(problem, self.param)
55
model = libsvm.svm_train(problem, self.param)
56
return self.ResultsType(model, dataset, self.kernel, PredictorType)
58
def _check_problem_param(self, problem, param):
59
error_msg = libsvm.svm_check_parameter(problem, param)
61
raise ValueError, error_msg