Should feature normalization be done for training , test set and target variables together? [duplicate]

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));
end


Example:

 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?

• 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? – Sm1 May 8 at 20:14
• I have changed my question which is now different from the one in the link – Sm1 May 8 at 20:34
• 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. – Frans Rodenburg May 9 at 8:01