1
$\begingroup$

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

$\endgroup$
2
  • 1
    $\begingroup$ How, exactly, do you propose computing this variable on the test set, where SalesPrice (presumably) will be unavailable? $\endgroup$
    – whuber
    Jul 31, 2021 at 1:57
  • 1
    $\begingroup$ @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. $\endgroup$ Aug 5, 2021 at 20:43

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ I am still having trouble even understanding what is being done. It sure sounds like the training set is using training prices -- at least averaged over ZIP regions -- to predict individual training prices, rather than predicting average prices in ZIP regions. But, because there are at least two kinds of "sale prices" mentioned in the question, I just can't figure out which really are the explanatory variables and which are the variables to be predicted. $\endgroup$
    – whuber
    Aug 5, 2021 at 21:31
  • $\begingroup$ The variable to be predicted is the sale price (target). As Darren pointed out I am merely grouping the targets by zip codes to compute the mean of each individual zip code. This is my new feature to help predict the sale price of each sample. $\endgroup$
    – Nils Lcrx
    Aug 7, 2021 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.