1
2
3
4
5
6
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
23 """`SensitivityAnalyzer` for the LIBSVM implementation of a linear SVM.
24 """
25
26 biases = StateVariable(enabled=True,
27 doc="Offsets of separating hyperplanes")
28
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
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
53
54
55
56
57
58
59
60
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