r/MLQuestions • u/CerealConMiel • 7d ago
Beginner question 👶 Does computing Group_size on train+test (no target) cause leakage? Reviewing a method from a Medium article
https://python.plainenglish.io/data-pre-processing-of-spaceship-titanic-kaggle-dataset-for-achieving-80-accuracy-f090e722fd42Hi everyone,
I'm working on the Spaceship Titanic competition and I'd like to get your opinion on a feature engineering decision that appears in a popular Medium article. I want to check whether the method is sound or if it introduces data leakage.
Reference article:
The issue: Kaggle splits passengers randomly between train and test, so some groups are split across both sets. If `Group_size` (number of passengers in the same group, extracted from `PassengerId`, e.g. `0001_01`, `0001_02` belong to group `0001`) is computed separately in train and test:
# In train
train['Group_size'] = train['Group'].map(train['Group'].value_counts())
# In test
test['Group_size'] = test['Group'].map(test['Group'].value_counts())
...the same group can get different sizes in train and test. Example: group `0001` has 3 members total (2 in train, 1 in test) → train says `Group_size = 2`, test says `Group_size = 1`. Inconsistent, and neither reflects the true size.
The alternative (which I believe the article uses, or something similar) is to compute group sizes using train + test together, without touching the target:
all_df = pd.concat([train, test], ignore_index=True)
all_df['Group_size'] = all_df.groupby('Group')['PassengerId'].transform('count')
train = all_df.iloc[:len(train)].copy()
test = all_df.iloc[len(train):].copy()
This makes `Group_size` consistent and reflects the real group size.
My questions:
- Is computing `Group_size` on train+test (excluding `Transported`) considered data leakage? My reasoning is no: it only uses `PassengerId`/`Group` (identifiers), never the target. The group size is a real-world fact, not a label being peeked at.
- Is this transductive feature engineering generally accepted in Kaggle competitions, or discouraged even when the target isn't involved?
- Does the same consistency issue apply to other derived features (`Deck`, `Side`, `CabinNum`, `FamilySize` from `Cabin`/`Name`)? Should those also be computed globally (train+test) to avoid train/test mismatch?
- Has anyone reviewed the preprocessing method in the article above? I'd appreciate feedback on whether it's a solid approach or if there are pitfalls (especially around the train/test split consistency).
I'd love to hear how experienced Kagglers handle this. Thanks in advance!
1
u/timy2shoes 7d ago
Yes it’s leakageÂ