Suppose, I have a dataset with a feature_1 value and a target value. Now, I want to engineer a new feature by creating relative value by subtracting mean from each value.
Question: Can I (1) use feature_1 value of test set to calculate mean or (2) should I use only the train set values?
If (1) is correct than I can use the same mean for test set and train set by calculating the mean of feature_1 for all dataset. I'm not sure it's legal, because here we use information from the test set in the train set. On the other hand, we don't use target value, so it might be ok.
If (2) is correct, then, I suppose, we can't use test to calculate the mean for train set, but we can use train set feature_1 values to calculate mean for test set. But then train and test sets' means might be different and influence the correctness of the model for test set. I could use the mean of train set for test set, but again I'm not sure it's correct.
It might be irrelevant for a large dataset, because influence of each value for mean is negligible, so I suppose (1) is ok her. But if I have very small dataset of, say, less than 30 samples or, if e.g. I want to generate new feature by calculating relative value of feature_1 for each category in some categorical feature_2 by calculating mean of all the samples belonging to the same category. Then, it might turn out to be just a few samples in some category of feature_2, so that each sample would influence mean greatly.