1
2
3
4
5
6
7
8
9 """Importer for the available SVM and SVR machines.
10
11 Multiple external libraries implementing Support Vector Machines
12 (Classification) and Regressions are available: LIBSVM, and shogun.
13 This module is just a helper to provide default implementation for SVM
14 depending on the availability of external libraries. By default LIBSVM
15 implementation is choosen by default, but in any case both libraries
16 are available through importing from this module:
17
18 > from mvpa.clfs.svm import sg, libsvm
19 > help(sg.SVM)
20 > help(libsvm.SVM)
21
22 Please refer to particular interface for more documentation about
23 parametrization and available kernels and implementations.
24 """
25
26 __docformat__ = 'restructuredtext'
27
28
29 from mvpa.base import warning, cfg, externals
30 from _svmbase import _SVM
31
32 if __debug__:
33 from mvpa.base import debug
34
35
36 SVM = None
37 _NuSVM = None
38
39
40
41 _VALID_BACKENDS = ('libsvm', 'shogun', 'sg')
42 default_backend = cfg.get('svm', 'backend', default='libsvm').lower()
43 if default_backend == 'shogun':
44 default_backend = 'sg'
45
46 if not default_backend in _VALID_BACKENDS:
47 raise ValueError, 'Configuration option svm.backend got invalid value %s.' \
48 ' Valid choices are %s' % (default_backend, _VALID_BACKENDS)
49
50 if __debug__:
51 debug('SVM', 'Default SVM backend is %s' % default_backend)
52
53 if externals.exists('shogun'):
54 from mvpa.clfs import sg
55 SVM = sg.SVM
56
57
58
59 if externals.exists('libsvm'):
60
61 from mvpa.clfs import libsvmc as libsvm
62 _NuSVM = libsvm.SVM
63 if default_backend == 'libsvm' or SVM is None:
64 if __debug__ and default_backend != 'libsvm' and SVM is None:
65 debug('SVM',
66 'Default SVM backend %s was not found, so using libsvm'
67 % default_backend)
68 SVM = libsvm.SVM
69
70
71 if SVM is None:
72 warning("None of SVM implementions libraries was found")
73 else:
74 _defaultC = _SVM._SVM_PARAMS['C'].default
75 _defaultNu = _SVM._SVM_PARAMS['nu'].default
76
77
79 """C-SVM classifier using linear kernel.
80
81 See help for %s for more details
82 """ % SVM.__class__.__name__
83
85 """
86 """
87
88 SVM.__init__(self, C=C, kernel_type='linear', **kwargs)
89
90
92 """C-SVM classifier using a radial basis function kernel.
93
94 See help for %s for more details
95 """ % SVM.__class__.__name__
96
98 """
99 """
100
101 SVM.__init__(self, C=C, kernel_type='RBF', **kwargs)
102
103 if _NuSVM is not None:
105 """Nu-SVM classifier using linear kernel.
106
107 See help for %s for more details
108 """ % _NuSVM.__class__.__name__
109
111 """
112 """
113
114 _NuSVM.__init__(self, nu=nu, kernel_type='linear', **kwargs)
115
117 """Nu-SVM classifier using a radial basis function kernel.
118
119 See help for %s for more details
120 """ % SVM.__class__.__name__
121
123
124 SVM.__init__(self, nu=nu, kernel_type='RBF', **kwargs)
125