Code RoomTarget encoding without train-test split
HardPrep Room Coding #2251

Target encoding without train-test split

Code reviewML systemsSenior–Staff~20 min

Review this Python target-encoding helper.

CV scores are excellent but live performance is poor. Diagnose it.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 20 min
Mark a line and say what kind of problem it is.0 findings
1import pandas as pd
2 
3def target_encode(df, cat_col, target_col):
4 # replace each category with the mean target for that category
5 means = df.groupby(cat_col)[target_col].mean()
6 df[cat_col + "_te"] = df[cat_col].map(means)
7 return df
8 
9# usage
10df = target_encode(df, "merchant_id", "is_fraud")
11X = df.drop(columns=["is_fraud"])
12y = df["is_fraud"]
13# ... then train_test_split(X, y) and fit
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.