Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Linear Algebra  ›  Lesson

Scalars, Vectors and Tensors

Scalars, Vectors, Tensors 12 minScalars, vectors, matrices and tensors are numbers arranged with zero, one, two or more indexes, and a shape says how many of each.

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)  matrix

The workhorse operation on vectors is the dot product: multiply matching entries and add. With weights w = [0.3, 1, -1], flat B scores 0.3×60+1×2+(−1)×1=190.3 \times 60 + 1 \times 2 + (-1) \times 1 = 19. 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.

Reading a year of listingsStep 1 of 4
  1. 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.

quick drill

What is the shape of flat D's feature vector (50 m², 2 rooms, 4 km)?

quick drill

How many numbers are in a tensor of shape (12, 5, 3)?

quick drill

With weights [0.3, 1, -1], what is the dot product with flat A's features [40, 1, 2]?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSay the shape

The agency stores 12 months of data for its 5 flats, each with 3 features. Explain what shape this tensor has, what each index means, how many numbers it holds, and why the rent of one flat in one month is a scalar.