Data Leakage হলো Machine Learning interview-এর সবচেয়ে গুরুত্বপূর্ণ practical topics-এর একটি।

একটি model offline evaluation-এ খুব ভালো performance করছে, কিন্তু production-এ গিয়ে suddenly performance খারাপ হয়ে গেল—এর একটি common কারণ হতে পারে data leakage।

Definition

Data leakage ঘটে যখন training বা evaluation-এর সময় model এমন information ব্যবহার করে, যা real prediction time-এ available থাকার কথা নয়।

অর্থাৎ model এমন কিছু information দেখে ফেলছে, যা বাস্তবে future prediction করার সময় তার জানার কথা ছিল না।

সহজ Example

ধরুন আজ রাতে আমরা আগামীকালের demand predict করছি।

Feature হিসেবে ব্যবহার করলাম:

Tomorrow's actual sales

এটি obviously leakage।

কারণ আগামীকালের sales predict করার সময় actual sales এখনও ঘটেইনি।

Leakage সবসময় এত obvious নয়

অনেক leakage subtle হয়।

যেমন:

Tomorrow's final stockout status
Post-delivery cancellation reason
Final delivery status
Actual delivery completion time
Future customer behaviour

এগুলো historical dataset-এ থাকতে পারে।

কিন্তু prediction generate করার সময় যদি এগুলো available না থাকে, তাহলে এগুলো feature হিসেবে ব্যবহার করা leakage।

একটি practical example

ধরুন order placed হওয়ার মুহূর্তে predict করতে চাই:

Will this order be cancelled?

কিন্তু feature হিসেবে ব্যবহার করলাম:

refund_status

Problem হলো refund status সাধারণত cancellation-এর পরে তৈরি হয়।

তাই model essentially answer দেখেই prediction করছে।

Offline accuracy খুব high হতে পারে।

কিন্তু production-এ feature-টি prediction time-এ পাওয়া যাবে না।

Train-Test Leakage

Data leakage শুধু feature-এর মাধ্যমে হয় না।

Train-test split-এর মাধ্যমেও leakage হতে পারে।

ধরুন time-series data randomly split করলাম।

তখন:

Future data → Training
Past data   → Validation

এমন situation তৈরি হতে পারে।

বাস্তবে আমরা future information ব্যবহার করে past predict করতে পারি না।

তাই temporal problem-এ random split misleading হতে পারে।

Same Entity Leakage

আরেকটি common case:

একই user, product, patient বা device-এর closely related records train ও test—দুই জায়গাতেই চলে গেল।

Model তখন general pattern না শিখে entity-specific information memorize করতে পারে।

উদাহরণ:

Same patient:
Record 1 → Train
Record 2 → Test

যদি records খুব strongly related হয়, evaluation overly optimistic হতে পারে।

Golden Question

প্রতিটি feature-এর জন্য সবচেয়ে গুরুত্বপূর্ণ প্রশ্ন:

Prediction generate করার exact moment-এ কি এই information বাস্তবে available থাকবে?

যদি উত্তর No হয়, তাহলে feature-টি carefully investigate করতে হবে।

Leakage এড়াতে কী করবেন?

Prediction timestamp define করুন
Time-aware feature generation করুন
Train/validation split carefully করুন
Future-derived features বাদ দিন
Preprocessing শুধু training data-তে fit করুন
Entity/group-aware split consider করুন

উদাহরণ:

Normalization করার সময় পুরো dataset-এর mean use না করে:

Fit scaler → Train data
Transform  → Train + Validation/Test

করতে হবে।

Interview-ready Explanation

Data leakage occurs when information that would not be
available at prediction time is used during model training
or evaluation.

It can make offline performance look unrealistically good
while the model performs poorly in production.

Leakage may come from future-derived features,
incorrect train-test splitting, preprocessing on the
full dataset, or information from the target itself.

The key question is:
Would this information actually be available at the exact
moment the prediction is made?