Sub topic 12
Pitfalls: overreading patterns and leaking information
This chapter assumes you can load and clean a DataFrame with pandas, read a histogram or box plot, and know basic statistics (mean, median, correlation). It builds on the bivariate step, where you compare pairs of variables. Here you learn how those comparisons mislead you and how to guard against it.
Why EDA misleads
EDA is cheap. You can draw dozens of plots in minutes, and some of them will show a pattern by chance. Two problems follow. You may read meaning into noise, and you may let information into your analysis that you would not have at prediction time.
Overreading patterns
Small groups
A group of five orders can have a mean far from the overall mean with no real cause. Always show the group size next to any group statistic.
summary = (
df.groupby("region")["amount"]
.agg(["count", "mean", "median"])
.sort_values("count")
)
display(summary)If a region has a high mean and a count of 6, treat it as a question, not a result.
Many looks
If you compare 20 pairs of variables, you expect about one to look "significant" at the 5% level by chance alone. Note how many comparisons you made. A pattern you found after many looks needs confirmation on data you have not explored, such as a held-out set.
Correlation and outliers
A correlation coefficient summarizes a straight-line relationship. A single extreme order can create or hide one. Plot the pair before you trust the number, and compare Pearson and Spearman correlations.
pair = df[["amount", "quantity"]].dropna()
print("pearson: ", round(pair.corr(method="pearson").iloc[0, 1], 3))
print("spearman:", round(pair.corr(method="spearman").iloc[0, 1], 3))A large gap between the two often points to outliers or a curved relationship.
Correlation also says nothing about cause. If larger orders come with faster shipping, a hidden factor such as customer type may drive both.
Leaking information
Leakage means the analysis uses information that would not be available when the result is used. It usually matters once the EDA feeds a model, but the habits start in the notebook.
Common sources:
- Target-derived columns. A column such as
refund_issuedmay be recorded after the outcome you want to predict. It will look highly predictive because it is a consequence. - Future data. Features computed with data from after the prediction date.
- Whole-dataset statistics. Filling missing values with the overall mean, or choosing outlier thresholds, using rows that later become the test set.
- Duplicates across splits. The same customer appearing in both train and test rows.
A practical guard is to split before you explore for modeling purposes.
from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size=0.2, random_state=SEED)Explore train only. Touch test once, at the end. For time-ordered data, split by date instead of at random.
A checklist for the notebook
- Show counts beside every group summary.
- Count how many comparisons you made, and say so in the findings log.
- Plot a relationship before quoting its correlation.
- For each column, ask when its value becomes known.
- Compute fill values and thresholds from training rows only.
Exercise
A box plot shows that region "north" has the highest median order amount. The group has 7 orders out of 4,000. Name two checks before you record this as a finding, and name one leakage risk if you later use amount statistics per region to fill missing values.
Show answer
First, check the group size and spread (count, and how much the north values overlap with other regions), and note how many region comparisons you made. Second, confirm the pattern on data you have not explored, or treat it as a hypothesis only. For leakage, computing per-region fill values from the whole dataset uses test rows; compute them from the training rows only.