Matrices
Tables of numbers.
A vector is one row of data. Stack many vectors together, and you get a matrix, a rectangle of numbers. Your whole dataset is a matrix, with one row per example, and one column per feature.

- Stack vectors together
- A rectangle of numbers
- Your whole dataset
A data matrix
Here are four houses, each with size, rooms, and age. The shape is four by three, four rows and three columns. We can pick a row, which is one house, or a column, which is one feature. And dot T, the transpose, flips rows and columns. Run it.
import numpy as np
X = np.array([[120, 4, 10],
[ 80, 3, 35],
[200, 6, 2],
[150, 5, 20]])
print("shape:", X.shape)
print("house 0:", X[0])
print("sizes:", X[:, 0])
print("transpose shape:", X.T.shape)shape: (4, 3) house 0: [120 4 10] sizes: [120 80 200 150] transpose shape: (3, 4)
Rows and columns
By convention, a matrix with M rows and N columns is called M by N. In machine learning, the data matrix is usually called capital X, with rows as examples and columns as features. Images are matrices too, with one number per pixel. A colour image is three matrices stacked together, for red, green, and blue.
| key | value |
|---|---|
| m × n | m rows, n columns |
| X | examples × features |
| X[:, j] | one feature |
| X.T | the transpose |
Summary
A matrix is a table of numbers. Your dataset is a matrix, X, with examples as rows and features as columns. Next, multiplying matrices.
Key takeaways- shape (rows, cols)
- X[i] and X[:, j]
- X.T flips it