Data Leak or Feature Engineering in regression problem? I recently worked on a housing price dataset, where the goal is to predict sale prices.
I had the idea to construct a feature on the training set, which would be dependent on the target variable and this improved my results drastically.
What I did was (Only on the training set):

*

*Create a new feature df["SalesPrice"] / df["Housing_area"]

*Group the new column by zipcodes and calculate the mean price for each zipcode

After that I applied the values to the corresponding zip codes in the test set.
My question is, if this is a valid approach or if you would consider it a data leak? In my oppinion it is not a leak, since I calculated everything on the training set - so no information from the test set was leaked into the training set.
But I can not help to have bad feelings, because I was always told to strongly separate test and training data - however whenever feature engineering is done in any way these features have to be created on the test set as well right?
Maybe you can share some insights with me.
Best regards

I am assuming you mean you created a new column in test data called "meanPriceForZipcode". And that with the benefit of this new column your model gave better results?
This is fine. One way to look at it is that you've done nothing different to a deep learning model (with residual connection). Your first layer has learned to predict the average price for each zipcode area; that then feeds into the second layer, along with all the other inputs (i.e. the residual connection), and produces your final prediction.
Both layers only saw the training data during training.
