Lecture 01 - Vectors and Matrices
📚 Lecture 01 - Vectors and Matrices.pdf
Brief overview
This document introduces the foundational concepts of vectors and matrices, essential mathematical tools for representing and manipulating data. Understanding these concepts is crucial for data science and machine learning as they form the basis for many algorithms and data structures.
Key points
- Introduction to vectors, vector spaces, and fundamental vector operations.
- How vectors represent data and their applications in machine learning tasks.
- Challenges of high-dimensional vector spaces and the curse of dimensionality.
- An introduction to matrices as representations of linear transformations.
- Key matrix operations including multiplication, special forms, and inversion.
1. Introduction to vectors and vector spaces
1.1 The necessity of vectors in data representation
- Traditional text representations (strings) lack the structural and semantic depth required for advanced computational tasks like machine translation 3.
- Simple dictionary lookups of words can lead to nonsensical translations when context and nuanced meaning are absent 4.
- To imbue text fragments (words, phrases, sentences) with additional mathematical structure and enable semantic operations, they are placed within a vector space 5.
1.2 Understanding vectors and vector spaces
- Vectors are fundamentally ordered tuples of real numbers, each representing a distance along an orthogonal (independent) direction.
- A vector has a fixed dimension, denoted as n, representing the number of such orthogonal directions 7.
- The set of all n-dimensional vectors forms the vector space Rn.
- Vectors can be conceptualized as points in space, arrows originating from the origin, or simply numerical tuples. The "points in space" model is often most useful for data representation, where vectors represent data points and matrices warp this space 14.

Key Concept: Vectors are ordered tuples of real numbers with fixed dimensions.
1.3 Core vector operations
- Scalar multiplication: For a scalar a and a vector x=[x1,x2,…,xn], the operation ax results in a new vector where each element is scaled by a, i.e., [ax1,ax2,…,axn]. This operation maps R×Rn→Rn.
- Vector addition: For two vectors x=[x1,x2,…,xn] and y=[y1,y2,…,yn] of equal dimension, their sum x+y is a new vector where each element is the sum of the corresponding elements of x and y, i.e., [x1+y1,x2+y2,…,xn+yn]. This operation maps Rn×Rn→Rn 10.

1.4 Measuring vectors and their relationships
- Norm (∥x∥): A function that assigns a non-negative length to each vector, mapping Rn→R≥0. Vector spaces equipped with a norm are called normed vector spaces or topological vector spaces.
- Inner product (x⋅y or ⟨x,y⟩): An operation that determines the angle between two vectors, mapping Rn×Rn→R. For real vectors, it is defined as x⋅y=x1y1+x2y2+⋯+xnyn 11.
- The inner product of two orthogonal vectors is zero.
- Vector spaces equipped with an inner product are called inner product spaces 12.
1.4.1 Relationships between topological and geometric spaces
A hierarchy exists among different types of vector spaces:
| Space Type | Definition | Relationship to Others |
|---|---|---|
| Vector Space | Set with addition and scalar multiplication | Broadest category |
| Metric Space | Defines distance between points | Broader than Normed |
| Normed Vector Space | Vector space with a defined norm | Subset of Metric |
| Inner Product Space | Vector space with a defined inner product | Subset of Normed |

- A norm can be derived from an inner product: ∥x∥=⟨x,x⟩.
- However, not all norms can be derived from an inner product 13.

1.5 Vectors in practice
- In computational contexts, 1D floating-point arrays often serve as practical approximations of mathematical vectors 15.
- Vectors are a fundamental lingua franca for data because they can be composed, compared, and weighted.
- Libraries like NumPy utilize efficient ndarray data structures to operate on vectors concisely and efficiently 16.
Key Insight: Vectors provide the mathematical framework to represent and manipulate diverse data types, enabling semantic understanding and complex operations in data science.

2. Vector data representation and applications
2.1 Representing data as vectors
- Datasets are frequently structured as 2D tables, where each row represents an observation or data point, and each column signifies a feature or attribute.
- Each row can be interpreted as a vector in Rn, where n is the number of features.

2.2 Geometric operations with vectors
- Vectors enable fundamental geometric operations in multi-dimensional spaces.
- These operations include transformations such as scaling, rotation, mirroring, and translation, which are crucial for manipulating geometric data.
- Applications range from 3D model representations, where vertices are defined by vectors, to sophisticated algorithms in machine learning 18.
2.3 Vector applications in machine learning
- Machine learning extensively relies on vector representations.
- The core ML process involves transforming raw data into feature vectors and then developing functions to map these vectors to predictions (e.g., class labels).
- Many ML algorithms function by performing geometric operations on these vectors, such as calculating distances, warping data spaces, or computing angles.
2.3.1 Classification tasks: k-Nearest Neighbors
- The k-Nearest Neighbors (k-NN) algorithm is a prime example of a distance-based classification method.
- It classifies a new data point based on the majority class of its k nearest neighbors in the feature space.
- The process involves calculating the distance between the new data point and all training examples, identifying the k closest ones, and assigning the label that appears most frequently among them 19 21.

- The choice of k is critical and can significantly influence the prediction outcome.

- The selection of a suitable distance function (norm or cosine similarity) is also a key consideration for effective classification 22.
2.3.2 Image compression
- Images can be represented as vectors by treating pixel data, or groups of pixels (e.g., 8x8 patches), as elements of a high-dimensional vector. An 8x8 patch becomes a 64-dimensional vector.

- Image compression can be achieved by clustering these image vectors into a smaller set of representative vectors (a "codebook"). The compressed image is then reconstructed using these representative vectors, significantly reducing storage requirements when the number of representative vectors (m) is much smaller than the total number of patches (n) 23.
2.4 Measuring vector magnitude: Norms
- The "size" or magnitude of a vector is quantified using norms.
- The L2-norm, also known as the Euclidean norm, is commonly used and represents the straight-line distance from the origin to the vector's endpoint in Euclidean space. It is computed as the square root of the sum of the squares of its elements 26.
For a vector x=[x1,x2,…,xn], the L2-norm is: ∣∣x∣∣2=∑i=1nxi2
-
More generally, Lp-norms (or Minkowski norms) are defined as: ∣∣x∣∣p=(∑i=1n∣xi∣p)1/p 27.
-
Different norms result in different geometric interpretations of distance:
-
L1-norm (Taxicab norm): Sum of absolute values of elements. Contour lines are squares rotated by 45 degrees.
-
L2-norm (Euclidean norm): Square root of the sum of squared elements. Contour lines are circles (or spheres in higher dimensions).
-
L-infinity norm: Maximum absolute value of elements. Contour lines are squares aligned with the axes.

2.5 Unit vectors and normalization
- A unit vector is a vector with a norm of 1. The specific definition of a unit vector depends on the norm being used.
- Normalization, particularly for the Euclidean norm, involves scaling a vector x by 1/∣∣x∣∣2.
- Unit vectors are conceptually useful for representing pure direction, independent of magnitude 29.
2.6 Inner products and vector relationships
-
The inner product (or dot product for real vectors) of two vectors in Rn measures the angle between them and is computed as the sum of their elementwise products. x⋅y=∑i=1nxiyi 30.
-
The inner product is closely related to the cosine similarity, which is defined as: cosθ=∣∣x∣∣⋅∣∣y∣∣x⋅y
-
For unit vectors, the cosine similarity simplifies to the inner product itself, as their norms are 1.
-
The inner product is defined for vectors of the same dimension and within inner product spaces. It is distinct from the general dot product operation in some libraries 31.
2.7 Basic vector statistics
- Statistical operations common to scalar numbers can be generalized to vectors.
- The mean vector of a collection of N vectors is computed by averaging each dimension across all vectors. This vector represents the geometric centroid or "center of mass" of the vector set 32.
| Operation | Description | Typical Use |
|---|---|---|
| Mean vector | The geometric centroid capturing the average position of a set of vectors. | Finding the central tendency of multivariate data. |
| Centering | Subtracting the mean vector from each vector in a dataset to achieve zero mean. | Data preprocessing for algorithms sensitive to scale. |
- Centering a dataset involves subtracting the mean vector from each data point (row) to ensure the dataset has a zero mean. This is a common preprocessing step 33.
🔑 Important: Vector representations are foundational in modern data science, enabling sophisticated geometric computations that underpin many machine learning algorithms and data analysis techniques.
3. High-dimensional vector spaces and the curse of dimensionality
3.1 Understanding high-dimensional vector spaces
-
High-dimensional vector spaces are ubiquitous in data science, referring to spaces with more than a few dimensions (e.g., >3). Datasets with tens or hundreds of dimensions are common, and millions of dimensions are not unheard of.
-
The curse of dimensionality describes the phenomenon where algorithms that perform well in low-dimensional spaces degrade significantly as the number of dimensions increases. This degradation arises from the exponential increase in the data required for meaningful representation.
📝 Example: Consider predicting if tomorrow's temperature will exceed 30°C using weather station data. With one dimension (temperature), a simple histogram suffices. With two dimensions (temperature and humidity), a 2D histogram can visualize relationships.

-
As the number of features (dimensions) increases, the number of bins needed for a histogram grows exponentially. For instance, with 10 features, each discretized into 20 bins, we would require 2010 (over 10 trillion) bins.
-
This exponential growth in required bins vastly outstrips the available data points (e.g., 10,000 measurements), leading to sparse data and hindering generalization.
3.2 Matrices as linear operators
-
Matrices are fundamental mathematical objects, defined as 2D arrays of real numbers. They are used to represent linear maps or linear transformations between vector spaces.
-
While vectors can be thought of as points in space, matrices represent operations that transform these points.
-
A matrix A of dimensions n×m (denoted as A∈Rn×m) defines a function that maps vectors from an m-dimensional space to an n-dimensional space (Rm→Rn).

-
The core property of linear maps is linearity:
-
f(x+y)=f(x)+f(y) (the transform of a sum is the sum of transforms).
-
f(cx)=cf(x) (the transform of a scalar multiple is the scalar multiple of the transform). This property makes linear operations tractable and predictable.
-
When an n×n matrix maps a vector space onto itself (Rn→Rn), it is called a linear transform. A specific type of linear map is a linear projection, where applying the transformation twice yields the same result as applying it once (f(x)=f(f(x))).
🔑 Important: Linear maps preserve the structure of vector addition and scalar multiplication, making them analytically tractable.
3.3 Matrix operations and applications
- Several fundamental operations can be performed with matrices:
- Addition: C=A+B where A,B,C∈Rn×m. Requires matrices to have identical dimensions.
- Scalar Multiplication: C=sA where A,C∈Rn×m and s∈R. Each element of A is multiplied by s.
- Transposition: B=AT where A∈Rn×m and B∈Rm×n. Rows become columns and vice versa.

-
Matrices can also be applied to vectors (y=Ax). This operation maps an m-dimensional vector to an n-dimensional vector, resulting in a vector y∈Rn.
-
The application of a matrix to a vector can be understood as forming a linear combination (or weighted sum) of the matrix's columns, where the weights are the elements of the vector. Specifically, for A∈Rn×m and x∈Rm, the result y=Ax is computed by summing the i-th column of A scaled by the i-th element of x (xi) for all i from 1 to m.

- Linear transformations can geometrically warp space, such as rotations. For example, a 90-degree rotation on a 2D plane transforms the grid accordingly.

- The composition of two matrices, C=AB, represents the sequential application of the linear transformations defined by A and B 34.
🔑 Important: Matrix multiplication represents the composition of linear transformations. If A represents f and B represents g, then AB represents f∘g.
| Operation | Input Dimensions | Output Dimensions | Description |
|---|---|---|---|
| Addition | A∈Rn×m,B∈Rn×m | C∈Rn×m | Element-wise summation. |
| Scalar Multiplication | A∈Rn×m,s∈R | C∈Rn×m | Each element of A multiplied by scalar s. |
| Transposition | A∈Rn×m | B∈Rm×n | Rows and columns are interchanged. |
| Vector Application | A∈Rn×m,x∈Rm | y∈Rn | Linear combination of matrix columns by vector elements. |
| Matrix Multiplication | A∈Rn×m,B∈Rm×p | C∈Rn×p | Composition of linear transformations. |
🔑 Important: The curse of dimensionality implies that as dimensions increase, the volume of the space grows exponentially, leading to data sparsity and computational challenges for algorithms. Matrices provide a powerful framework for manipulating and transforming data in these high-dimensional spaces via linear operations.
4. Matrix multiplication, special forms, and inversion
4.1 Matrix multiplication
Matrix multiplication defines the product C=AB where A, B, and C are matrices. This operation is fundamental for composing linear transformations.
If matrix A represents linear transformation f(x) and matrix B represents linear transformation g(x), then the product BA represents the composite transformation g(f(x)), where the transformations are applied from right to left. This composition means that BAx=B(Ax), signifying that the transformation A is applied to x first, followed by the application of transformation B to the result 60.
Cij=∑kAikBkj

- Multiplication compatibility: For C=AB, matrix A must be of dimensions p×q and matrix B must be of dimensions q×r. The resulting matrix C will have dimensions p×r 61.
- Implementation: In Python, matrix multiplication is commonly performed using
np.dot(a, b)or the more concise@operator (a @ b) 62. - Non-commutativity: Matrix multiplication is generally not commutative, meaning AB=BA. The order of multiplication is critical 63.
4.2 Time complexity and special cases
The general time complexity for multiplying two square matrices of size n×n is O(n3). However, for matrices with specific structures, this complexity can be significantly reduced.
🔑 Important: Specialized algorithms exist that achieve complexities greater than O(n2) but less than O(n3) for general matrix multiplication 64.
4.3 Matrix transposition
The transpose of a matrix A, denoted as AT, is obtained by interchanging its rows and columns. Both .T and np.transpose(A) in NumPy can be used to compute the transpose 65.

4.4 Special matrix multiplication forms
Two common forms of matrix multiplication are:
- Outer product: The product of a p×1 vector and a 1×q vector results in a p×q matrix.
- Inner product: The product of a 1×q vector and a q×1 vector yields a 1×1 matrix, which is a scalar 66.
4.5 Matrix multiplication as composed linear transformations
Matrix multiplication is directly analogous to the composition of linear transformations. If A represents a linear transformation f(x) and B represents a linear transformation g(x), then the product BA represents the composite transformation g(f(x)).
This means that applying BA to a vector x is equivalent to first applying A to x and then applying B to the result 67.
🔑 Important: In graphics pipelines, multiple operations like translation, scaling, and rotation can be combined into a single matrix transform by multiplying their respective matrices 71.
4.6 Special matrix forms
Several matrix forms have properties that simplify operations and computations:
| Matrix Type | Description | NumPy Function |
|---|---|---|
| Diagonal matrix | Non-zero elements only on the main diagonal. | np.diag(x) |
| Identity matrix | Diagonal matrix with all diagonal elements equal to 1. | np.eye(n) |
| Zero matrix | All elements are zero. | np.zeros() |
| Upper triangular | All elements below the main diagonal are zero. | N/A |
| Lower triangular | All elements above the main diagonal are zero. | N/A |

4.7 Matrices for measuring spread: Covariance
Covariance matrices are crucial for understanding the spread and interdependencies of data.
- Variance: Measures the spread of a single dataset.
- Covariance: Measures how two datasets or variables co-vary.
- Covariance >0: Indicates variables tend to increase together.
- Covariance <0: Indicates variables tend to move in opposite directions.
- Covariance =0: Suggests no linear relationship between variables 72.
- Covariance Matrix Properties:
- The main diagonal contains the variances of individual variables.
- The matrix is symmetric.
- Eigenvalues are non-negative 73.

For a 2-dimensional dataset, a mean vector captures the "center," and a 2×2 covariance matrix captures the "spread" or orientation of the data points 75.

4.8 Matrix inversion
Matrix inversion provides an operation that "undoes" a linear transformation. If A is a matrix representing a linear transformation, its inverse, A−1, is a matrix such that A−1A=I, where I is the identity matrix.
This property implies that A−1(Ax)=x.
🔑 Important: Inversion is only defined for square matrices that are non-singular (i.e., they have a non-zero determinant).
Key properties of matrix inversion include:
- (A−1)−1=A: Inverting the inverse returns the original matrix.
- (AB)−1=B−1A−1: The inverse of a product of matrices is the product of their inverses in reverse order.
4.8.1 Inversion as an "undo" operation
The inverse of a matrix effectively creates a new linear operator that reverses the action of the original matrix. For example, if A is a rotation matrix, A−1 will be a rotation matrix in the opposite direction, returning any rotated vector to its original orientation 78.
🔑 Important: Matrix inversion is a fundamental concept for solving systems of linear equations and understanding the properties of linear transformations, acting as the multiplicative inverse in the realm of matrices 77.
© 2026 Maeve. All rights reserved. This content is protected by copyright.