Numerical Python·Lesson 20 of 67·intermediate
Arrays, shapes and dtypes
The three questions to ask about any array — what shape, what dtype, is it a view — and why float32 halves the memory bill of every embedding you store.
In this lesson · 18 min
- →Create arrays and read their shape, ndim, size and dtype
- →Choose a dtype deliberately, and know what float32 costs and saves
- →Reshape, transpose and add axes without copying data
- →Stack and concatenate arrays along the right axis
- →Tell a view from a copy, and avoid changing data you did not mean to
A Python list holds references to objects scattered across memory. A NumPy array holds raw numbers of one type, side by side. That single difference is what makes array code both far faster and far more memory-efficient — and it is the model every tensor library inherits.
Making arrays
arange counts by step and excludes the stop, like range. linspace takes the number of
points you want and includes both ends — the one you want for plotting and for sweeping a
hyperparameter.
The three questions
Whenever an array surprises you, ask: what shape, what dtype, and is it a view?
Shape is a tuple, read outside-in: (32, 768) is 32 rows of 768 numbers. In AI code the first
axis is almost always the batch.
dtypes
An array is one type throughout. Choosing it is a real decision:
Precision is what you pay for the saving:
Integer types overflow silently, which is worth seeing once:
Reshaping
reshape reinterprets the same numbers with a different shape. -1 means “work this one out”:
Transposing swaps axes, and T is the shorthand you will use constantly:
Adding a length-1 axis is how you make shapes line up for broadcasting, which is the next lesson:
Stacking and joining
concatenate joins along an axis that already exists; stack creates a new one. Reach for
np.stack(list_of_embeddings) whenever you turn per-item vectors into a batch.
Views and copies
Slicing an array does not copy it. You get a view onto the same memory, and writing through the view changes the original:
Try it yourself
Build batch: the numbers 0 to 11 as a float32 array of shape (3, 4). Then print its shape, dtype and total bytes, one per line.
Turn the list of three 1-D embeddings into one (3, 4) float32 batch, then print saved: N — the bytes saved versus keeping it in float64.
Check yourself
window = batch[0:2] and then window[0, 0] = 99. What happened to batch?
Check yourself
You store one million 1536-dimension embeddings. Why float32 rather than float64?
Recap
- An array holds one dtype in contiguous memory; that is where the speed and the memory savings come from.
- Ask three questions: what shape, what dtype, is it a view.
reshape(-1)infers a dimension;Ttransposes;[:, np.newaxis]adds a length-1 axis.concatenatejoins along an existing axis,stackadds a new one —np.stackis how a list of embeddings becomes a batch.- Basic slicing,
reshapeandTgive views; fancy indexing copies..copy()when you mean it, andnp.shares_memorywhen you are unsure. - float32 halves the memory of float64 and loses nothing an embedding model actually produced.