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

Source Code for Module mvpa.clfs.svm

  1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  #ex: set 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  """Wrap the libsvm package into a very simple class interface.""" 
 10   
 11  __docformat__ = 'restructuredtext' 
 12   
 13  # take care of conditional import of external classifiers 
 14  from mvpa.base import warning, cfg, externals 
 15  from _svmbase import _SVM 
 16   
 17  if __debug__: 
 18      from mvpa.base import debug 
 19   
 20  # default SVM implementation 
 21  SVM = None 
 22  _NuSVM = None 
 23   
 24   
 25  # TODO: handle choices within cfg 
 26  _VALID_BACKENDS = ('libsvm', 'shogun', 'sg') 
 27  default_backend = cfg.get('svm', 'backend', default='libsvm').lower() 
 28  if default_backend == 'shogun': 
 29      default_backend = 'sg' 
 30   
 31  if not default_backend in _VALID_BACKENDS: 
 32      raise ValueError, 'Configuration option svm.backend got invalid value %s.' \ 
 33            ' Valid choices are %s' % (default_backend, _VALID_BACKENDS) 
 34   
 35  if __debug__: 
 36      debug('SVM', 'Default SVM backend is %s' % default_backend) 
 37   
 38  if externals.exists('shogun'): 
 39      from mvpa.clfs import sg 
 40      SVM = sg.SVM 
 41      #if not 'LinearCSVMC' in locals(): 
 42      #    from mvpa.clfs.sg.svm import * 
 43   
 44  if externals.exists('libsvm'): 
 45      # By default for now we want simply to import all SVMs from libsvm 
 46      from mvpa.clfs import libsvmc 
 47      _NuSVM = libsvmc.SVM 
 48      if default_backend == 'libsvm' or SVM is None: 
 49          if __debug__ and default_backend != 'libsvm' and SVM is None: 
 50              debug('SVM', 
 51                    'Default SVM backend %s was not found, so using libsvm' 
 52                    % default_backend) 
 53          SVM = libsvmc.SVM 
 54      #from mvpa.clfs.libsvm.svm import * 
 55   
 56  if SVM is None: 
 57      warning("None of SVM implementions libraries was found") 
 58  else: 
 59      _defaultC = _SVM._SVM_PARAMS['C'].default 
 60      _defaultNu = _SVM._SVM_PARAMS['nu'].default 
 61   
 62      # Define some convinience classes 
63 - class LinearCSVMC(SVM):
64 """C-SVM classifier using linear kernel. 65 66 See help for %s for more details 67 """ % SVM.__class__.__name__ 68
69 - def __init__(self, C=_defaultC, **kwargs):
70 """ 71 """ 72 # init base class 73 SVM.__init__(self, C=C, kernel_type='linear', **kwargs)
74 75
76 - class RbfCSVMC(SVM):
77 """C-SVM classifier using a radial basis function kernel. 78 79 See help for %s for more details 80 """ % SVM.__class__.__name__ 81
82 - def __init__(self, C=_defaultC, **kwargs):
83 """ 84 """ 85 # init base class 86 SVM.__init__(self, C=C, kernel_type='RBF', **kwargs)
87 88 if _NuSVM is not None:
89 - class LinearNuSVMC(_NuSVM):
90 """Nu-SVM classifier using linear kernel. 91 92 See help for %s for more details 93 """ % _NuSVM.__class__.__name__ 94
95 - def __init__(self, nu=_defaultNu, **kwargs):
96 """ 97 """ 98 # init base class 99 _NuSVM.__init__(self, nu=nu, kernel_type='linear', **kwargs)
100
101 - class RbfNuSVMC(SVM):
102 """Nu-SVM classifier using a radial basis function kernel. 103 104 See help for %s for more details 105 """ % SVM.__class__.__name__ 106
107 - def __init__(self, nu=_defaultNu, **kwargs):
108 # init base class 109 SVM.__init__(self, nu=nu, kernel_type='RBF', **kwargs)
110