Concepts / Tensor Shapes and Dimensions

Tensor Shapes and Dimensions

The rank of a tensor is the number of axes it has.

  • 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.

Tensornumber of axesRank 0scalarRank 1vectorRank 2matrix0 axes1 axis2 axes
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.

Worked example

Locating a Value in a Vector

Consider the generated vector [8, 3, 6]. How many axis positions are needed to identify the value 3?

  1. 1
    Classify the structure

    The values are arranged as one array of numbers, so the structure is a vector tensor.

  2. 2
    Count the axes

    A vector has one axis. Each value is located by one position along that axis.

  3. 3
    Locate the value

    The value 3 is identified by its single position in the vector; no row position and column position are needed.

Result

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?
Key point

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.

Worked example

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?

  1. 1
    Classify the structure

    The values are arranged in rows and columns, so the structure is a matrix tensor.

  2. 2
    Identify the two axes

    One axis corresponds to the row arrangement and the other corresponds to the column arrangement.

  3. 3
    Locate the value

    The value 7 is identified by its row position and its column position. Both positions are required.

Result

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

Mistakes with Axes and Rank

  • Common mistake

    Treating rank as the number of values

    What it looks like

    A vector contains several numbers, but its rank is not the number of numbers it contains.

    Why it fails

    Rank means the number of axes, not the amount of data stored along those axes.

    Do this instead

    Count the directions needed to locate one value. A vector has one axis and rank 1.

  • Common mistake

    Calling a vector a matrix because it contains many values

    What it looks like

    A single array of numbers is described as a matrix simply because it is long.

    Why it fails

    A matrix requires two axes and is arranged across rows and columns.

    Do this instead

    Check whether values need both a row position and a column position. If one position is enough, the structure is a vector.

  • Common mistake

    Ignoring the row axis or column axis in a matrix

    What it looks like

    A value in a matrix is described using only one position.

    Why it fails

    A matrix value is located using two positions, one along each of its two axes.

    Do this instead

    Identify both the row position and the column position.

  • Common mistake

    Assuming a later operation caused every unexpected result

    What it looks like

    An operation gives an unexpected result, but the tensor's number of axes is never checked.

    Why it fails

    The tensor may already have had a different structural category than expected.

    Do this instead

    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.
Worked example

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?

  1. 1
    Classify the arrangement

    An array of vectors is the matrix case.

  2. 2
    Count the axes

    The matrix uses two axes: one for rows and one for columns.

  3. 3
    Map the count to rank and ndim

    Two axes mean rank 2, and NumPy's ndim attribute reports 2.

Result

It is a matrix tensor with rank 2 and ndim equal to 2.

Summary

  1. 1
    A scalar is a single number that needs no axis, so it has rank 0.
  2. 2
    A vector is an array of numbers arranged along one axis, so it has rank 1.
  3. 3
    A matrix is an array of vectors arranged across rows and columns, so it has rank 2.
  4. 4
    Rank means the number of axes in a tensor.
  5. 5
    NumPy's ndim attribute reports the tensor's number of axes and therefore provides a direct check of its rank.

At a glance

  • 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.

Related concepts

More concepts starting with T