1
2
3
4
5
6
7
8
9 """Data 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
53 if self._demean:
54
55 X = X - self._mean
56
57 if __debug__:
58 debug("MAP_",
59 "Mean of data in input space %s was subtracted" %
60 (self._mean))
61
62
63
64 U, SV, Vh = N.linalg.svd(X, full_matrices=0)
65
66
67
68
69 self._proj = Vh.H
70
71
72 self._sv = SV
73
74 if __debug__:
75 debug("MAP", "SVD was done on %s and obtained %d SVs " %
76 (dataset, len(SV)) + " (%d non-0, max=%f)" %
77 (len(SV.nonzero()), SV[0]))
78
79 debug("MAP_", "Mixing matrix has %s shape and norm=%f" %
80 (self._proj.shape, N.linalg.norm(self._proj)))
81
82
90
91
92 sv = property(fget=lambda self: self._sv, doc="Singular values")
93