Tensor Shapes and Dimensions
A scalar, vector, and matrix differ by the number of axes they have.
Finding a Value by Counting Axes
Before interpreting a tensor operation, ask a structural question: how many axes are needed to locate one value? A single number needs no axis. A one-dimensional array of numbers needs one axis. An array of vectors needs two axes. These structures are called a scalar tensor, a vector tensor, and a matrix tensor.
Rank as Axis Count
The rank of a tensor is the number of axes it has. This gives a direct naming rule: a scalar has rank 0, a vector has rank 1, and a matrix has rank 2. Rank does not describe the numerical values themselves. It describes how many directions are needed to locate one value.
| Tensor type | Rank | Axes needed to locate a value | Arrangement |
|---|---|---|---|
| Scalar tensor | 0 | None | One number |
| Vector tensor | 1 | One position along one axis | An array of numbers |
| Matrix tensor | 2 | One row position and one column position | An array of vectors arranged across rows and columns |
Rank identifies the number of axes in each tensor type.
Reading Values Along One Axis
A vector is an array of numbers arranged along one axis. To identify one value, you need one position along that axis. The important idea is not the particular values in the vector; it is that one position is sufficient to locate any value.
Locating a Value in a Vector
Consider the generated vector [8, 3, 6]. How many axis positions are needed to identify the value 3?
Classify the structure: The values are arranged as one array of numbers, so the structure is a vector tensor.
Count the axes: A vector has one axis. Each value is located by one position along that axis.
Locate the value: The value 3 is identified by its single position in the vector; no row position and column position are needed.
One axis position identifies the value 3, so the vector has rank 1.
A vector is not identified by rows and columns. It has one axis, so one position along that axis identifies a value.
Reading Rows and Columns
A matrix is an array of vectors arranged across rows and columns. Because it has two axes, locating one value requires two positions: one along the row axis and one along the column axis. This two-axis arrangement makes a matrix behave like a rectangular grid.
Locating a Value in a Matrix
Consider the generated matrix with rows [4, 9] and [2, 7]. How many positions are needed to identify the value 7?
Classify the structure: The values are arranged in rows and columns, so the structure is a matrix tensor.
Identify the two axes: One axis corresponds to the row arrangement and the other corresponds to the column arrangement.
Locate the value: The value 7 is identified by its row position and its column position. Both positions are required.
Two axis positions identify the value 7, so the matrix has rank 2.
Checking Axes with ndim
NumPy's ndim attribute reports the number of axes in a tensor. It provides a concrete structural checkpoint: a scalar tensor has ndim equal to 0, a vector tensor has ndim equal to 1, and a matrix tensor has ndim equal to 2.
| Tensor structure | Number of axes | Rank | ndim result |
|---|---|---|---|
| Scalar | 0 | 0 | 0 |
| Vector | 1 | 1 | 1 |
| Matrix | 2 | 2 | 2 |
The ndim result matches the tensor's rank because both count axes.
- Classify the tensor as a scalar, vector, or matrix.
- Ask how many positions are needed to locate one value.
- Use the number of axes as the tensor's rank.
- Check the NumPy ndim attribute when you need a concrete confirmation.
- If the result differs from what you expected, investigate the tensor's structure before investigating later operations.
Mistakes with Axes and Rank
Treating rank as the number of values
Rank means the number of axes, not the amount of data stored along those axes.
Fix:
Count the directions needed to locate one value. A vector has one axis and rank 1.Calling a vector a matrix because it contains many values
A matrix requires two axes and is arranged across rows and columns.
Fix:
Check whether values need both a row position and a column position. If one position is enough, the structure is a vector.Ignoring the row axis or column axis in a matrix
A matrix value is located using two positions, one along each of its two axes.
Fix:
Identify both the row position and the column position.Assuming a later operation caused every unexpected result
The tensor may already have had a different structural category than expected.
Fix:
Check whether the value is a scalar, vector, or matrix, then inspect ndim in NumPy.
Practice: Classify Before Operating
For each description, identify whether it is a scalar tensor, vector tensor, or matrix tensor. Then state its rank and the number of positions needed to locate one value: a single number; one array of numbers; an array of vectors arranged in rows and columns.
Hints
- A single number needs no axis.
- A vector needs one position along one axis.
- A matrix needs a row position and a column position.
Practice Check
A tensor is described as an array of vectors. What tensor type is it, what is its rank, and what would NumPy's ndim attribute report?
Classify the arrangement: An array of vectors is the matrix case.
Count the axes: The matrix uses two axes: one for rows and one for columns.
Map the count to rank and ndim: Two axes mean rank 2, and NumPy's ndim attribute reports 2.
It is a matrix tensor with rank 2 and ndim equal to 2.
Summary
- A scalar is a single number that needs no axis, so it has rank 0.
- A vector is an array of numbers arranged along one axis, so it has rank 1.
- A matrix is an array of vectors arranged across rows and columns, so it has rank 2.
- Rank means the number of axes in a tensor.
- NumPy's ndim attribute reports the tensor's number of axes and therefore provides a direct check of its rank.
Key Takeaways
- Count axes by asking how many positions are needed to locate one value.
- Scalar, vector, and matrix tensors have ranks 0, 1, and 2 respectively.
- A vector uses one axis, while a matrix uses row and column axes.
- NumPy's ndim attribute reports the number of axes.
- Checking tensor structure early can reveal a mismatch before a later operation produces an unexpected result.