5.4.2 · D1Scientific Computing (Python)

Foundations — Array creation — np.zeros, np.ones, np.linspace, np.arange, np.random

2,549 words12 min readBack to topic

This page assumes nothing. Before you meet np.zeros or np.linspace, we build — from the ground up — every symbol, word, and picture the parent note quietly leaned on. Read top to bottom; each block earns the next.


0. The very first object: a number line

Everything here lives on a number line — a straight horizontal ruler where left is small, right is big, and each position is one number.

Why the topic needs it: np.linspace(0, 1, 5) and np.arange(0, 1, 0.25) both place points on this line. The whole difference between them is which points and whether the right end is kept.

Recall What do the brackets mean?

includes 0 but not 1 ::: left end kept, right end thrown away.


1. What is a "buffer of one dtype"?

The parent note says an array is "a contiguous block of memory of the same dtype." Let's unpack every word.

The picture: compare a Python list (top row) — each cell is a pointer wandering off to a separate boxed object — with a NumPy buffer (bottom row) — the actual numbers packed in a straight line. Same numbers, wildly different layout.

Why the topic needs it: np.zeros(10**6) is fast precisely because it reserves one clean strip and writes zeros into it. And "default dtype is float64" only makes sense once you know a dtype is a fixed-width promise.

For the full story see NumPy arrays — shape, dtype, ndim and Floating-point representation and rounding errors.


2. What is a "shape"?

A buffer is just a line of numbers. Shape is how we interpret that line as a grid.

The picture: the same 12 numbers in the flat buffer get folded into 3 rows of 4. Nothing about the memory changes — only how we picture reading it.

Recall What does the tuple

(2, 3) mean as a shape? 2 rows and 3 columns ::: a 2-D grid of 6 numbers.

Reshaping this fold without moving memory is its own topic: Reshaping — reshape, ravel, newaxis.


3. Indexing: naming a single cell

To fill a preallocated array (the "pro pattern" in the parent), you must point at one cell.

Why the topic needs it: the loop for i in range(5): out[i] = i**2 walks and drops a value into each slot. If you didn't know indexing starts at 0, the fencepost counting below would confuse you. See Array indexing and slicing.


4. Counting evenly-spaced points (the fencepost idea)

This single idea powers both linspace and arange. It is worth a picture.

Let's earn every symbol the parent uses before we write the formulas.

WHAT we do: with points and both ends kept, we have gaps to share the total width . WHY we divide by : because gaps = posts − 1 (the fencepost picture). WHAT IT LOOKS LIKE: in the figure, 5 posts on split the length 1 into 4 equal gaps of .

Here literally means "start at , then take full gaps of size " — so (no gaps yet) and (all gaps used).

That is the linspace engine. For arange we instead fix the step and ask how many points fit before we pass :

Why ceiling? We keep taking steps until we would cross ; a partial final step still counts as one more index, so we round up. And because the stop is excluded, a value landing exactly on is dropped.

Recall Why

and not in the (endpoint-kept) linspace gap? Because points make gaps between them (fenceposts) ::: only the gaps get an equal share of the length.

Recall What does

np.arange(5, 0, -1) produce? A descending range ::: 5, 4, 3, 2, 1 (stop 0 is excluded).


5. Two more symbols: [0, 1) for random, and "loop vs vectorized"

Deeper in Vectorization vs Python loops and Random sampling and distributions.


6. How the foundations feed the topic

Number line and intervals

Even spacing and fenceposts

Buffer of one dtype

Shape as a tuple

Indexing from zero

Array creation functions

Preallocate then fill

linspace and arange

Half-open interval for random

np.random


Equipment checklist

Test yourself — you are ready for the parent note when you can answer all of these out loud.

What does a round bracket ) mean in an interval like [0, 1)?
The right end is excluded (0 kept, 1 not).
Why is a NumPy buffer faster than a Python list of the same numbers?
It stores the numbers contiguously with one fixed dtype, no per-element pointer objects.
What is a dtype in one sentence?
A promise that every cell holds the same kind of number using the same byte-width (e.g. float64).
How do you write the shape "3 rows, 4 columns" as one argument?
As a tuple (3, 4).
What is the literal 1-D shape tuple for a length-3 array, and why the trailing comma?
(3,); the comma makes Python treat it as a one-element tuple rather than the plain number (3).
Why does np.zeros(3, 4) fail?
Shape must be one tuple; the second slot is dtype, so 4 is misread as a data type.
What index does the first element of an array have?
0 (the last of N is N−1).
For 5 points on a line, how many gaps are there between them?
4 (posts minus one).
What do the symbols and stand for?
= number of linspace points; = the fixed step size for arange.
Spacing for num points with both ends kept?
.
Spacing when endpoint=False?
(stop dropped, so one more gap).
What does denote?
The -th point, , with .
What does do?
Rounds up to the next whole number.
Number of elements in arange(a, b, s)?
, with excluded.
What does np.arange(0, 1, -1) return, and why?
An empty array — a negative step can't reach a stop above the start.
What happens with step=0 in arange?
Error (division by zero); a zero step never advances.
What does "vectorization" mean here?
Making/operating on the whole array at once instead of looping element by element.

Connections

  • Parent topic
  • NumPy arrays — shape, dtype, ndim
  • Array indexing and slicing
  • Reshaping — reshape, ravel, newaxis
  • Vectorization vs Python loops
  • Random sampling and distributions
  • Floating-point representation and rounding errors