ModelRefs / Vectors & Matrices — Tutorial

Vectors & Matrices — Tutorial

The linear algebra primitives that underlie every ML model. Covers Vectors are coordinates in feature space, Matrices as data and transformations.

Overview

The linear algebra primitives that underlie every ML model

Level: Beginner. Estimated reading time: 20 minutes.

Vectors are coordinates in feature space

A vector is an ordered list of numbers. In ML, a row of your dataset is a vector. A photo of 28×28 pixels becomes a 784-dimensional vector. A word embedding is a 1536-dimensional vector.

Vectors have two key properties: magnitude (length) and direction. The dot product of two vectors measures how similar their directions are — the core operation behind attention mechanisms and semantic search.

Matrices as data and transformations

A matrix is a 2D array of numbers. Your training dataset is a matrix of shape (n_samples, n_features). A weight matrix in a neural network layer transforms an input vector into an output vector.

Matrix multiplication AB means: for each row of A and each column of B, compute a dot product. Shape rule: if A is (m, k) and B is (k, n), the result is (m, n). The inner dimensions must match.

Why shapes matter more than values

Most ML bugs are shape errors. Always check .shape on your arrays before training. A matrix of (1000, 10) means 1000 samples, 10 features. Transposing accidentally (10, 1000) gives nonsensical results silently.

Broadcasting: NumPy allows operations between arrays of different shapes if they are compatible (a (3,) vector can broadcast against a (5,3) matrix, applying the vector to each of the 5 rows).

Continue your research

Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to Vectors & Matrices — Tutorial.