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