TExcelTAD
Home › Machine Learning › ML Normal Data Distribution

ML Normal Data Distribution

In the previous lesson every value was equally likely. Nature rarely works that way. Measure the heights of adults, the weight of apples from one orchard or the error of a kitchen scale, and you will find most values bunched around an average, with fewer and fewer values the further you go from it. This bell-shaped pattern is the normal distribution, also called the Gaussian distribution.

Generating normal data

A normal distribution is described by two numbers:

  • the mean (μ, mu): where the peak of the bell is,
  • the standard deviation (σ, sigma): how wide the bell is.

NumPy's generator draws normal values with rng.normal(loc=mean, scale=std, size=n):

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
apple_grams = rng.normal(loc=180, scale=15, size=5000)

print("first five:", apple_grams[:5].round(1))
print(f"mean = {apple_grams.mean():.2f}")
print(f"std  = {apple_grams.std():.2f}")
print(f"min  = {apple_grams.min():.1f}, max = {apple_grams.max():.1f}")
first five: [184.6 164.4 191.3 194.1 150.7]
mean = 179.70
std  = 14.99
min  = 125.3, max = 231.8

We asked for a mean of 180 g and a standard deviation of 15 g, and the sample comes out very close to those values. With more samples it would get even closer.

The bell curve

A histogram of normal data shows the famous bell shape:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
apple_grams = rng.normal(loc=180, scale=15, size=5000)

plt.hist(apple_grams, bins=40, color="#f7931e", edgecolor="white")
plt.axvline(apple_grams.mean(), color="black", linestyle="--", label="mean")
plt.xlabel("Apple weight (g)")
plt.ylabel("Count")
plt.title("5,000 apples: a normal distribution")
plt.legend()
plt.show()
print("Histogram drawn: a symmetric bell centred near 180 g")
Histogram drawn: a symmetric bell centred near 180 g

The curve is symmetric: the left and right halves mirror each other, so the mean, median and mode all sit at the centre.

The 68–95–99.7 rule

The normal distribution has a very useful property. Roughly:

Range Share of values
mean ± 1σ about 68 %
mean ± 2σ about 95 %
mean ± 3σ about 99.7 %

Let's check it on our simulated apples:

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
apples = rng.normal(loc=180, scale=15, size=5000)
mean, std = apples.mean(), apples.std()

for k in [1, 2, 3]:
    inside = np.abs(apples - mean) <= k * std
    print(f"within {k} std: {inside.mean() * 100:.1f} %")
within 1 std: 68.6 %
within 2 std: 95.3 %
within 3 std: 99.7 %

inside is an array of True/False, and the mean of booleans is the fraction of True values — a neat NumPy trick.

This rule is the reason the "more than 2 standard deviations away" check from the Standard Deviation lesson works: in normal data only about 5 % of values are that far out.

Exact probabilities with SciPy

scipy.stats.norm represents the ideal (theoretical) normal curve. Its cumulative distribution function cdf(x) gives the probability of a value at most x:

python · try it yourself
from scipy.stats import norm

apple = norm(loc=180, scale=15)

print(f"P(weight <= 180) = {apple.cdf(180):.3f}")
print(f"P(weight <= 150) = {apple.cdf(150):.4f}")
print(f"P(165 <= weight <= 195) = {apple.cdf(195) - apple.cdf(165):.4f}")
print(f"Heaviest 10 % start at {apple.ppf(0.90):.1f} g")
P(weight <= 180) = 0.500
P(weight <= 150) = 0.0228
P(165 <= weight <= 195) = 0.6827
Heaviest 10 % start at 199.2 g

ppf (percent point function) is the inverse of cdf: it turns a probability back into a value — the theoretical 90th percentile here.

Why ML cares about normal data

  • Many models (linear regression, linear discriminant analysis, Gaussian naive Bayes) assume that values or errors are roughly normal.
  • Standardising features (the Scale lesson) produces z-scores, and for normal data those z-scores follow the standard normal distribution with mean 0 and σ 1.
  • When you check a model's errors, a bell shape centred on 0 is a good sign. A skewed or lopsided error histogram hints that the model is missing a pattern.
python · try it yourself
import numpy as np

rng = np.random.default_rng(1)
heights_cm = rng.normal(172, 8, size=1000)
z = (heights_cm - heights_cm.mean()) / heights_cm.std()

print(f"z mean = {z.mean():.3f}, z std = {z.std():.3f}")
print("share of |z| > 2:", round((np.abs(z) > 2).mean(), 3))
z mean = -0.000, z std = 1.000
share of |z| > 2: 0.046

Note: Not everything is normal. Incomes, waiting times and counts are often skewed. Always plot a histogram before assuming a bell curve.

Test yourself

Which two numbers fully describe a normal distribution?

  1. Minimum and maximum
  2. Mean and standard deviation
  3. Median and mode
  4. Count and sum

About what share of normally distributed values lie within two standard deviations of the mean?

  1. 50 %
  2. 68 %
  3. 95 %
  4. 100 %

Which call draws 100 values from a normal distribution with mean 50 and standard deviation 5?

  1. rng.normal(5, 50, 100)
  2. rng.normal(loc=50, scale=5, size=100)
  3. rng.uniform(50, 5, 100)
  4. rng.integers(45, 55, 100)