Difference between preprocessing train and test set before and after splitting Is there a difference between doing preprocessing for a dataset in sklearn before and after splitting data into train_test_split?
In other words, are both of these approaches equivalent?
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

#standardizing after splitting
X_train, X_test, y_train, y_test = train_test_split(data, target)
sc = StandardScaler().fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)

#standardizing before splitting
data_std = StandardScaler().fit_transform(data)
X_train, X_test, y_train, y_test = train_test_split(data_std, target)

 A: No, Both approaches are not equivalent. 
StandardScaler() standardize features by removing the mean and scaling to unit variance
If you fit the scaler after splitting: Suppose, if there are any outliers in the test set(after Splitting), the Scaler would not consider those in computing mean and Variance.
If you fit the scaler on whole dataset and then split, Scaler would consider all values while computing mean and Variance. 
Since, the mean and variance are different in both cases, the fits and transform functions would perform differently.
A: The test set should ideally not be preprocessed with the training data. This will ensure no 'peeking ahead'. Train data should be preprocessed separately and once the model is created we can apply the same preprocessing parameters used for the train set, onto the test set as though the test set didn't exist before.
A: You can also use the method below will preprocess your data separately but similar parameter used for training data set.  
norm = preprocessing.Normalizer().fit(xtrain)

then
x_train_norm = norm.transform(xtrain) 
x_test_norm = norm.transform(Xtest)

A: To add to phanny's answer - you should do the preprocessing on your training set separately, otherwise information from the test set will "leak" into your training data.
For preprocessing the test set, I do not see why you shouldn't preprocess it together with your training data.
