Week 02 · Datasets, delayed labels, and grouped splits

Keep repeat users and not-yet-available labels out of training leakage.

← Week 1 · Subject contents · Week 3 →

Keep repeat users and not-yet-available labels out of training leakage.

Work through the problem

Rows from the same customer can share a strong identifying signal. An evaluation on familiar customers measures a different task from evaluation on new customers. A split by customer keeps the sets disjoint. A temporal cutoff also requires each training label to have been observed by that date.

In the lab each customer has one fixed binary label across four rows. A model memorizes customer labels and predicts zero for unknown customers. A random row split can look excellent even though the feature provides little information on a new ID. The grouped split asks the harder question explicitly.

Run the Python lab

import numpy as np
from sklearn.model_selection import GroupShuffleSplit, train_test_split
ids = np.repeat(np.arange(40),4)
y = ids % 2
a,b = train_test_split(np.arange(len(ids)),test_size=.25,random_state=5)
c,d = next(GroupShuffleSplit(n_splits=1,test_size=.25,random_state=5).split(ids,y,ids))
for name, train, test in [("Row",a,b),("Customer",c,d)]:
    memory = dict(zip(ids[train],y[train]))
    score = np.mean([memory.get(ids[i],0)==y[i] for i in test])
    print(name, "shared IDs", len(set(ids[train])&set(ids[test])), "accuracy", round(score,2))
assert not set(ids[c]) & set(ids[d])

Output from the lab

Row shared IDs 28 accuracy 1.0
Customer shared IDs 0 accuracy 0.6

Practice

  1. A case opened Monday and closed Friday. Is its duration known in a Wednesday training snapshot?

Check your reasoning

  1. No. The label only becomes available on Friday.

← Week 1 · Subject contents · Week 3 →

Further reading · Rules of Machine Learning