# [SciPy-user] Numeric array functionality for cloning matlab's zscore(matrix)

Syd Diamond syd.diamond at gmail.com
Thu Aug 11 11:58:20 CDT 2005

```Long time scipy fan, first time poster :)

Anyways, I've always dealt with list objects in the past, but as
strange as this might sound, I'm not up to par on manipulating array
objects.  I'm need eigenvector functionality for a project I'm working
on, and so I started to work with the arrays more directly.

The limitations of my expertise became clear when my clone of the
simple zscore matlab function got me the right results but in a very
ugly manner.  For one, when I declared a matrix of zeros, when I
defined elements as X[i,j] I could not change the type from int to
float.

In [10]: X = zeros((2,2))
In [11]: X[0,0] = 1.5
In [12]: X[0,0]
Out[12]: 1

Well, that's just one thing.  You'll see that my code below is ugly
(although it gives me the right result).  I researched online, looked
at the source code for various scripts using Numeric, and tried to
find an irc channel, but I never got what I needed.

suggest a better way to implement this simple zscore function?  Thank
you.

MATLAB HELP:
>> help zscore
ZSCORE Standardized z score.
Z = ZSCORE(X) returns a centered, scaled version of X, known as the Z
scores of X.  For a vector input, Z = (X - MEAN(X)) ./ STD(X).  For a
matrix input, Z is a row vector containing the Z scores of each column
of X.  For N-D arrays, ZSCORE operates along the first non-singleton
dimension.

MY CODE (which matches the matlab results, but is _ugly_

from Numeric import array, shape, transpose, zeros
from scipy import cov, mean, std
A = array([[1, 2.3, 3],[1.1, 2.2, 2.9],[0.9, 1.9, 3.3]])
def zscore(X):
#Z = zeros(shape(X)) # can't get rid of the ints
Z = X.__deepcopy__(X)
X = transpose(X)
for i in xrange(shape(X)[0]):
for j in xrange(shape(X)[1]):
Z[j,i] = float((X[i,j] - mean(X[i])) / std(X[i]))
return Z

print zscore(A)

```