/usr/share/pyshared/mvpa2/mappers/svd.py is in python-mvpa2 2.2.0-4ubuntu2.
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 | # 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 np
#import scipy.linalg as spl
from mvpa2.base.dochelpers import borrowdoc
from mvpa2.mappers.base import accepts_dataset_as_samples
from mvpa2.mappers.projection import ProjectionMapper
from mvpa2.featsel.helpers import ElementSelector
if __debug__:
from mvpa2.base import debug
class SVDMapper(ProjectionMapper):
"""Mapper to project data onto SVD components estimated from some dataset.
"""
@borrowdoc(ProjectionMapper)
def __init__(self, **kwargs):
"""Initialize the SVDMapper
Parameters
----------
**kwargs:
All keyword arguments are passed to the ProjectionMapper
constructor.
"""
ProjectionMapper.__init__(self, **kwargs)
self._sv = None
"""Singular values of the training matrix."""
@accepts_dataset_as_samples
def _train(self, samples):
"""Determine the projection matrix onto the SVD components from
a 2D samples x feature data matrix.
"""
X = np.asmatrix(samples)
X = self._demean_data(X)
# singular value decomposition
U, SV, Vh = np.linalg.svd(X, full_matrices=0)
#U, SV, Vh = spl.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 " %
(samples, 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, np.linalg.norm(self._proj)))
##REF: Name was automagically refactored
def _compute_recon(self):
"""Since singular vectors are orthonormal, sufficient to take hermitian
"""
return self._proj.H
sv = property(fget=lambda self: self._sv, doc="Singular values")
|