Package mvpa :: Package clfs :: Module svm
[hide private]
[frames] | no frames]

Source Code for Module mvpa.clfs.svm

  1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  # vi: set ft=python sts=4 ts=4 sw=4 et: 
  3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
  4  # 
  5  #   See COPYING file distributed along with the PyMVPA package for the 
  6  #   copyright and license terms. 
  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  # take care of conditional import of external classifiers 
 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  # default SVM implementation 
 36  SVM = None 
 37  _NuSVM = None 
 38   
 39   
 40  # TODO: handle choices within cfg 
 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      #if not 'LinearCSVMC' in locals(): 
 57      #    from mvpa.clfs.sg.svm import * 
 58   
 59  if externals.exists('libsvm'): 
 60      # By default for now we want simply to import all SVMs from libsvm 
 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      #from mvpa.clfs.libsvm.svm import * 
 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      # Define some convinience classes 
78 - class LinearCSVMC(SVM):
79 """C-SVM classifier using linear kernel. 80 81 See help for %s for more details 82 """ % SVM.__class__.__name__ 83
84 - def __init__(self, C=_defaultC, **kwargs):
85 """ 86 """ 87 # init base class 88 SVM.__init__(self, C=C, kernel_type='linear', **kwargs)
89 90
91 - class RbfCSVMC(SVM):
92 """C-SVM classifier using a radial basis function kernel. 93 94 See help for %s for more details 95 """ % SVM.__class__.__name__ 96
97 - def __init__(self, C=_defaultC, **kwargs):
98 """ 99 """ 100 # init base class 101 SVM.__init__(self, C=C, kernel_type='RBF', **kwargs)
102 103 if _NuSVM is not None:
104 - class LinearNuSVMC(_NuSVM):
105 """Nu-SVM classifier using linear kernel. 106 107 See help for %s for more details 108 """ % _NuSVM.__class__.__name__ 109
110 - def __init__(self, nu=_defaultNu, **kwargs):
111 """ 112 """ 113 # init base class 114 _NuSVM.__init__(self, nu=nu, kernel_type='linear', **kwargs)
115
116 - class RbfNuSVMC(SVM):
117 """Nu-SVM classifier using a radial basis function kernel. 118 119 See help for %s for more details 120 """ % SVM.__class__.__name__ 121
122 - def __init__(self, nu=_defaultNu, **kwargs):
123 # init base class 124 SVM.__init__(self, nu=nu, kernel_type='RBF', **kwargs)
125