Scalars, Vectors and Tensors
To you, flat B is "a two-room flat near the metro". To a model it is just [60, 2, 1]. Everything a model sees, from one rent to a year of photos, is numbers arranged in a grid of some number of dimensions. Naming those arrangements correctly is the first step to reading any ML code.
The rule
Count the indexes you need: 0 scalar, 1 vector, 2 matrix, more tensor.
An index is a position you must give to pick out one number.
- Scalar: a single number, no index needed. Flat B's rent, 22.
- Vector: a list, one index. Flat B's features,
[60, 2, 1]: size, rooms, metro km. Its third entry is 1. - Matrix: a table, two indexes (row, column). All five flats, one row each.
- Tensor: three or more indexes. The same table for 12 months needs month, flat and feature.
Deep-learning libraries such as PyTorch and TensorFlow call every one of these a tensor, including scalars and vectors, because their code treats them all the same way.
A shape lists how many positions each index has. The number of indexes is the rank (NumPy calls it ndim).
import numpy as np
rent_b = np.array(22) # shape () scalar
flat_b = np.array([60, 2, 1]) # shape (3,) vector
X = np.array([[40, 1, 2],
[60, 2, 1],
[80, 3, 3],
[50, 2, 4],
[70, 2, 1]]) # shape (5, 3) matrixThe workhorse operation on vectors is the dot product: multiply matching entries and add. With weights w = [0.3, 1, -1], flat B scores . A linear model's prediction is exactly this: features dotted with weights. Two vectors can only be dotted if they have the same length.
The shortcut
Read a shape aloud as "how many of what". (5, 3) is "5 flats, 3 features each". The total count of numbers is the product of the shape.
Name each axis
The agency keeps the five-flat table for each of 12 months. The axes are month, flat, feature.
A common slip is calling any grid "a matrix". A colour photo 224 pixels square has three colour channels, so its shape is (224, 224, 3): a tensor, not a matrix.
Drills
Answer fast — trust the rule.
What is the shape of flat D's feature vector (50 m², 2 rooms, 4 km)?
How many numbers are in a tensor of shape (12, 5, 3)?
With weights [0.3, 1, -1], what is the dot product with flat A's features [40, 1, 2]?
A colour photo 224 by 224 pixels with 3 colour channels is best described as a:
Remember
- Count the indexes: 0 scalar, 1 vector, 2 matrix, 3 or more tensor.
- Shortcut: read the shape as "how many of what"; multiply it for the total.
- Trap: calling every grid a matrix, and dotting vectors of different lengths.
