]> Creatis software - CreaPhase.git/blob - octave_packages/nnet-0.1.13/prestd.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nnet-0.1.13 / prestd.m
1 ## Copyright (C) 2005 Michel D. Schmid  <michaelschmid@users.sourceforge.net>
2 ##
3 ##
4 ## This program is free software; you can redistribute it and/or modify it
5 ## under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2, or (at your option)
7 ## any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but
10 ## WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ## General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; see the file COPYING.  If not, see
16 ## <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {}[@var{pn},@var{meanp},@var{stdp},@var{tn},@var{meant},@var{stdt}] =prestd(@var{p},@var{t})
20 ## @code{prestd} preprocesses the data so that the mean is 0 and the standard deviation is 1.
21 ## @end deftypefn
22
23 ## @seealso{trastd}
24
25 ## Author: Michel D. Schmid
26
27 function [pn,meanp,stdp,tn,meant,stdt] = prestd(Pp,Tt)
28
29   ## inital description
30   ## prestd(p,t)
31   ##  * p are the general descriptions for the inputs of
32   ##    neural networks
33   ##  * t is written for "targets" and these are the outputs
34   ##    of a neural network
35   
36   ## some more detailed description:
37   ## for more informations about this
38   ## formula programmed in this file, see:
39   ## 1. http://en.wikipedia.org/wiki/Standard_score
40   ## 2. http://www.statsoft.com/textbook/stathome.html
41   ##    choose "statistical glossary", choose "standardization"
42
43   ## check range of input arguments
44   error(nargchk(1,2,nargin))
45
46   ## do first inputs
47   meanp = mean(Pp')';
48   stdp = std(Pp')';
49   [nRows,nColumns]=size(Pp);
50   rowOnes = ones(1,nColumns);
51
52   ## now set all standard deviations which are zero to 1
53   [nRowsII, nColumnsII] = size(stdp);
54   rowZeros = zeros(nRowsII,1); # returning a row containing only zeros
55   findZeros = find(stdp==0); # returning a vector containing index where zeros are
56   rowZeros(findZeros)=1; #
57   nequal = !rowZeros;
58   if (sum(rowZeros) != 0)
59     warning("Some standard deviations are zero. Those inputs won't be transformed.");
60     meanpZero = meanp.*nequal;
61     stdpZero = stdp.*nequal + 1*rowZeros;
62   else
63     meanpZero = meanp;
64     stdpZero = stdp;
65   endif
66
67   ## calculate the standardized inputs
68   pn = (Pp-meanpZero*rowOnes)./(stdpZero*rowOnes);
69
70   ## do also targets
71   if ( nargin==2 )
72     meant = mean(Tt')';
73     stdt = std(Tt')';
74
75     ## now set all standard deviations which are zero to 1
76     [nRowsIII, nColumnsIII] = size(stdt);
77     rowZeros = zeros(nRowsIII,1);
78     findZeros = find(stdt==0);
79     rowZeros(findZeros)=1;
80     nequal = !rowZeros;
81     if (sum(rowZeros) != 0)
82       warning("Some standard deviations are zero. Those targets won't be transformed.");
83       meantZero = meant.*nequal;
84       stdtZero = stdt.*nequal + 1*rowZeros;
85     else
86       meantZero = meant;
87       stdtZero = stdt;
88     endif
89
90     ## calculate the standardized targets
91     tn = (Tt-meantZero*rowOnes)./(stdtZero*rowOnes);
92   endif
93 endfunction
94
95
96 %!shared Pp, Tt, pn
97 %!  Pp = [1 2 3 4; -1 3 2 -1];
98 %!  Tt = [3 4 5 6];
99 %!  [pn,meanp,stdp] = prestd(Pp);
100 %!assert(pn,[-1.16190 -0.38730 0.38730 1.16190; -0.84887 1.09141 0.60634 -0.84887],0.00001);