← Tensor by Tensor
§ Math Fundamentals for AI
Tensor by Tensor · Complete Guide

Vectors

Fourteen lessons that connect high-school mathematics to AI. Start with the plain-language explanation and a small example. Then read the full formulas, derivations, and Python code at your own pace.

How to study a lesson

Read the opening example. Try the question before revealing its answer. Then work through the detailed notes below it. You do not need to understand every proof on the first reading.

Symbols: a subscript such as xi identifies one entry. Σ means add a collection of terms. ∈ means “belongs to”. The symbol ≈ means “approximately equal”, not exactly equal. A parameter is an adjustable number in a model.

01.03 · Vectors

Introduction

A model may receive several features at once, such as values 2 and 3. A vector keeps those values together, while a dot product combines them with weights 4 and 1 to produce one score, 11.

Learning goal

Represent feature lists as vectors and calculate, code, and interpret their dot product in small examples.

Before you start

Basic algebra, ordered lists, multiplication, addition, and simple Python or NumPy arrays.

Lesson plan

  1. Name vector dimensions, components, length, direction, and basic arithmetic operations.
  2. Calculate a weighted score by multiplying matching entries and adding them.
  3. Implement vector operations and connect dot products to angles and similarity.

Name the coordinates first. Let x=[2,3] mean two measured features. With weights w=[4,1], the dot product is 4×2+1×3=11. Swapping the vector entries changes the meaning and usually changes the answer.

What is a vector?

A vector is an ordered list of numbers. In AI, vectors represent features, parameters, gradients, and more:

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

Example: a study session might be represented as $\mathbf{x} = [2, 3]$, where $x_1 = 2$ hours reading and $x_2 = 3$ exercises attempted. Order matters: $[3, 2]$ is a different vector.

Definition · Vector operations

For vectors $\mathbf{u}, \mathbf{v} \in \mathbb{R}^n$ and scalar $c \in \mathbb{R}$:

  • Addition: $\mathbf{u} + \mathbf{v} = [u_1+v_1, \ldots, u_n+v_n]$
  • Scalar multiplication: $c\mathbf{u} = [cu_1, \ldots, cu_n]$
  • Dot product: $\mathbf{u} \cdot \mathbf{v} = \sum_{i=1}^n u_i v_i$
  • Norm (length): $\|\mathbf{u}\| = \sqrt{\mathbf{u} \cdot \mathbf{u}}$

The dot product

The dot product combines two vectors into a scalar. It measures how much one vector "aligns" with another:

$$\mathbf{w} \cdot \mathbf{x} = w_1 x_1 + w_2 x_2 + \cdots + w_n x_n$$

Example: with $\mathbf{w} = [4, 1]$ and $\mathbf{x} = [2, 3]$:

$$\mathbf{w} \cdot \mathbf{x} = 4(2) + 1(3) = 8 + 3 = 11$$
Example · Interpretation

If $\mathbf{w}$ represents weights (importance) and $\mathbf{x}$ represents features, the dot product gives a weighted score. Increasing $x_1$ by 1 adds $w_1 = 4$ to the score; increasing $x_2$ by 1 adds $w_2 = 1$.

Vector operations in code

import numpy as np

x = np.array([2, 3])
w = np.array([4, 1])

# Dot product
score = np.dot(w, x)  # or w @ x
print(score)  # 11

# Addition
y = x + np.array([1, 2])  # [3, 5]

# Scalar multiplication
z = 2 * x  # [4, 6]

# Norm
length = np.linalg.norm(x)  # sqrt(13) ≈ 3.606

Geometric interpretation

A vector $\mathbf{x} = [x_1, x_2]$ can be drawn as an arrow from the origin $(0,0)$ to the point $(x_1, x_2)$. The dot product has a geometric formula:

$$\mathbf{u} \cdot \mathbf{v} = \|\mathbf{u}\| \|\mathbf{v}\| \cos\theta$$

where $\theta$ is the angle between the vectors. This shows that:

  • $\mathbf{u} \cdot \mathbf{v} > 0$ when $\theta < 90°$ (vectors point in similar directions)
  • $\mathbf{u} \cdot \mathbf{v} = 0$ when $\theta = 90°$ (vectors are orthogonal/perpendicular)
  • $\mathbf{u} \cdot \mathbf{v} < 0$ when $\theta > 90°$ (vectors point in opposite directions)
Key insight

The dot product is the fundamental operation in neural networks. A linear layer computes $\mathbf{y} = W\mathbf{x} + \mathbf{b}$, where each output $y_i$ is the dot product of the $i$-th row of $W$ with $\mathbf{x}$.