Gradient LabStart the lesson

Lessons › Chapter 2: Math toolkit

Vectors

A single number, like a temperature or a price, is called a scalar. Picture it as one bar.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

Customers as lists of numbers

To feed customers into a model, Leo turns each one into a list of numbers: income, age, balance.

Concept

A scalar is one number

A single number, like a temperature or a price, is called a scalar. Picture it as one bar.

Concept

A vector is a list of numbers

Line several numbers up and you have a vector. A house with size 3, 1 bathroom and 2 floors becomes [3, 1, 2]. ML turns every example into a vector like this.

Concept

A vector is also an arrow

Two numbers can be drawn as an arrow from the origin: go 2 along x, then 1 along y. The arrow has a direction and a length.

Build

Adding vectors: tip to tail

To add b, start it where a ends. The arrow from the origin to the final tip is a + b = [3, 4]. In numbers, just add matching components.

Build

Scaling stretches the arrow

Multiply a vector by a number and every component scales. 2 × a = [4, 2] points the same way, twice as long. A negative number flips it around.

Insight

The length of a vector

By Pythagoras, the length (or norm) of [2, 1] is √(2² + 1²) ≈ 2.24. ML uses lengths and distances constantly: how far apart are two examples?

Real life

In real life: songs as lists of numbers

Music apps can describe each song with a handful of numbers, like tempo, energy and mood. That turns every song into a vector. “Find songs like this one” then becomes “find arrows that point nearly the same way”.

Quick check: A house with size 120, 3 rooms and 10 years old becomes…

Each feature becomes one component of the example’s feature vector.

Pseudocode

# a vector is just a list of numbers
v = [3, 1, 2]

a = [2, 1]
b = [1, 3]
a + b        # [2+1, 1+3] = [3, 4]
2 × a        # [4, 2]: every number doubled
length(a)    # √(2² + 1²) ≈ 2.24

Key terms

scalar
A single number.
vector
A list of numbers. It can also be drawn as an arrow.
norm
The length of a vector.