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 import warning
16 from mvpa.base.dochelpers import enhancedDocString
17 from mvpa.mappers.base import ProjectionMapper
18
19 from mdp.nodes import NIPALSNode
20
21
23 """Mapper to project data onto PCA components estimated from some dataset.
24
25 After the mapper has been instantiated, it has to be train first. The PCA
26 mapper only handles 2D data matrices.
27 """
28 - def __init__(self, transpose=False, **kwargs):
32
33
34 __doc__ = enhancedDocString('PCAMapper', locals(), ProjectionMapper)
35
36
38 """Determine the projection matrix onto the components from
39 a 2D samples x feature data matrix.
40 """
41 samples = dataset.samples
42 dtype = samples.dtype
43 if str(samples.dtype).startswith('uint') \
44 or str(samples.dtype).startswith('int'):
45 warning("PCA: input data is in integers. " + \
46 "MDP's NIPALSNode operates only on floats, thus "+\
47 "coercing to double")
48 dtype = N.double
49 samples = samples.astype(N.double)
50
51 node = NIPALSNode(dtype=dtype)
52 node.train(samples)
53 self._proj = N.asmatrix(node.get_projmatrix())
54 self._recon = N.asmatrix(node.get_recmatrix())
55
56
57 self._var = node.d
58
59
60 var = property(fget=lambda self: self._var, doc='Variances per component')
61