Scalars vectors and shapes
Quick answer
A scalar is a single number, such as a delivery fee. A vector is an ordered collection of numbers, such as distance and parcel weight. A matrix is a rectangular grid of numbers; in this chapter each row is a sample and each column is a feature. In numerical ML, a tensor is commonly represented as an array with any number of axes, including a scalar (zero axes), vector (one), and matrix (two). Shape records the size of each axis, not the total number of elements. A shape of (3, 2) means three rows and two columns.
Read a small feature table
Use ordinary Python lists first. Indexing starts at zero: row 1 is the second sample and column 0 is its distance. len counts items in a list. The table is nonempty and rectangular.
samples = [[2, 1], [5, 3], [4, 2]]print(len(samples), len(samples[0]))print(samples[1][0])print(samples[0])Expected output:
3 25[2, 1]There are three parcels and two features. The second parcel has distance 5; the first has distance 2 and weight 1.
Inspect shapes and transpose with NumPy
Optional local lab (Python 3.13.5, NumPy 2.3.5, CPU). np.array converts a rectangular list into an array; .shape gives axis sizes and .T swaps the axes of a two-dimensional array. A one-dimensional array has neither a separate row nor column axis. reshape(2, 1) introduces an explicit column axis without changing values.
import numpy as npX = np.array([[2, 1], [5, 3], [4, 2]])v = np.array([2, 1])print(np.array(7).shape, v.shape, X.shape)print(X.T.tolist())print(v.T.shape, v.reshape(2, 1).shape)print(np.zeros((2, 3, 4)).shape)Expected output:
() (2,) (3, 2)[[2, 5, 4], [1, 3, 2]](2,) (2, 1)(2, 3, 4)Transpose makes features rows, but does not exchange the meaning of the underlying quantities. The final tensor has three axes, for example batch, time and feature, with 24 entries.
Common mistakes
Do not infer that a length-two vector has shape (1, 2). That would be a two-dimensional row matrix. A ragged list is not a rectangular feature matrix. Keep feature order consistent across samples and document what each axis represents.
Practice and next steps
Write the shape of a table with four samples and three features. Transpose it and name the new axes. Next, combine feature values with a weighted dot product.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Scalars vectors and shapes — knowledge check
Apply linear algebra to small numerical fixtures.
A table has 8 samples and 3 features. What is its shape?
What is the shape of a NumPy scalar?
Which expression selects the second row, first column of a nested list X?
What is the shape after transposing a 3-by-5 matrix?
What is the shape of a NumPy 1-D array with four values after .T?