How to apply z-score?

Hello
Should I apply z-score (to get zero mean and unit variance) on feature vectors (columns) or data points (rows)? I'm speaking about features in a machine learning context.
Second, how can I do this z-score thing in Matlab?
Thank your very much for the answers beforehand.

 Accepted Answer

Walter Roberson
Walter Roberson on 27 May 2015
Edited: Walter Roberson on 27 May 2015
You usually work feature by feature.
centered = [A,ones(size(A,1),1)] * [eye(size(A,2));-mean(A)]);
s_and_c = centered * diag(1./std(centered));
(That took entirely too long to work out in terms of matrix algebra! Perhaps this time I will be able to remember for future ;-))

5 Comments

The zscore function is part of the Statistics Toolbox.
Sepp
Sepp on 27 May 2015
Thank you very much.
Do you mean doing z-score feature vector by feature vector, i.e. scaling each feature vector to zero mean and unit variance?
Walter Roberson
Walter Roberson on 27 May 2015
Edited: Walter Roberson on 27 May 2015
Yes, you usually apply feature vector by feature vector, scaling each feature vector to zero mean and unit variance. The code I provided above does that for the matrix A when it is assumed that features are columns and samples are rows. s_and_c is the data already recentered and rescaled. (Now that I have fixed my mistake; I had used var() where I needed std())
Sepp
Sepp on 27 May 2015
Thank you very much, Walter.
I have 3D images which I divide into cubes. Inside each cube I do PCA to reduce dimensionality. Afterwards, I compute cross-correlation between the cubes of one image to derive features for that image and then apply PCA again to reduce dimensionality. The features I then feed into classifier.
Should I apply z-score just before I feed it into the classifier or just after having the cubes (i.e. before applying PCA the first time)?
I think you should be doing it before you run PCA, but I do not remember. SVD never was one of my strong points :(

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!