Lesson 1 of 9 · Linear Algebra

Scalars and vectors

A scalar is a single quantity, and a vector is an ordered list of numbers with a length and a direction; together they represent the features and the weights in every machine learning model.

Mathematics for machine learning Difficulty: Beginner Study time: 35 min
Lesson contents
  1. Introduction
  2. Why study this topic?
  3. Theoretical explanation
  4. Mathematical definitions
  5. The equations
  6. Worked example
  7. Application in AI
  8. Interactive lab
  9. Practice exercises
  10. Summary
  11. Short quiz
  12. Related lessons

1 Introduction

When a machine learning model receives an image, a sentence or a patient record, all it sees is ordered numbers. The simplest of these objects is the Scalar , followed by the Vector , which gathers several numbers into one ordered structure.

This lesson gives the mathematical definition of a vector, how it is written, its length (norm), and its geometric reading as an arrow in the plane or in space.

Learning objectives

  • Telling a scalar and a vector apart
  • Writing a vector as a row or a column
  • Computing the length of a vector with the Euclidean norm and the L1 norm
  • Interpreting a vector geometrically

2 Why study this topic?

Every data sample is written as a feature vector: a house with an area, a number of rooms and an age is represented by the vector \(\mathbf{x} = (120, 3, 15)\). A neuron's weights are also a vector, and embeddings in language models are vectors with hundreds of dimensions. Understanding vectors is therefore a prerequisite for understanding any later algorithm.

3 Theoretical explanation

Scalar

A scalar is a single real number, such as the temperature \(27\) or the learning rate \(\eta = 0.01\). It is usually written as a small italic letter such as \(a\) or \(\lambda\).

Vector

A vector in \(\mathbb{R}^n\) is an ordered list of \(n\) real numbers called components. It is usually written as a column:

\[\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} \in \mathbb{R}^n\]

Order matters: the vector \((1,2)\) differs from \((2,1)\). The number \(n\) is called the dimension of the vector.

Geometric interpretation

In \(\mathbb{R}^2\) the vector \((3,4)\) is an arrow that starts at the origin and ends at the point \((3,4)\). In machine learning we usually deal with high dimensions that cannot be drawn, but the geometric intuition (length, direction and angle) still holds.

Vector length (norm)

The Euclidean length, or \(L_2\) norm, is the square root of the sum of the squared components. The \(L_1\) norm is the sum of the absolute values of the components, and it appears in Lasso-type regularization.

ConceptNotationExample
A scalar\(a\)\(a = 2.5\)
Vector\(\mathbf{x}\)\(\mathbf{x} = (3,4)\)
L2 length\(\lVert\mathbf{x}\rVert_2\)\(5\)
L1 length\(\lVert\mathbf{x}\rVert_1\)\(7\)
The vector u and the unit vector in its direction

4 Mathematical definitions

Vector
An element of the space \(\mathbb{R}^n\): an ordered list of n real numbers.
\[\mathbf{x} = (x_1, x_2, \dots, x_n)^\top\]
Euclidean norm
The length of a vector in Euclidean space.
\[\lVert \mathbf{x} \rVert_2 = \sqrt{\sum_{i=1}^{n} x_i^2}\]
Unit vector
A vector of length 1, obtained by dividing any non-zero vector by its length.
\[\hat{\mathbf{x}} = \frac{\mathbf{x}}{\lVert \mathbf{x} \rVert}\]
Vector
An ordered list of real numbers representing a point or an arrow in the space \(\mathbb{R}^n\), used to represent samples, weights and embeddings.
\[\mathbf{x} \in \mathbb{R}^n\]
Scalar
A single real number with no direction, such as the learning rate or the loss value.
\[a \in \mathbb{R}\]
Vector length (norm)
A function that measures the size of a vector; the best known are the Euclidean L2 norm and the L1 norm.
\[\lVert \mathbf{x} \rVert_2 = \sqrt{\textstyle\sum_i x_i^2}\]

5 The equations

\[\lVert \mathbf{x} \rVert_2 = \sqrt{x_1^2 + x_2^2 + \cdots + x_n^2}\]
The L2 norm
\[\lVert \mathbf{x} \rVert_1 = |x_1| + |x_2| + \cdots + |x_n|\]
The L1 norm

6 Step-by-step worked example

Vector length and unit vector

Problem: Compute the length of the vector \(\mathbf{u} = (3, 4)\) and the unit vector in its direction.

  1. Square the components: \(3^2 = 9\) and \(4^2 = 16\).
  2. Add: \(9 + 16 = 25\).
  3. Take the square root: \(\lVert \mathbf{u} \rVert = \sqrt{25} = 5\).
  4. The unit vector: \(\hat{\mathbf{u}} = (3/5, 4/5) = (0.6, 0.8)\), whose length is \(\sqrt{0.36+0.64}=1\).

Result: \(\lVert \mathbf{u} \rVert = 5\) and \(\hat{\mathbf{u}} = (0.6, 0.8)\)

7 Practical application in AI

  • Feature vector: Each row of a data table becomes a vector \(\mathbf{x} \in \mathbb{R}^d\) that enters the model.
  • Word embeddings: Language models represent each word or token as a dense vector, and closeness of meaning is measured by the distance or angle between vectors.
  • Input normalization: Dividing a vector by its length (L2 normalization) makes comparisons between samples independent of their scale.

8 Interactive lab linked to the lesson

Vector operations (addition, subtraction, dot product, length, distance and cosine similarity), matrix operations (addition, multiplication, transpose, determinant and inverse), eigenvalues and eigenvectors, and singular value decomposition (SVD) with lower-rank reconstruction.

Open the Linear Algebra Lab

9 Practice exercises

Review questions for reflection

  • Why does the order of a vector's components matter when representing data?
  • When is the length of a vector zero?
  • What is the practical difference between the L1 and L2 norms in regularization?

Common mistakes

  • Confusing the length of a vector with its number of components (its dimension).
  • Forgetting to take the square root when computing the L2 norm.
  • Treating the order of the components as unimportant.

Practice 6 questions from the question bank for this lesson

10 Lesson summary

  • A scalar is a single quantity, and a vector is an ordered list of numbers.
  • The L2 length of a vector is the root of the sum of squares, and L1 is the sum of absolute values.
  • Every data sample in machine learning is represented as a feature vector.

11 Short quiz

5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.