32. Features and Labels in Machine Learning
32.3 Label Leakage (Do Not Cheat the Model)
Why. Label leakage means your features secretly include the answer (or something only known after the answer). The model “cheats” in the notebook and fails in real life.
What. Example: predicting was_late while including actual_delivery_minutes as a feature. In production, when the rider leaves, you do not yet know actual minutes. The feature is illegal for that prediction moment.
How.
Leakage hunt
- Write the prediction moment: “When the order is packed, before dispatch.”
- For each feature ask: “Do I know this value at that moment?”
- If no → drop it from X.
- Also drop columns that are just another spelling of y.
# Bad feature list (leakage) vs safer list
y = "was_late"
leaky_X = ["items", "distance_km", "actual_delivery_minutes"] # minutes leak the answer
safer_X = ["items", "distance_km", "rain_mm", "hour_ist"]
print("Use:", safer_X)
print("Avoid for this problem:", leaky_X)
Ravindra Bagale's Tip
If your model shows 99% accuracy, check for label leakage before you celebrate. For every column, ask one simple question: “Will I know this value at the time of prediction?” If the answer is no, remove that column from your features.
Ravindra Bagale's Tip – मराठी
तुमचा model 99% accuracy दाखवत असेल, तर आनंद साजरा करण्याआधी label leakage तपासा. प्रत्येक column साठी एक सोपा प्रश्न विचारा: “Prediction च्या वेळी ही value मला आधीच माहीत असेल का?” उत्तर नाही असेल, तर तो column features मधून काढून टाका.
Ravindra Bagale's Tip – हिंदी
अगर तुम्हारा model 99% accuracy दिखा रहा है, तो खुशी मनाने से पहले label leakage जाँचो. हर column के लिए एक आसान सवाल पूछो: “क्या prediction के समय यह value मुझे पहले से पता होगी?” अगर जवाब नहीं है, तो उस column को features से हटा दो.