NumPy·Lesson 2·8 min·0/3 exercises

Making arrays

zeros, ones, arange, linspace, eye, random — pick a shape, then a filling rule.

np.zerosnp.onesnp.fullnp.arangenp.linspacenp.eyedefault_rng

Watch it happen

Play it through, or step back and forth yourself.

np.zeros((3, 4))
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
shape (3, 4)dtype float64
  • np.zeros((3, 4))
  • np.ones((3, 4))
  • np.full((3, 4), 7)
  • np.arange(12).reshape(3, 4)
  • np.linspace(0, 1, 12).reshape(3, 4)
  • np.eye(3)
  • rng.random((3, 4))

np.zeros((3, 4))Empty canvas. Note the dtype: float64, not int — zeros gives you floats unless you ask otherwise with dtype=int.

The idea

You'll rarely type an array out by hand. Nearly every array starts as one of a handful of constructors, and they all follow the same pattern: choose a shape, then choose how to fill it.

np.zeros(shape) and np.ones(shape) are the workhorses. Note the double brackets in np.zeros((3, 4)) — the shape is a single tuple argument, not two separate ones. Forgetting that is the most common first-day error with these.

Both give you float64. If you wanted integers, say so: np.zeros((3, 4), dtype=int).

arange and linspace

These two look similar and answer different questions. np.arange(0, 10, 2) takes a step and, like Python's range, excludes the stop. np.linspace(0, 10, 5) takes a count and includes both ends.

np.arange(0, 10, 2)     # [0 2 4 6 8]        — stop excluded
np.linspace(0, 10, 5)   # [0. 2.5 5. 7.5 10.] — both ends included

Reach for linspace whenever you know how many points you want — plotting a smooth curve, sampling a range evenly. Reach for arange when you care about the gap between values. And avoid arange with a float step: floating-point drift means you can't reliably predict how many elements you'll get.

Hand-drawn notes comparing arange, which excludes its stop and takes a step, with linspace, which includes its stop and takes a count.

Random numbers

Modern NumPy wants you to make a generator first:

rng = np.random.default_rng(0)   # 0 is the seed
rng.random((3, 4))               # floats in [0, 1)
rng.integers(0, 10, size=(3, 4)) # ints in [0, 10)
rng.normal(size=5)               # standard normal

You'll still see the older np.random.rand style in tutorials; it works, but the generator form is the one to learn. Passing a seed makes the output reproducible — the same numbers every run, which is exactly what you want in a lesson, a test, or anything you plan to debug.

Hand-drawn notes showing that a seeded generator returns the same numbers every run, while an unseeded one differs each time.

Practice

Write it yourself. The answer is there when you want it.

Putting the kettle on…

Starting up…

Write it yourself

not graded

Make three arrays without typing a single value into them: a (2, 3) of zeros, the even numbers below 10, and five evenly spaced points from 0 to 1. Print each. Then make a seeded generator with np.random.default_rng(0) and finish with a (3, 4) of random integers under 100.

Write something and press Run — the output appears here.

Your turn

3 exercises. Write the code yourself, then press Check — a nudge and the answer are there if you want them.

Make a (2, 5) array of zeros with dtype int.

your answer

Use np.arange to make [10, 20, 30, 40, 50].

your answer

Use np.linspace for five evenly spaced values from 0 to 1, including both ends.

your answer