Concepts / Tensor Shapes and Dimensions

Tensor Shapes and Dimensions

A scalar, vector, and matrix differ by the number of axes they have.

  • Programming

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.

Scalar0 axesVector1 axisMatrix2 axes
How do the structures differ in the number of axes needed to locate a value?

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 typeRankAxes needed to locate a valueArrangement
Scalar tensor0NoneOne number
Vector tensor1One position along one axisAn array of numbers
Matrix tensor2One row position and one column positionAn array of vectors arranged across rows and columns

Rank identifies the number of axes in each tensor type.

0 axes1 axis2 axesTensornumber of axesRank 0scalarRank 1vectorRank 2matrix
How does tensor rank map to the number of axes for scalar, vector, and matrix tensors?

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.

Position 08Position 13Position 26
How are values arranged along one axis, and how does each position correspond to a value?

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.

Row 0, Column 04Row 1, Column 02Row 0, Column 19Row 1, Column 17
How are values located using row and column positions across two axes?

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 structureNumber of axesRankndim result
Scalar000
Vector111
Matrix222

The ndim result matches the tensor's rank because both count axes.

  1. Classify the tensor as a scalar, vector, or matrix.
  2. Ask how many positions are needed to locate one value.
  3. Use the number of axes as the tensor's rank.
  4. Check the NumPy ndim attribute when you need a concrete confirmation.
  5. 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

EASY

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

  1. A scalar is a single number that needs no axis, so it has rank 0.
  2. A vector is an array of numbers arranged along one axis, so it has rank 1.
  3. A matrix is an array of vectors arranged across rows and columns, so it has rank 2.
  4. Rank means the number of axes in a tensor.
  5. 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.