1
2
3
4
5
6
7
8
9 """Simple mapper to perform zscoring"""
10
11 __docformat__ = 'restructuredtext'
12
13 from mvpa.base import warning, externals
14
15 import numpy as N
16 from mvpa.base.dochelpers import enhancedDocString
17 from mvpa.mappers.base import ProjectionMapper
18
19 if externals.exists('scipy', raiseException=True):
20 import scipy.sparse
21 if externals.versions['scipy'] >= (0, 7, 0):
22 _identity = scipy.sparse.identity
23 else:
24 _identity = scipy.sparse.spidentity
25
26
27 __all__ = [ 'ZScoreMapper' ]
28
30 """Mapper to project data into standardized values (z-scores).
31
32 After the mapper has been instantiated, it has to be train first.
33
34 Since it tries to reuse ProjectionMapper, invariant features will
35 simply be assigned a std == 1, which would be equivalent to not
36 standardizing them at all. This is similar to not touching them
37 at all, so similar to what zscore function currently does
38 """
39 - def __init__(self,
40 baselinelabels=None,
41 **kwargs):
42 """Initialize ZScoreMapper
43
44 :Parameters:
45 baselinelabels : None or list of int or string
46 Used to compute mean and variance
47 TODO: not in effect now and need to be adherent to all
48 `ProjectionMapper`s
49 """
50
51 ProjectionMapper.__init__(self, **kwargs)
52 if baselinelabels is not None:
53 raise NotImplementedError, "Support for baseline labels " \
54 "is not yet implemented in ZScoreMapper"
55 self.baselinelabels = baselinelabels
56
57
58
59 __doc__ = enhancedDocString('ZScoreMapper', locals(), ProjectionMapper)
60
61
63 """Determine the diagonal matrix with coefficients for standartization
64 """
65 samples = dataset.samples
66 X = self._demeanData(samples)
67 std = X.std(axis=0)
68
69
70
71 std[std == 0] = 1.0
72 n = len(std)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 if n > 1:
88 proj = _identity(n)
89 proj.setdiag(1.0/std)
90 recon = _identity(n)
91 recon.setdiag(std)
92 self._proj = proj
93 self._recon = recon
94 else:
95 self._proj = N.matrix([[1.0/std[0]]])
96 self._recon = N.matrix([std])
97