Consider a matrix X where the columns are the attributes and each row is an example. There is no order of occurrence of the examples. The target variables are continuous valued.

Does normalization to 0 mean and 1 standard deviation occur by considering the mean and standard deviation each column separately or for each row? Do we have a mean and sigma for each column? My confusion is that each feature is in a row representing and example. So feature normalization should be done for each example. But I may be wrong.

QUESTION 1) Should each row be normalized or each column wise? Is this the correct approach for calculating the mean and standard deviation for normalization (in MATLAB) and then apply it for normalization to zero mean and 1 std? I am calculating the mean and sigma for each attribute across all examples.

%X = data matrix rows are the number of examples, columns are the attributEs
for i = 1:size(X, 2),
  mu(i) = mean(X(:,i));
  sigma(i) = std(X(:,i));


 X= [1 2 4; 1 1 1; 3 2 2; 0 0 0]; %4 examples and 3 attributes
 %for each column there is a mu and sigma
mu =

    1.2500    1.2500    1.7500
sigma =

    1.2583    0.9574    1.7078

QUESTION 2) My dataset is composed of the input and targets (continuous valued). Should normalization of the entire dataset which contains the input and target be done altogether and then should the splitting of the normalized dataset into train, validation and test set and traintarget and test target be done?

Please correct me where wrong.

  • $\begingroup$ Basically I have asked if the test set and target variables need to be normalized together with the entire training set. Say I have a dataset which contains the train, test and target. Should I normalize the whole set and then split it into train, test and target? Should the target be normalized as well? $\endgroup$
    – Sm1
    May 8, 2019 at 20:14
  • $\begingroup$ I have changed my question which is now different from the one in the link $\endgroup$
    – Sm1
    May 8, 2019 at 20:34
  • $\begingroup$ There is no limit to the number of questions you can ask. However when you post one, please limit it to a single question. Also please don't change your question fundamentally, but write a new one instead. Otherwise existing answers might not make sense anymore. $\endgroup$ May 9, 2019 at 8:01

2 Answers 2


Question 1 You find the mean and standard deviation of the first column and perform the z-score transform in that column. Then you move to the next column and repeat the process.

Question 2 Elaborating on what Emma Jean wrote, consider the goal of machine learning. You want to make predictions about data that you truly haven’t ever seen. Indeed, speech recognition software ought to be able to make predictions about speech by people who haven’t been born yet (once they’re born and learn to talk, of course).

So you completely hide the out-of-sample data, since the real application of your model will not get to see the data on which it will be making predictions.

Getting back to question 1, if you want to develop a model on 100 observations (per column—each row is one multivariate observation) with 80 as development data and 20 for testing, you only calculate mean and standard deviation of those 80 observations, but you use those means and standard deviations to do the z-score transform of all 100 observations per column.


I've always taken the approach that you should treat your test set like you have never seen it before (i.e. you don't know what it looks like so you can't include it in your normalization!)

Normalize your training set only and apply the same scaler to your test set. This is easy to do in Python with scikit-learn methods but I am not sure how to implement this in MATLAB.


Not the answer you're looking for? Browse other questions tagged or ask your own question.