# 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):

1. Create a new feature df["SalesPrice"] / df["Housing_area"]
2. 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

• How, exactly, do you propose computing this variable on the test set, where SalesPrice (presumably) will be unavailable?
– whuber
Jul 31, 2021 at 1:57
• @whuber I think he means he created a lookup table from zipcode to mean price, and that is the new feature. I've answered from that assumption, anyway. Aug 5, 2021 at 20:43