/usr/share/pyshared/mvpa/mappers/svd.py is in python-mvpa 0.4.8-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the PyMVPA package for the
# copyright and license terms.
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Singular-value decomposition mapper"""
__docformat__ = 'restructuredtext'
import numpy as N
from mvpa.base.dochelpers import enhancedDocString
from mvpa.mappers.base import ProjectionMapper
from mvpa.featsel.helpers import ElementSelector
if __debug__:
from mvpa.base import debug
class SVDMapper(ProjectionMapper):
"""Mapper to project data onto SVD components estimated from some dataset.
"""
def __init__(self, **kwargs):
"""Initialize the SVDMapper
:Parameters:
**kwargs:
All keyword arguments are passed to the ProjectionMapper
constructor.
Note, that for the 'selector' argument this class also supports
passing a `ElementSelector` instance, which will be used to
determine the to be selected features, based on the singular
values of each component.
"""
ProjectionMapper.__init__(self, **kwargs)
self._sv = None
"""Singular values of the training matrix."""
__doc__ = enhancedDocString('SVDMapper', locals(), ProjectionMapper)
def _train(self, dataset):
"""Determine the projection matrix onto the SVD components from
a 2D samples x feature data matrix.
"""
X = N.asmatrix(dataset.samples)
X = self._demeanData(X)
# singular value decomposition
U, SV, Vh = N.linalg.svd(X, full_matrices=0)
# store the final matrix with the new basis vectors to project the
# features onto the SVD components. And store its .H right away to
# avoid computing it in forward()
self._proj = Vh.H
# also store singular values of all components
self._sv = SV
if __debug__:
debug("MAP", "SVD was done on %s and obtained %d SVs " %
(dataset, len(SV)) + " (%d non-0, max=%f)" %
(len(SV.nonzero()), SV[0]))
# .norm might be somewhat expensive to compute
if "MAP_" in debug.active:
debug("MAP_", "Mixing matrix has %s shape and norm=%f" %
(self._proj.shape, N.linalg.norm(self._proj)))
def selectOut(self, outIds):
"""Choose a subset of SVD components (and remove all others)."""
# handle ElementSelector operating on SV (base class has no idea about)
# XXX think about more generic interface, where some 'measure' is assigned
# per each projection dimension, like in _sv in case of SVD.
# May be selector could be parametrized with an instance + attribute as literal
# so later on it could extract necessary values?
if isinstance(self._selector, ElementSelector):
ProjectionMapper.selectOut(self, self._selector(self._sv))
else:
ProjectionMapper.selectOut(self, outIds)
def _computeRecon(self):
"""Since singular vectors are orthonormal, sufficient to take hermitian
"""
return self._proj.H
sv = property(fget=lambda self: self._sv, doc="Singular values")
|