Data Distribution — Generating & Understanding Data
Real ML works with thousands of data points — learn to generate and visualise them with histograms.
Use NumPy to generate random data sets and Matplotlib histograms to see how values are distributed across ranges.
EXERCISE
1Why You Need Big Data Sets
Real ML works with thousands or millions of data points — but you can generate fake ones in a single line of Python.
In the previous units we used tiny data sets of 7–13 values. That is like judging a city by visiting one street. Real ML needs thousands of data points to find reliable patterns. But you do not need to collect them by hand — Python can generate them for you.
Generating random data with NumPy:
import numpy
# 250 random floats between 0 and 5
x = numpy.random.uniform(0.0, 5.0, 250)
print(f"Count: {len(x)}")
# Output: Count: 250
print(f"Min: {round(min(x), 2)}, Max: {round(max(x), 2)}")
# Output: Min: ~0.02, Max: ~4.98
print(f"Mean: {round(numpy.mean(x), 2)}")
# Output: Mean: ~2.50 (close to the midpoint of 0 and 5)
Scaling up is trivial:
import numpy
# 100,000 random values — takes milliseconds
big_data = numpy.random.uniform(0.0, 5.0, 100000)
print(f"Count: {len(big_data)}")
# Output: Count: 100000
print(f"Mean: {round(numpy.mean(big_data), 4)}")
# Output: Mean: ~2.5000
Pro Tip: As you increase the data size, the mean converges closer to the true theoretical mean (2.5 in this case). This is the Law of Large Numbers in action — more data means more accurate statistics. This is why ML models trained on more data tend to perform better.
EXERCISE
2Histograms — Seeing How Values Are Distributed
A histogram takes your raw numbers and groups them into bars — each bar shows how many values fall in that range.
Imagine sorting a bag of marbles by colour into separate cups. A histogram does the same thing with numbers — it sorts values into bins (ranges) and counts how many land in each one.
Drawing a histogram with Matplotlib:
import numpy
import matplotlib.pyplot as plt
x = numpy.random.uniform(0.0, 5.0, 250)
plt.hist(x, 5)
plt.show()
# Output: A bar chart with 5 bars
# Each bar represents a range: 0-1, 1-2, 2-3, 3-4, 4-5
# Each bar height ≈ 50 (since 250/5 = 50 per bin)
What each bar means:
| Bin Range | Approximate Count |
|---|---|
| 0.0 – 1.0 | ~50 |
| 1.0 – 2.0 | ~50 |
| 2.0 – 3.0 | ~50 |
| 3.0 – 4.0 | ~50 |
| 4.0 – 5.0 | ~50 |
More bars = more detail:
import numpy
import matplotlib.pyplot as plt
x = numpy.random.uniform(0.0, 5.0, 100000)
plt.hist(x, 100)
plt.title("100,000 values in 100 bins")
plt.show()
# Output: A very flat, even histogram — this is uniform distribution
Pro Tip: A uniform distribution (flat histogram) means every value is equally likely. This is great for random number generators, but terrible for ML — if every outcome is equally likely, there is no pattern to learn. ML needs distributions with shape — peaks, clusters, and trends.
EXERCISE
3Uniform vs Non-Uniform — What the Shape Tells You
The shape of a distribution is a sneak preview of your data's story — flat means random, peaked means pattern.
Distributions are like geographic terrain maps for your data. A flat plain (uniform) means nothing interesting is happening. A mountain peak (normal) means most values cluster around the centre. A cliff edge (skewed) means values pile up on one side.
Uniform distribution — flat and boring:
import numpy
# Every value between 0-5 is equally likely
uniform = numpy.random.uniform(0.0, 5.0, 10000)
print(f"Mean: {round(numpy.mean(uniform), 2)}")
# Output: Mean: ~2.50
print(f"Std: {round(numpy.std(uniform), 2)}")
# Output: Std: ~1.44
# Mean is dead centre, spread is even
Non-uniform — clustered and interesting:
import numpy
# Values cluster around 5.0, with std dev of 1.0
clustered = numpy.random.normal(5.0, 1.0, 10000)
print(f"Mean: {round(numpy.mean(clustered), 2)}")
# Output: Mean: ~5.00
print(f"Std: {round(numpy.std(clustered), 2)}")
# Output: Std: ~1.00
# Most values are between 3 and 7 (mean ± 2σ)
Comparing them side by side:
| Property | Uniform | Normal |
|---|---|---|
| Shape | Flat | Bell curve |
| Common values | Everywhere equally | Near the mean |
| Rare values | None | Far from mean |
| ML usefulness | Low (no pattern) | High (clear centre) |
Pro Tip: Most real-world data is NOT uniformly distributed. Heights, test scores, temperatures, stock returns — they all tend toward a normal (bell curve) distribution. Recognising the shape of your data is the first step in choosing the right ML algorithm.
