Distributions and plotting
Sample common distributions and chart them with Matplotlib.
Why it matters: Weight init and train/val curves come from distributions. A plot catches problems numbers hide.
Basics
Common distributions
Use np.random.default_rng(seed) for a seeded generator.
- Uniform:
rng.uniform(low, high, size) - Normal:
rng.normal(loc, scale, size) - Integers:
rng.integers(low, high, size) - Choice:
rng.choice(options, size, p=...)
Seed once so experiments are repeatable.
Tiny example
Sample a few normal values and print the mean.
import numpy as np
rng = np.random.default_rng(0)
samples = rng.normal(loc=0.0, scale=1.0, size=5)
print(np.round(samples, 3))
print("mean", float(samples.mean()))Basics
Plotting with Matplotlib
Prefer fig, ax = plt.subplots() then ax.hist / ax.plot / ax.scatter.
Common charts:
- histogram - shape and outliers
- scatter - predicted vs actual
- line - training curves
- bar - class counts
Always set title, xlabel, and ylabel.
Tiny example
Build a tiny series you could histogram.
import numpy as np
vals = np.array([0.1, 0.2, 0.2, 0.8, 0.9])
print("bins idea:", np.histogram(vals, bins=3)[0])Basics
Reading what you plot
A bimodal histogram often means two populations mixed together.
Predicted vs actual should hug the diagonal.
If train loss falls while val loss rises, the model is overfitting.
Tiny example
Print train vs val loss to spot overfitting.
train = [0.9, 0.5, 0.2, 0.05]
val = [0.85, 0.55, 0.45, 0.50]
print("train", train)
print("val ", val)
print("gap grows:", val[-1] - train[-1])Then AI / ML
AI / ML examples
Same idea, used in real AI work. Press Try in lab to run it.
AI / ML example
AI / ML: latency histogram
The chart renders below the output.
In AI / ML: Request latency and score distributions are plotted before tuning thresholds.
example.py
Python
import matplotlib.pyplot as plt
import numpy as np
rng = np.random.default_rng(0)
latencies = np.concatenate([rng.normal(200, 30, 900), rng.normal(1200, 200, 100)])
fig, ax = plt.subplots(figsize=(7, 3.5))
ax.hist(latencies, bins=40, color="#1d4ed8", edgecolor="white")
ax.axvline(np.median(latencies), color="#ea580c", label="median")
ax.set_title("Request latency")
ax.set_xlabel("ms")
ax.set_ylabel("count")
ax.legend()
print("median", round(float(np.median(latencies)), 1))AI / ML example 2
AI / ML: train vs val loss curve
The growing gap is overfitting.
In AI / ML: Stop at the best validation epoch instead of memorising train data.
example.py
Python
import matplotlib.pyplot as plt
import numpy as np
epochs = np.arange(1, 16)
train = 1.2 * np.exp(-0.25 * epochs) + 0.05
val = train + 0.01 * np.clip(epochs - 6, 0, None) ** 1.4
fig, ax = plt.subplots(figsize=(7, 3.5))
ax.plot(epochs, train, label="train")
ax.plot(epochs, val, label="val")
ax.set_title("Loss curves")
ax.set_xlabel("epoch")
ax.set_ylabel("loss")
ax.legend()
print("best val epoch", int(np.argmin(val)) + 1)Takeaways
- 1Seed with np.random.default_rng(seed) for repeatable samples.
- 2Use fig, ax = plt.subplots() and always label axes.
- 3Histogram for shape, scatter for pred vs actual, line for training curves.