Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 76 ยท Getting Data

Generating Synthetic Data for Practice

Generate realistic practice datasets with seeded NumPy generators: noise, missing values, outliers and duplicates, plus a known ground truth to test methods.

Real data is messy, private and unlabelled: you never know the true answer, so you cannot tell whether your method found it. Synthetic data flips that. You write the rules that create the data, so you know exactly what is in it. If your analysis is supposed to find that "each support ticket raises churn risk", you can check that it does, and you can see what goes wrong when you add missing values or outliers on top.

This is how we will make practice datasets for the rest of the course. You will learn to draw random numbers reproducibly, to build a table with a hidden ground truth, to check that a method recovers that truth, and to corrupt a clean table in controlled ways. The goal is not to fake results for a report (never do that); it is to build a laboratory where methods can be tested.

Seeded random generators

NumPy's modern random API starts with a generator created from a seed. The same seed always produces the same numbers, so your table, your plots and your quiz answers are identical on every run. Without a seed, every run gives different data and bugs become impossible to reproduce. See NumPy's random generator lesson for the full toolbox.

Choose the distribution to match what you are imitating:

  • normal for symmetric measurements such as height or measurement noise.
  • lognormal for positive, right-skewed amounts such as income or order value.
  • poisson for counts of events such as support tickets.
  • binomial (with n=1) for yes/no outcomes with a given probability.
  • choice for categories, with p= to set how common each one is.
Python Seeds and common distributions

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 first two lines are identical because the generators share a seed. Everything after that comes from one generator, so the draws continue its sequence; running the cell again gives the same output every time.

Tip: Create the generator once at the top of a function and pass the seed in as a parameter. Do not call np.random.seed and the old global functions; a local generator cannot be disturbed by other code.

A table with a hidden truth

A good synthetic table has three layers:

  1. Inputs: columns drawn from distributions (age, plan, tenure).
  2. Signal: an outcome computed from the inputs by a rule you choose.
  3. Noise: randomness added so the rule is never perfectly visible.

The function below builds a customer table. The churn probability follows a logistic rule: it falls with tenure (longer customers stay) and rises with the number of support tickets. We keep the true probability in a column, which in real life nobody has, so we can compare against it later.

flowchart LR
    A["Inputs<br/>age, plan, tenure, tickets"] --> B["Signal<br/>churn probability from a rule"]
    B --> C["Noise<br/>random draw 0 or 1"]
    C --> D["Observed table"]
    B -.->|"kept as ground truth"| E["true_p_churn"]

The rule is hidden from the analyst but kept by the author, so results can be checked.

Python A reusable customer generator

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 last line shows the contract of a seeded generator: seed 42 reproduces the table exactly (True), while a different seed produces a different table (False). About 30% of customers churn, and a typical customer has around 1.5 tickets. You can change n for a bigger table, or change the rule to explore another world.

Notice a design choice: age plays no role in the churn rule. It is a distractor column. Real tables are full of columns that carry no signal, and a method that claims age matters here is fooling itself.

Check that a method recovers the truth

The most valuable use of synthetic data is a recovery test: apply the method you plan to use, and see whether it finds the numbers you planted. Here a logistic regression from statsmodels tries to estimate the churn rule from 5,000 simulated customers. The true coefficients are -0.5 (intercept), -0.04 (per tenure month) and 0.5 (per ticket).

Python Does logistic regression recover the planted rule?

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 estimates land close to the truth, but not exactly on it: -0.541 instead of -0.5, 0.469 instead of 0.5. That gap is sampling noise, and it shrinks as n grows. Try 500 rows and then 50,000 to see both effects. A method that was far off with plenty of data would be broken, or the wrong tool for this kind of data.

Making the data realistic: mess on purpose

Clean synthetic data is useful for learning a method; messy synthetic data is useful for learning to survive reality. Keep the clean table as the answer key and build a separate messy copy by applying corruptions with known rates:

  • Missing completely at random (MCAR): values vanish with the same probability for everyone.
  • Missing at random (MAR): the chance of being missing depends on another observed column. Here, young customers skip the income question more often.
  • Outliers: a small share of values multiplied by 10, as with a misplaced decimal point.
  • Inconsistent labels: case and trailing spaces.
  • Duplicates: rows exported twice.

The lessons in the next chapter, starting with Auditing Data Quality, reuse tables made this way, because we can check every repair against the clean version.

Python Corrupting a clean table in controlled ways

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 last line is the point of the exercise. The true mean income is about 34,888, but the mean in the messy table is about 39,268, over 4,000 higher. Two corruptions each push it up: young people (who earn less in this world) are missing more often, and a few values grew an extra zero. A real analyst sees only the messy table and never knows this; you, the author, know both the bias and its causes. You will practise repairing exactly this kind of damage in the coming lessons.

Honest limits of synthetic data

  • It contains only what you put in. Real data has surprises you would never think to simulate, so a method that passes on synthetic data is not proven on real data. Treat recovery tests as a smoke test, not a certificate.
  • Do not present simulated results as findings. Label simulated data clearly in every report and chart.
  • Realism takes effort. Real columns are correlated, seasonal, rounded and constrained. Add the structure that matters to your test, and no more.
  • Privacy is a legitimate use. When real data cannot be shared, a synthetic stand-in with similar structure lets people practise or test pipelines. Check that it cannot be traced back to real individuals, which is harder than it sounds.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why create the random generator from a fixed seed?

    A seed fixes the sequence of random numbers. The same code then produces the same table, the same plots and the same bugs.
  2. 2. What is the main benefit of synthetic data for testing a method?

    With a planted rule you can compare the estimate with the truth. That cannot be done with real data, where the truth is unknown.
  3. 3. Customers under 30 skip the income question more often, and age is recorded for everyone. Which missingness type is this?

    The chance of being missing depends on another observed column (age), not on the missing value itself. That is missing at random.
  4. 4. A recovery test estimates a coefficient of 0.469 when the true value is 0.5. What is the best interpretation?

    Estimates from finite samples vary around the truth. A small gap is normal; a large gap with lots of data would be worrying.
  5. 5. Which distribution is a sensible choice for the number of support tickets per customer?

    Counts of events are non-negative integers, which is what the Poisson distribution produces.

Summary

  • A seeded numpy.random.default_rng(seed) makes simulated data reproducible; pick distributions that fit what you imitate.
  • A synthetic table has inputs, a hidden rule (signal) and noise; keep the true values so you can check results.
  • A recovery test asks whether your method finds the planted numbers; small gaps are sampling noise.
  • Make a messy copy with MCAR and MAR gaps, outliers, inconsistent labels and duplicates, and keep the clean table as the answer key.
  • Synthetic data is a laboratory, not evidence about the real world: label it clearly.

Next: Auditing Data Quality, where you learn to find these problems systematically.