Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 16: Working with Real Data: Cleaning, Missing Values and Bias in Data

Real data is never as tidy as textbook data. It arrives with blank cells, misspelt names, duplicate rows, impossible values — a person's age listed as 250, a temperature of minus 400 — and columns in three different formats. Before any model can learn from it, the data has to be cleaned, and cleaning is not a chore to rush past: it is where most real projects succeed or fail, and where many of the fairness problems in machine learning begin.

Missing values are the first problem. A survey respondent skips a question; a sensor fails for an hour; a hospital record has no weight for a patient. You have three options: drop the incomplete rows, fill in the gaps, or let the model handle them. Dropping rows is simple but can throw away most of your data, and worse, the missing rows may not be random — if a survey is skipped mostly by one group, dropping those rows erases that group from the model. Filling in, called imputation, might mean using the average value, or predicting the missing value from the other features. Either way, you must record what you did, because a model trained on filled-in data is making assumptions you chose.

Finding the impossible and the duplicated

The next step is hunting for errors. Values outside any possible range, dates in the future, the same person recorded twice under two spellings of their name — all of these teach a model nonsense if they survive. Duplicates are especially sneaky: if the same example appears in both the training set and the test set, the model has effectively seen the test, and its score is a lie. A careful practitioner checks for duplicates across the split, not just inside one file. This kind of checking is unglamorous, but it is the difference between a model that works and a model that only looks as if it works.

Then comes the hardest part: bias in the data itself. Data records the world as it was, including its unfairness. If a hiring dataset comes from a company that historically hired mostly men, a model trained on it will learn to prefer men — not because anyone programmed prejudice in, but because the data itself is skewed. If a face dataset is mostly light-skinned faces, the model will work best on light-skinned faces. No algorithm can fix this on its own; the fix is in the data — collecting more representative examples, checking performance separately for each group, and being honest about who the model serves badly.

Turning categories into numbers

Categories need converting before a model can use them, and the standard method is called one-hot encoding. A feature such as town, with three possible values — Milltown, Edison and New Brunswick — becomes three separate yes-or-no columns: is it Milltown, is it Edison, is it New Brunswick? Each example gets a one in exactly one column and a zero in the others. The method is simple and it works, but it has a cost: a feature with a hundred possible values becomes a hundred columns, most of them zeros, and the model must learn from a very sparse, spread-out representation. For features with only a few values it is the right default. For features with very many values — such as a user's home town among thousands — practitioners use other approaches, such as grouping rare values together, or learning a compact embedding for each category, the same idea you will meet for words in the next course. Whichever method you choose, record it: the encoding is part of the model, and it must be applied identically when the model meets new data.

The habit to take from this lesson is simple: never trust a dataset you have not looked at. Open it, count things, draw pictures, check the strange values, ask who is missing. A model is only as honest as the data it learned from, and in the next lesson you will see what happens when that honesty is tested in the real world.

Watch & Learn

One video to watch for this unit, from a trusted educator.

  • But what is a neural network? | Deep learning chapter 1 — 3Blue1Brown
    Grant Sanderson builds a neural network from scratch with clear animations, showing how layers of neurons turn a grid of pixels into a digit — the best visual introduction to what Lessons 14 and 15 described.

Key Terms

Data cleaning
Preparing raw data for a model: fixing errors, handling missing values and removing duplicates.
Missing value
A blank or absent measurement in a dataset, caused by a skipped question, a failed sensor or an incomplete record.
Imputation
Filling in missing values, for example with the average, so the example can still be used.
Duplicate
The same example recorded twice, which can leak test data into training if it appears in both sets.
Outlier
A value far outside the normal range, such as an age of 250, which may be an error or a genuinely unusual case.
Bias in data
Skew in a dataset that reflects past unfairness or uneven collection, which a model will learn and repeat.
Representative data
Data that covers all the groups the model will serve, in fair proportions, rather than mostly one group.

Discussion Questions

  1. A survey's missing answers come mostly from one age group. What happens if you simply delete those rows?
  2. Why is a duplicate that appears in both the training set and the test set so damaging?
  3. A hiring dataset reflects a company's past hiring. Who is harmed if a model learns from it unchanged, and what could be done?

Practice Questions

  1. Name three kinds of mess real data arrives with.
  2. What is imputation?
  3. Why can dropping rows with missing values be unfair?
  4. Why are duplicates across the train-test split dangerous?
  5. How does bias get into a dataset?
Show answers
  1. Missing values, impossible values and duplicate rows. Real data also arrives with misspellings, mixed formats and inconsistent units.
  2. Filling in missing values, for example with the average value. It lets incomplete rows still be used, but it adds an assumption you chose.
  3. Because the missing rows may not be random — one group may be missing more often. Dropping them can erase that group from the model entirely.
  4. Because the model has effectively seen the test examples during training. Its test score then looks better than it deserves, since it memorised rather than generalised.
  5. The data records the world as it was, including past unfairness and uneven collection. A hiring dataset from a company that mostly hired men teaches a model to prefer men.
Keep reading to complete this lesson