问题
I want to separate my data into train and test set, should I apply normalization over data before or after the split? Does it make any difference while building predictive model? Thanks in advance.
回答1:
You first need to split the data into training and test set (validation set might also be required).
Don't forget that testing data points represent real-world data. Feature normalization (or data standardization) of the explanatory (or predictor) variables is a technique used to center and normalise the data by subtracting the mean and dividing by the variance. If you take the mean and variance of the whole dataset you'll be introducing future information into the training explanatory variables (i.e. the mean and variance).
Therefore, you should perform feature normalisation over the training data. Then perform normalisation on testing instances as well, but this time using the mean and variance of training explanatory variables. In this way, we can test and evaluate whether our model can generalize well to new, unseen data points.
回答2:
you can use fit then transform learn
normalizer = preprocessing.Normalizer().fit(xtrain)
transform
xtrainnorm = normalizer.transform(xtrain)
xtestnorm = normalizer.transform(Xtest)
来源:https://stackoverflow.com/questions/49444262/normalize-data-before-or-after-split-of-training-and-testing-data