Python for data · lesson 7 of 11
Parts in this lesson
Arng = np.random.default_rng(42)a random number generator, with a seed
Brng.normal(0, 1, size=4)numbers from the bell curve
Crng.permutation(10)0 to 9 in a random order
DX[[0, 3]]0.3: rows picked by position

Random numbers

Last updated

Machine learning uses randomness everywhere: a model's weights start as random numbers, data is shuffled before it's split into training and test sets, and training picks random batches. If the randomness changes every run, so do your results, and you can't tell a real improvement from luck. In this lesson you'll make random numbers with NumPy, make them repeatable with a seed, and shuffle and split data.

By the end of this lesson you can

  • Make uniform, whole-number and normal random numbers with a Generator
  • Use a seed so a run gives the same random numbers every time
  • Shuffle positions with permutation to split data into training and test sets
  • Explain why an experiment without a seed can’t be checked

A generator and its numbers

NumPy makes random numbers with a generator. You make one with np.random.default_rng, giving it a number called a seed, then ask it for numbers. Run each cell:

random.ipynb
  • rng.random(3): three decimals between 0 and 1, every value equally likely (uniform).
  • rng.integers(1, 7, size=10): ten whole numbers from 1 up to but not including 7, like ten throws of a die.
  • rng.normal(0, 1, size=4): four numbers from the normal distribution, the bell curve, centred on 0 with a spread (standard deviation) of 1. Most are close to 0; a few are further out.

A computer can't make truly random numbers from arithmetic. A generator makes numbers that look random, by a fixed method from its seed. That's why they're called pseudo-random, and why the same seed always gives the same numbers: the next step shows it.

You may see older code that calls np.random.seed(42) and np.random.rand(). NumPy's documentation recommends default_rng for new code, and this course uses it.

The bell shape

Normal numbers matter because so much in the world, and so many starting weights in models, are drawn from that shape. Watch it appear as more numbers are drawn:

Normal samples filling a histogram. 10 days of simulated sales, each a random number around 48 kg. With so few, you can’t tell what shape they come from.10 days30343842465054586266
1 of 4 10 days of simulated sales, each a random number around 48 kg. With so few, you can’t tell what shape they come from.

Now draw 10,000 penguin masses from a normal distribution with mean 4200 g and standard deviation 800 g, and count them into 8 bins (equal-width ranges):

histogram.ipynb

The sample's mean and spread are close to 4200 and 800, but not exact: 10,000 random draws never land perfectly. The counts are small at both ends and big in the middle: the bell. Lesson 0.8 draws it as a picture.

Why seeds matter

Make two generators with the same seed and ask each for three numbers:

seed.ipynb

The same three numbers, both times. This is what makes an experiment repeatable. When you train a model and get 94% accuracy, then change one thing and get 95%, you need to know the change did it, and not a luckier set of starting weights. With a fixed seed, everything random is the same in both runs, so the only difference is the one you made.

The examples in this course all fix their seeds, so your output matches the page. The number itself doesn't matter: 0, 7 and 42 are all common, and none is better than another.

Shuffling and splitting

Here's the randomness every model needs. To check a model fairly, you hold back some examples it never trains on: the test set. Which examples go where must be random, or the split could put all of one species in the test set. Run it:

split.ipynb
  • rng.permutation(10) is the positions 0 to 9 in a random order.
  • The first 8 positions are the training set, the last 2 the test set. With a data matrix, X[train] and X[test] pick those rows (lists of positions, from lesson 0.3).
  • rng.choice picks at random from a list, here five species names.
  • rng.normal(0, 0.01, size=3) is how a model's weights often start: small random numbers near zero. Stage 7 explains why small and why random.

Stage 2 does this split properly, with scikit-learn, and explains why the test set must stay untouched. The idea is just this shuffle.

Wrong way: no seed

Make two generators without a seed and compare one number from each:

noseed.ipynb

False. With no seed, default_rng() takes a fresh seed from the computer each time, so every run is different. Run the cell again and the numbers change.

Now picture two training runs a week apart. The first scored 91%, the second 93%. You'd changed the learning rate in between. Did that help? Without a seed you can't say: the starting weights and the shuffle were different too, and on a small dataset that alone can move accuracy by a few points.

Set the seed once, at the top of the notebook, and pass the same rng everywhere you need randomness. When you do want to know how much luck matters, run the experiment with several seeds on purpose and compare. Stage 7 shows how.

Check what you learned

What can rng.integers(1, 7) give?
Why fix the seed before training a model?
Predict, then check in step 3’s notebook: np.random.default_rng(7).random(3) run in two different notebooks on two computers. Same numbers?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning