Skip to content

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

PythonPython not started

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?

PythonPython not started

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.

PythonPython not started

dtypes

An array is one type throughout. Choosing it is a real decision:

PythonPython not started

Precision is what you pay for the saving:

PythonPython not started
PythonPython not started

Integer types overflow silently, which is worth seeing once:

PythonPython not started

Reshaping

reshape reinterprets the same numbers with a different shape. -1 means “work this one out”:

PythonPython not started

Transposing swaps axes, and T is the shorthand you will use constantly:

PythonPython not started

Adding a length-1 axis is how you make shapes line up for broadcasting, which is the next lesson:

PythonPython not started

Stacking and joining

PythonPython not started

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:

PythonPython not started

Try it yourself

Exercise 1

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.

Your turnPython not started
Exercise 2

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.

Your turnPython not started

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; T transposes; [:, np.newaxis] adds a length-1 axis.
  • concatenate joins along an existing axis, stack adds a new one — np.stack is how a list of embeddings becomes a batch.
  • Basic slicing, reshape and T give views; fancy indexing copies. .copy() when you mean it, and np.shares_memory when you are unsure.
  • float32 halves the memory of float64 and loses nothing an embedding model actually produced.