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

Source Code for Module mvpa.clfs.libsvmc.sens

 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  """Provide sensitivity measures for libsvm's SVM.""" 
10   
11  __docformat__ = 'restructuredtext' 
12   
13  import numpy as N 
14   
15  from mvpa.base import warning 
16  from mvpa.misc.state import StateVariable 
17  from mvpa.measures.base import Sensitivity 
18   
19  if __debug__: 
20      from mvpa.base import debug 
21   
22 -class LinearSVMWeights(Sensitivity):
23 """`SensitivityAnalyzer` for the LIBSVM implementation of a linear SVM. 24 """ 25 26 biases = StateVariable(enabled=True, 27 doc="Offsets of separating hyperplanes") 28
29 - def __init__(self, clf, **kwargs):
30 """Initialize the analyzer with the classifier it shall use. 31 32 :Parameters: 33 clf: LinearSVM 34 classifier to use. Only classifiers sub-classed from 35 `LinearSVM` may be used. 36 """ 37 # init base classes first 38 Sensitivity.__init__(self, clf, **kwargs)
39 40
41 - def _call(self, dataset, callables=[]):
42 if self.clf.model.nr_class != 2: 43 warning("You are estimating sensitivity for SVM %s trained on %d" % 44 (str(self.clf), self.clf.model.nr_class) + 45 " classes. Make sure that it is what you intended to do" ) 46 47 svcoef = N.matrix(self.clf.model.getSVCoef()) 48 svs = N.matrix(self.clf.model.getSV()) 49 rhos = N.asarray(self.clf.model.getRho()) 50 51 self.biases = rhos 52 # XXX yoh: .mean() is effectively 53 # averages across "sensitivities" of all paired classifiers (I 54 # think). See more info on this topic in svm.py on how sv_coefs 55 # are stored 56 # 57 # First multiply SV coefficients with the actuall SVs to get 58 # weighted impact of SVs on decision, then for each feature 59 # take mean across SVs to get a single weight value 60 # per feature 61 weights = svcoef * svs 62 63 if __debug__: 64 debug('SVM', 65 "Extracting weights for %d-class SVM: #SVs=%s, " % \ 66 (self.clf.model.nr_class, str(self.clf.model.getNSV())) + \ 67 " SVcoefshape=%s SVs.shape=%s Rhos=%s." % \ 68 (svcoef.shape, svs.shape, rhos) + \ 69 " Result: min=%f max=%f" % (N.min(weights), N.max(weights))) 70 71 return N.asarray(weights.T)
72