# Create features for each row or only for a specific value

I have a problem. I want to predict when the customer will place another order in how many days if an order comes in. I have already created my target variable next_day_in_days. This specifies in how many days the customer will place an order again. And I would like to predict this.

Since I have too few features, I want to do feature engineering. I would like to specify how many orders the customer has placed in the last 90 days. For example, I have calculated back from today's date how many orders the customer has placed in the last 90 days.

Is it better to say per row how many orders the customer has placed? Please see below for the example.

So does it make more sense to calculate this from today's date and include it as a feature or should it be recalculated for each row?

    customerId    fromDate next_day_in_days
0            1  2021-02-22         24
1            1  2021-03-18         4
2            1  2021-03-22         109
3            1  2021-02-10         12
4            1  2021-09-07         133
8            3  2022-05-17         61
10           3  2021-02-22         133
11           3  2021-02-22         133


Example

# What I have
customerId    fromDate next_day_in_days   purchase_in_last_90_days
0            1  2021-02-22         24         0
1            1  2021-03-18         4          0
2            1  2021-03-22         109        0
3            1  2021-02-10         12         0
4            1  2021-09-07         133        0
8            3  2022-05-17         61         1
10           3  2021-02-22         133        1
11           3  2021-02-22         133        1

# Or does this make more sense?
customerId    fromDate next_day_in_days   purchase_in_last_90_days
0            1  2021-02-22         24         1
1            1  2021-03-18         4          2
2            1  2021-03-22         109        3
3            1  2021-02-10         12         0
4            1  2021-09-07         133        0
8            3  2022-05-17         61         1
10           3  2021-02-22         133        0
11           3  2021-02-22         133        0

• Not really sure that I understand: in the first case you compute purchase_in_last_90_days per customer as of today, and in the second case you compute purchase_in_last_90_days based on the 90 days before the fromDate1 date?
– A Co
Commented May 30, 2022 at 7:42
• @ACo yes, correct.
– Test
Commented May 30, 2022 at 7:52

You should use option number 2, it is the only one that actually make sense statistically speaking.

If you use option 1, you are making your model dependent on its training date, which makes no sense. Indeed, the purchase_in_last_90_days feature you build this way depends on the training day's date (which you refer to as today's date in your question). As an example, if customer 1 place an order tonight, your purchase_in_last_90_days will be different tomorrow for this customer (equal to 1).

# Training today
customerId    fromDate next_day_in_days   purchase_in_last_90_days
0            1  2021-02-22         24         0
1            1  2021-03-18         4          0
2            1  2021-03-22         109        0
3            1  2021-02-10         12         0
4            1  2021-09-07         133        0
8            3  2022-05-17         61         1
10           3  2021-02-22         133        1
11           3  2021-02-22         133        1

# Training tomorrow if customer #1 places an order tonight
customerId    fromDate next_day_in_days   purchase_in_last_90_days
0            1  2021-02-22         24         1
1            1  2021-03-18         4          1
2            1  2021-03-22         109        1
3            1  2021-02-10         12         1
4            1  2021-09-07         133        1
8            3  2022-05-17         61         1
10           3  2021-02-22         133        1
11           3  2021-02-22         133        1



So your training data - and hence your model - implicitly becomes a function of the training date, because purchase_in_the_last_90_days will be dependent on the training date, all other things equals. And the training date is clearly not a feature that should be used to predict your target value!

Furthermore, using option 1 you include information in the training data that is posterior to the prediction date. Keep in mind that your training set simulates what you will have at inference time, so you should consider that for each row, the fromDate from the training set represents the prediction date (i.e. "today's date"). And you should only use information available prior to this fromDate.

The purchase_in_last_90_days` calculated with option 2 (number of purchase in the 90 days before prediction date) is a feature that you can build at inference time with the data you have available and that we can reasonably assume to be relevant to the model.

So use option 2!