FoundationsBeginner5h

Data structures for ML.

Arrays, tensors, and shapes — thinking in dimensions.

What are the data structures for ML?

ML data lives in n-dimensional arrays — NumPy arrays and, later, tensors. A grayscale image is a 2D array, a batch of color images is 4D. Thinking clearly about shapes and dimensions is the core skill that makes the rest of ML code make sense.

Why it matters

Most ML bugs are shape bugs: a matrix that is transposed, a batch dimension in the wrong place, an array that is one dimension off. Fluency with array shapes turns these from baffling errors into quick fixes. It is the practical literacy under every model.

What to learn

  • N-dimensional arrays and the idea of shape
  • Axes and what each dimension represents
  • Indexing, slicing, and selecting
  • Reshaping and transposing
  • Broadcasting rules
  • The batch dimension convention
  • Reading a shape error message

Common pitfall

Ignoring the batch dimension and feeding a model a single sample shaped like one example instead of a batch of one. The model expects an extra leading dimension, and you get a confusing shape error. Always know whether you are holding one sample or a batch, and add the batch axis when needed.

Resources

Primary (free):

Practice

In NumPy, create a 3D array representing a batch of small images, then practice: print its shape, select one image, reshape it, transpose two axes, and add a batch dimension to a single sample. Trigger a shape mismatch on purpose and read the error. Done when shapes stop surprising you.

Outcomes

  • Reason about array shapes and what each axis means.
  • Index, slice, reshape, and transpose confidently.
  • Apply broadcasting rules correctly.
  • Diagnose a shape error from its message.
Back to AI / ML roadmap