1
2
3
4
5
6
7
8
9 """Singular-value decomposition mapper"""
10
11 __docformat__ = 'restructuredtext'
12
13 import numpy as N
14
15 from mvpa.base.dochelpers import enhancedDocString
16 from mvpa.mappers.base import ProjectionMapper
17 from mvpa.featsel.helpers import ElementSelector
18
19 if __debug__:
20 from mvpa.base import debug
21
22
24 """Mapper to project data onto SVD components estimated from some dataset.
25 """
27 """Initialize the SVDMapper
28
29 :Parameters:
30 **kwargs:
31 All keyword arguments are passed to the ProjectionMapper
32 constructor.
33
34 Note, that for the 'selector' argument this class also supports
35 passing a `ElementSelector` instance, which will be used to
36 determine the to be selected features, based on the singular
37 values of each component.
38 """
39 ProjectionMapper.__init__(self, **kwargs)
40
41 self._sv = None
42 """Singular values of the training matrix."""
43
44 __doc__ = enhancedDocString('SVDMapper', locals(), ProjectionMapper)
45
46
48 """Determine the projection matrix onto the SVD components from
49 a 2D samples x feature data matrix.
50 """
51 X = N.asmatrix(dataset.samples)
52 X = self._demeanData(X)
53
54
55 U, SV, Vh = N.linalg.svd(X, full_matrices=0)
56
57
58
59
60 self._proj = Vh.H
61
62
63 self._sv = SV
64
65 if __debug__:
66 debug("MAP", "SVD was done on %s and obtained %d SVs " %
67 (dataset, len(SV)) + " (%d non-0, max=%f)" %
68 (len(SV.nonzero()), SV[0]))
69
70 if "MAP_" in debug.active:
71 debug("MAP_", "Mixing matrix has %s shape and norm=%f" %
72 (self._proj.shape, N.linalg.norm(self._proj)))
73
74
86
88 """Since singular vectors are orthonormal, sufficient to take hermitian
89 """
90 return self._proj.H
91
92 sv = property(fget=lambda self: self._sv, doc="Singular values")
93