Package mvpa :: Package mappers :: Module pca
[hide private]
[frames] | no frames]

Source Code for Module mvpa.mappers.pca

 1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
 2  #ex: set sts=4 ts=4 sw=4 et: 
 3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 4  # 
 5  #   See COPYING file distributed along with the PyMVPA package for the 
 6  #   copyright and license terms. 
 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   
22 -class PCAMapper(ProjectionMapper):
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):
29 ProjectionMapper.__init__(self, **kwargs) 30 31 self._var = None
32 33 34 __doc__ = enhancedDocString('PCAMapper', locals(), ProjectionMapper) 35 36
37 - def _train(self, dataset):
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 # store variance per PCA component 57 self._var = node.d
58 59 60 var = property(fget=lambda self: self._var, doc='Variances per component')
61