Skip to content
Tutorials Center
My learning

Topics

Lesson 71 of 76 ยท Practice and Review

Common Data Science Mistakes

Six costly data science mistakes shown with live simulations: leakage, reusing the test set, base rates, p-value misreadings, survivorship bias and over-cleaning.

Most failed data science projects do not fail because of a wrong algorithm. They fail because of a quiet mistake in the setup: information that should have been hidden leaked into the model, a test was reused until it said yes, a rare event was judged without its base rate, or the data that was left after cleaning no longer described the world. These mistakes are dangerous because they do not produce errors. They produce good-looking results.

This lesson shows six of the most common ones, and instead of only describing them, it makes each one happen in code. Every simulation uses a fixed seed and a case where we know the truth (random labels, a known effect size), so you can watch a method produce an impressive answer to a question that has no answer. Run each example, read the numbers, and notice how convincing the wrong result looks. That feeling is the thing to train.

flowchart TD
    A["A surprisingly good result"] --> B{"Could the setup<br/>have helped it?"}
    B -- "info from the future<br/>or the test rows" --> C["Leakage"]
    B -- "many tries, one winner" --> D["Test-set reuse /<br/>multiple comparisons"]
    B -- "rare event" --> E["Base-rate neglect"]
    B -- "only some cases visible" --> F["Survivorship / selection"]
    B -- "rows or values removed" --> G["Over-cleaning"]
    B -- "none of these" --> H["Maybe it is real:<br/>replicate it"]

When a result looks too good, walk through this list before celebrating.

Mistake 1: leakage through preprocessing

Leakage means the model learns from information it will not have when it is used for real. The obvious form is a feature that records the outcome (see exercise 20). The sneaky form is leakage through preprocessing: scaling, imputing or selecting features using all the data before splitting it. The test rows have then influenced the training steps.

The demonstration below uses 100 rows and 2,000 columns of pure noise, with random labels. There is nothing to find. We pick the 20 columns that look most related to the label, then cross-validate a model on them. Done outside the folds, this produces a high score. Done inside a pipeline, so that the choice is made again within each training fold, the score falls to the truth.

Python Feature selection outside vs inside cross-validation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With 2,000 noise columns, some always correlate with the labels by chance, and choosing them with the full data memorizes those coincidences for the test rows too. The cure is mechanical: everything that learns from data (scalers, imputers, encoders, selectors, even PCA) belongs inside the pipeline, fitted on training rows only. See Pipelines and Hyperparameter Tuning and Train/Test Splits and Cross-Validation.

Mistake 2: overfitting to the test set

The test set is a one-time exam. Each time you look at its score and then change something, a little information about it flows into your decisions. After dozens of rounds the score is no longer an estimate; it is a number you have selected for being high.

Here is the extreme version. We create 400 random "models" (random weight vectors, no learning at all) and score them on a test set of 100 rows with random labels. We keep the best. Then we check that model on 5,000 fresh rows.

Python Choosing the best of 400 useless models

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The winner scores 0.65 on the reused test set, which would look like real skill, but 0.50 on fresh data, which is chance. Real projects are less extreme, but the mechanism is the same: tuning thresholds, features and hyperparameters against one test set quietly inflates the score. The fix is a three-way split (train, validation for decisions, test touched once), cross-validation for decisions, and a final fresh holdout or a live trial when the stakes are high.

Mistake 3: ignoring the base rate

Suppose a screening test catches 95 percent of sick people and wrongly flags 5 percent of healthy ones. A friend tests positive. How likely is it that they are sick? Most people say about 95 percent. The right answer depends on how common the condition is. If 1 person in 100 has it, among 10,000 people there are 100 sick (95 test positive) and 9,900 healthy (495 test positive). So 95 of the 590 positives are really sick: about 16 percent.

flowchart TD
    P["10,000 people"] --> S["100 sick (1%)"]
    P --> H["9,900 healthy"]
    S --> SP["95 test positive"]
    S --> SN["5 test negative"]
    H --> HP["495 test positive"]
    H --> HN["9,405 test negative"]

Natural frequencies make the problem visible: 95 true positives are swamped by 495 false positives.

Python Simulating a screening test at different prevalences

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The simulation (about 16.3 percent) matches the formula (16.1 percent), and the last lines show that the same test is almost useless at 0.1 percent prevalence (1.9 percent) and good at 30 percent (89.1 percent). The same trap appears in fraud detection, spam filters, churn alerts and security alerts: when the event is rare, even an accurate model produces mostly false alarms. Always ask "how common is the thing?" and report precision, not just accuracy. See Evaluating Models with the Right Metrics.

Mistake 4: misreading p-values

A p-value of 0.03 does not mean "there is a 97 percent chance the effect is real". It says how surprising the data would be if there were no effect. What you really want, the share of "significant" findings that are real, depends on how many ideas tested were real to begin with and on the power of the test.

Imagine a team testing 4,000 ideas, of which only 10 percent really help (by half a standard deviation), with 30 users per group. How many of the 'wins' at p < 0.05 are false?

Python How many significant results are false?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

About 44 percent of the significant results are noise. Two things drive that: most ideas do nothing (so false alarms from the 90 percent that do not work are numerous), and the tests are weak (they catch only 49 percent of real effects). This is the engine behind many irreproducible findings. The defences are larger samples, pre-registered hypotheses, correction for multiple comparisons, effect sizes with intervals, and above all replication. See Hypothesis Testing Done Right.

Also avoid: "p > 0.05 means no effect" (small samples cannot detect real effects) and "p < 0.05 means an important effect" (with a million rows, trivial differences are significant). Always read the effect size and its interval next to the p-value.

Mistake 5: survivorship bias

When you only see the cases that survived a filter, your conclusions describe the survivors, not the whole population. Famous example: studying the habits of successful companies without the many identical ones that failed. In data work it hides in table joins and dashboards: only customers who stayed, only funds still operating, only patients who returned for a follow-up.

The simulation creates 2,000 investment funds whose yearly returns are random with a true average of 4 percent. Funds that lose 40 percent are shut down and disappear from the database.

Python The average return of funds that still exist

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The survivors seem to earn 5.74 percent a year (instead of 4.2 percent for all funds), and 81 percent of them ended above their starting value, against 69 percent of all funds. Nothing about skill changed. Selection did the work. Whenever a dataset is a filtered version of reality, ask who is missing and why, and see Sampling and Collection Bias.

Mistake 6: over-cleaning

Cleaning is necessary, but every deletion is a decision about what the data says. Two harmful habits are removing every "outlier" the rule flags, and dropping every row with a missing value.

In the example, most customers spend a modest amount, but 3 percent are VIPs who spend a lot. The classic |z| > 3 rule flags the VIPs as outliers. Then we create missing incomes that occur much more often among older people and drop incomplete rows.

Python What cleaning can quietly destroy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The outlier rule removed 80 customers and 46 percent of all revenue, including most VIPs (only 25 of 105 remain). Those rows were not errors; they were the business. The second half shows composition drift: older people make up 48 percent of the data but only 37 percent after dropna, and since older people earn more here, the complete-case mean income is too low (46,755 instead of 49,570). A model or report built from the cleaned table describes a different population from the real one. Before deleting, ask whether a value is impossible (a negative age) or merely unusual (a big customer). Fix the first, keep and understand the second, and prefer methods that keep rows (robust statistics, imputation inside a pipeline, an indicator column for missing values). See Outliers, Errors and Impossible Values and Handling Missing Values.

A short prevention checklist

Before you trust a result Ask
Leakage Would I know every feature at prediction time? Is every fitted step inside the pipeline?
Test reuse How many times did I look at the test score before deciding?
Base rates How common is the event? What are precision and the number of false alarms?
p-values How many tests did I run? What are the effect size and interval?
Selection Who or what is missing from this table, and why?
Cleaning Did I remove errors or just unusual cases? Did the group proportions change?
Baseline Does the model clearly beat the simplest sensible rule?

Build these questions into the review of every analysis, yours or someone else's. They catch more problems than any additional model.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why did selecting the 20 best features on the full data give an AUC of 0.88 on random labels?

    Selecting features with all rows lets the held-out rows leak into the training step. Inside a pipeline, selection is redone on each training fold and the score drops to chance.
  2. 2. A screening test is 95 percent sensitive and 95 percent specific, and 1 percent of people have the condition. About how likely is a positive result to be correct?

    Of 10,000 people, 95 sick people test positive but so do 495 healthy people, so only about 95 of 590 positives are true.
  3. 3. What is the main problem with tuning many versions of a model and reporting the best test score?

    Every decision informed by the test score leaks information about it. Use validation data or cross-validation for decisions, and keep the test set for one final check.
  4. 4. Why do survivors of a fund database look better than all funds, even when skill is identical?

    Removing the losers is itself a selection that raises the average of what remains.
  5. 5. Removing every |z| > 3 outlier from customer revenue deleted 46 percent of revenue. What was the mistake?

    Outlier rules only flag unusual values. Deciding that they are errors requires domain knowledge; here they were the most valuable customers.

Summary

  • Impressive results deserve suspicion first: simulate the mistake and see how easily it produces them.
  • Keep every data-dependent step (scaling, imputing, selecting) inside the cross-validated pipeline.
  • Treat the test set as a one-time exam; use validation data for choices.
  • Rare events produce mostly false alarms even with accurate tests: think in natural frequencies and report precision.
  • A p-value is not the probability that a finding is true; with weak tests and mostly null ideas, many "wins" are noise.
  • Ask who is missing from the data (survivorship), and whether cleaning removed errors or the signal.

Next: Data Science Interview Questions, where these ideas come up as questions with model answers.